|
| 1 | +# Unity Catalog Metric View using dbt |
| 2 | + |
| 3 | +This project demonstrates how to materialize a [Unity Catalog Metric View](https://docs.databricks.com/metric-views/) via dbt-databricks using Declarative Automation Bundles. The metric view is defined as a dbt model with the [`metric_view` materialization](https://github.com/databricks/dbt-databricks/pull/1285) added in dbt-databricks 1.12.0. |
| 4 | + |
| 5 | +For the SQL-job variant, see [`../metric_view`](../metric_view). |
| 6 | + |
| 7 | +## `bookings_kpis` metric view |
| 8 | + |
| 9 | +This project defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. |
| 10 | + |
| 11 | +The model lives in [`src/models/bookings_kpis.sql`](src/models/bookings_kpis.sql): |
| 12 | + |
| 13 | +```sql |
| 14 | +{{ config(materialized='metric_view') }} |
| 15 | + |
| 16 | +version: 1.0 |
| 17 | +source: samples.wanderbricks.bookings |
| 18 | +filter: status = 'confirmed' |
| 19 | +dimensions: |
| 20 | + - name: check_in_month |
| 21 | + expr: date_trunc('MONTH', check_in) |
| 22 | +measures: |
| 23 | + - name: total_bookings |
| 24 | + expr: COUNT(1) |
| 25 | + - name: total_revenue |
| 26 | + expr: SUM(total_amount) |
| 27 | +``` |
| 28 | + |
| 29 | +The `{{ config(materialized='metric_view') }}` line is the only jinja; everything below it is the metric view YAML body. dbt-databricks issues the equivalent `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` against the warehouse at `dbt run` time. |
| 30 | + |
| 31 | +Once materialized, query the metric view from any SQL editor: |
| 32 | + |
| 33 | +```sql |
| 34 | +SELECT |
| 35 | + check_in_month, |
| 36 | + MEASURE(total_bookings) AS bookings, |
| 37 | + MEASURE(total_revenue) AS revenue |
| 38 | +FROM <catalog>.<your_schema>.bookings_kpis |
| 39 | +GROUP BY check_in_month |
| 40 | +ORDER BY check_in_month; |
| 41 | +``` |
| 42 | + |
| 43 | +## Get started |
| 44 | + |
| 45 | +### Prerequisites |
| 46 | + |
| 47 | +* Databricks workspace with Unity Catalog enabled |
| 48 | +* A SQL warehouse on a runtime that supports Unity Catalog metric views |
| 49 | +* Databricks CLI installed and configured |
| 50 | + |
| 51 | +### Setup |
| 52 | + |
| 53 | +1. In `dbt_profiles/profiles.yml`, set `http_path` (in both `dev` and `prod`) to one of your warehouses (`databricks warehouses list`) and update `catalog` to one you can write to (the default `main` is often not writable). |
| 54 | + |
| 55 | +### Deployment |
| 56 | + |
| 57 | +Deploy to dev: |
| 58 | + |
| 59 | +```bash |
| 60 | +databricks bundle deploy --target dev |
| 61 | +``` |
| 62 | + |
| 63 | +```bash |
| 64 | +databricks bundle run metric_view_dbt_job --target dev |
| 65 | +``` |
| 66 | + |
| 67 | +Deploy to production: |
| 68 | + |
| 69 | +```bash |
| 70 | +databricks bundle deploy --target prod |
| 71 | +``` |
| 72 | + |
| 73 | +```bash |
| 74 | +databricks bundle run metric_view_dbt_job --target prod |
| 75 | +``` |
| 76 | + |
| 77 | +The metric view will be created at `<catalog>.<your_username>.bookings_kpis` (dev) or `<catalog>.prod.bookings_kpis` (prod). |
| 78 | + |
| 79 | +### Notes |
| 80 | + |
| 81 | +- The job has a daily `periodic` trigger so `dbt run` re-applies the view definition in production. [Development-mode](https://docs.databricks.com/dev-tools/bundles/deployment-modes.html) pauses the trigger automatically, so it only fires after `bundle deploy --target prod`. |
| 82 | +- Requires `dbt-databricks >= 1.12.0` (the version that introduced the `metric_view` materialization). The job pins this in its task environment. |
| 83 | +- The model file is `.sql` even though its body is YAML — dbt model files must use `.sql`. dbt-databricks wraps the body in `CREATE OR REPLACE VIEW … LANGUAGE YAML AS $$ … $$` at run time. |
| 84 | +- Set `source:` in the YAML body to any UC table you read from. For production, replace `samples.wanderbricks.bookings` with a table from your own pipeline. |
| 85 | + |
| 86 | +## Learn more |
| 87 | + |
| 88 | +- [Unity Catalog Metric Views](https://docs.databricks.com/metric-views/) — Official documentation |
| 89 | +- [Metric View YAML Reference](https://docs.databricks.com/metric-views/yaml-ref) |
| 90 | +- [`metric_view` materialization in dbt-databricks](https://github.com/databricks/dbt-databricks/pull/1285) |
| 91 | +- [Declarative Automation Bundles](https://docs.databricks.com/dev-tools/bundles/index.html) |
0 commit comments