From 0d833c86e35a8f564fce837a33595a37d5e58e9f Mon Sep 17 00:00:00 2001 From: Lennart Kats Date: Thu, 28 May 2026 11:22:01 +0200 Subject: [PATCH 1/7] knowledge_base: add metric_view and metric_view-dbt examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two parallel knowledge_base demos for creating a Unity Catalog Metric View from a bundle: - knowledge_base/metric_view/ — minimal sql_task-in-a-job pattern that runs CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML against a SQL warehouse. - knowledge_base/metric_view-dbt/ — dbt-databricks 1.12.0+ variant using the new `metric_view` materialization (dbt-databricks PR #1285). Both define bookings_kpis over samples.wanderbricks.bookings and were verified end-to-end on a real workspace (17,948 bookings / $9.9M revenue). Co-authored-by: Isaac --- knowledge_base/metric_view-dbt/README.md | 53 +++++++++++++++++++ knowledge_base/metric_view-dbt/databricks.yml | 42 +++++++++++++++ .../metric_view-dbt/dbt_profiles/profiles.yml | 32 +++++++++++ .../metric_view-dbt/dbt_project.yml | 25 +++++++++ .../resources/metric_view_dbt.job.yml | 27 ++++++++++ .../src/models/bookings_kpis.sql | 51 ++++++++++++++++++ .../metric_view-dbt/src/models/schema.yml | 8 +++ knowledge_base/metric_view/README.md | 47 ++++++++++++++++ knowledge_base/metric_view/databricks.yml | 50 +++++++++++++++++ .../resources/bookings_kpis.job.yml | 18 +++++++ .../src/bookings_kpis.metric_view.sql | 41 ++++++++++++++ 11 files changed, 394 insertions(+) create mode 100644 knowledge_base/metric_view-dbt/README.md create mode 100644 knowledge_base/metric_view-dbt/databricks.yml create mode 100644 knowledge_base/metric_view-dbt/dbt_profiles/profiles.yml create mode 100644 knowledge_base/metric_view-dbt/dbt_project.yml create mode 100644 knowledge_base/metric_view-dbt/resources/metric_view_dbt.job.yml create mode 100644 knowledge_base/metric_view-dbt/src/models/bookings_kpis.sql create mode 100644 knowledge_base/metric_view-dbt/src/models/schema.yml create mode 100644 knowledge_base/metric_view/README.md create mode 100644 knowledge_base/metric_view/databricks.yml create mode 100644 knowledge_base/metric_view/resources/bookings_kpis.job.yml create mode 100644 knowledge_base/metric_view/src/bookings_kpis.metric_view.sql diff --git a/knowledge_base/metric_view-dbt/README.md b/knowledge_base/metric_view-dbt/README.md new file mode 100644 index 0000000..13b25d0 --- /dev/null +++ b/knowledge_base/metric_view-dbt/README.md @@ -0,0 +1,53 @@ +# Unity Catalog Metric View (via dbt) + +This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using the [`metric_view` materialization](https://github.com/databricks/dbt-databricks/pull/1285) added in **dbt-databricks 1.12.0**. The deploy runs a Databricks job whose `dbt_task` executes `dbt run`, which in turn issues `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML` against your warehouse. + +See [`../metric_view`](../metric_view) for a variant that does the same thing with a raw `sql_task` (no dbt). + +## What it does + +Defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. Once `dbt run` has materialized it, query it like: + +```sql +SELECT + check_in_month, + MEASURE(total_bookings) AS bookings, + MEASURE(total_revenue) AS revenue, + MEASURE(avg_booking_value) AS aov +FROM main..bookings_kpis +WHERE check_in_date >= '2024-01-01' +GROUP BY check_in_month +ORDER BY check_in_month; +``` + +The metric view exposes these dimensions and measures (see [`src/models/bookings_kpis.sql`](src/models/bookings_kpis.sql)): + +- Dimensions: `check_in_date`, `check_in_month`, `status`, `guests_count` +- Measures: `total_bookings`, `total_revenue`, `avg_booking_value`, `total_guests` + +## Layout + +``` +metric_view-dbt/ + databricks.yml # bundle config and targets + dbt_project.yml # dbt project; points at src/models + dbt_profiles/profiles.yml # profile the deployed job uses + resources/metric_view_dbt.job.yml # job with one dbt_task + src/models/ + bookings_kpis.sql # the metric_view (jinja config + YAML body) + schema.yml # dbt model docs +``` + +## Getting started + +1. Point `dbt_profiles/profiles.yml` at one of your SQL warehouses (set `http_path`) and pick a `catalog`/`schema` you can write to. +2. `databricks bundle deploy`. +3. `databricks bundle run metric_view_dbt_job`. +4. Query the view from any SQL editor. + +## Notes + +- **dbt-databricks 1.12.0+ is required** — that's the version that introduced the `metric_view` materialization. The job pins it in the task environment (`resources/metric_view_dbt.job.yml`). +- Requires a Databricks Runtime / SQL warehouse with Metric View support (DBR 16.4+; serverless SQL warehouses qualify). +- The model file is `.sql` even though its body is YAML — dbt model files must use `.sql`, and the `metric_view` materialization wraps the body in `CREATE OR REPLACE VIEW ... LANGUAGE YAML AS $$ ... $$`. +- For production, replace `samples.wanderbricks.bookings` with a table from your own pipeline. diff --git a/knowledge_base/metric_view-dbt/databricks.yml b/knowledge_base/metric_view-dbt/databricks.yml new file mode 100644 index 0000000..cc4f132 --- /dev/null +++ b/knowledge_base/metric_view-dbt/databricks.yml @@ -0,0 +1,42 @@ +# This bundle creates a Unity Catalog Metric View using dbt-databricks. +# +# Metric Views are a Unity Catalog feature (Public Preview, Databricks Runtime 16.4+) +# that let you declare reusable dimensions and measures over a base table, queryable +# via the MEASURE() SQL function. dbt-databricks added a first-class `metric_view` +# materialization in v1.12.0 (https://github.com/databricks/dbt-databricks/pull/1285). +# +# See also the SQL-job variant at ../metric_view. +# +# Docs: +# https://docs.databricks.com/aws/en/metric-views/ +# https://docs.databricks.com/aws/en/metric-views/yaml-ref +# https://docs.getdbt.com/reference/resource-configs/databricks-configs + +bundle: + name: metric_view_dbt + +include: + - resources/*.yml + +# Deployment targets. +# The default schema/catalog for dbt are in dbt_profiles/profiles.yml. +targets: + dev: + # 'mode: development' creates a development copy: + # - Deployed resources are prefixed with '[dev my_user_name]'. + # - Job schedules/triggers are paused by default. + # See https://docs.databricks.com/dev-tools/bundles/deployment-modes.html. + mode: development + default: true + workspace: + host: https://company.databricks.com + + prod: + mode: production + workspace: + host: https://company.databricks.com + # Pin the deploy root so a single copy of the bundle lives in production. + root_path: /Workspace/Users/user@company.com/.bundle/${bundle.name}/${bundle.target} + permissions: + - user_name: user@company.com + level: CAN_MANAGE diff --git a/knowledge_base/metric_view-dbt/dbt_profiles/profiles.yml b/knowledge_base/metric_view-dbt/dbt_profiles/profiles.yml new file mode 100644 index 0000000..4e744bf --- /dev/null +++ b/knowledge_base/metric_view-dbt/dbt_profiles/profiles.yml @@ -0,0 +1,32 @@ +# dbt profiles used by the deployed dbt job in resources/metric_view_dbt.job.yml. +# +# For local development with the dbt CLI you should create your own profile in +# ~/.dbt/profiles.yml using `dbt init`; this file is only read by the job. +metric_view_dbt: + target: dev # default target + outputs: + + # The 'dev' target uses the workspace's current_user.short_name as the + # schema (passed in as the `dev_schema` var from the job; see the job YAML). + dev: + type: databricks + method: http + catalog: main + schema: "{{ var('dev_schema') }}" + + http_path: /sql/1.0/warehouses/abcdef1234567890 + + # DBT_HOST / DBT_ACCESS_TOKEN are injected by Databricks at run time. + host: "{{ env_var('DBT_HOST') }}" + token: "{{ env_var('DBT_ACCESS_TOKEN') }}" + + prod: + type: databricks + method: http + catalog: main + schema: default + + http_path: /sql/1.0/warehouses/abcdef1234567890 + + host: "{{ env_var('DBT_HOST') }}" + token: "{{ env_var('DBT_ACCESS_TOKEN') }}" diff --git a/knowledge_base/metric_view-dbt/dbt_project.yml b/knowledge_base/metric_view-dbt/dbt_project.yml new file mode 100644 index 0000000..2f408bf --- /dev/null +++ b/knowledge_base/metric_view-dbt/dbt_project.yml @@ -0,0 +1,25 @@ +name: 'metric_view_dbt' +version: '1.0.0' +config-version: 2 + +# Which 'profile' (in dbt_profiles/profiles.yml) dbt uses for this project. +profile: 'metric_view_dbt' + +# Everything dbt needs lives under src/ so that non-dbt bundle resources +# (such as the job in resources/) can sit alongside without confusing dbt. +model-paths: ["src/models"] +analysis-paths: ["src/analyses"] +test-paths: ["src/tests"] +seed-paths: ["src/seeds"] +macro-paths: ["src/macros"] +snapshot-paths: ["src/snapshots"] + +clean-targets: + - "target" + - "dbt_packages" + +# Default materialization is 'view'; the single metric_view model overrides +# this with `{{ config(materialized='metric_view') }}`. +models: + metric_view_dbt: + +materialized: view diff --git a/knowledge_base/metric_view-dbt/resources/metric_view_dbt.job.yml b/knowledge_base/metric_view-dbt/resources/metric_view_dbt.job.yml new file mode 100644 index 0000000..90b7c38 --- /dev/null +++ b/knowledge_base/metric_view-dbt/resources/metric_view_dbt.job.yml @@ -0,0 +1,27 @@ +resources: + jobs: + metric_view_dbt_job: + name: metric_view_dbt_job + description: Materializes the `bookings_kpis` Unity Catalog metric view via dbt-databricks. + + tasks: + - task_key: dbt + environment_key: default + dbt_task: + project_directory: ../ + # The default schema/catalog are defined in ../dbt_profiles/profiles.yml. + profiles_directory: dbt_profiles/ + # `dev_schema` is consumed by the 'dev' target in profiles.yml so + # each developer gets their own schema (matches DABs dev-mode prefixing). + commands: + - 'dbt deps --target=${bundle.target}' + - 'dbt run --target=${bundle.target} --vars "{ dev_schema: ${workspace.current_user.short_name} }"' + + environments: + - environment_key: default + spec: + environment_version: "4" + dependencies: + # The metric_view materialization landed in dbt-databricks 1.12.0 + # (https://github.com/databricks/dbt-databricks/pull/1285). + - dbt-databricks>=1.12.0,<2.0.0 diff --git a/knowledge_base/metric_view-dbt/src/models/bookings_kpis.sql b/knowledge_base/metric_view-dbt/src/models/bookings_kpis.sql new file mode 100644 index 0000000..9540026 --- /dev/null +++ b/knowledge_base/metric_view-dbt/src/models/bookings_kpis.sql @@ -0,0 +1,51 @@ +{# + A Unity Catalog Metric View, materialized by dbt-databricks. + + Everything below the `config(...)` line is the metric-view YAML body (see + https://docs.databricks.com/aws/en/metric-views/yaml-ref). The metric_view + materialization wraps it in: + + CREATE OR REPLACE VIEW WITH METRICS LANGUAGE YAML AS + + so the file looks like SQL to dbt but its contents are YAML. (These jinja + comments are stripped at compile time; SQL `--` comments would be passed + through verbatim into the YAML body and break the view definition.) + + Query the resulting metric view from any SQL editor: + + SELECT + check_in_month, + MEASURE(total_bookings) AS bookings, + MEASURE(total_revenue) AS revenue, + MEASURE(avg_booking_value) AS aov + FROM main..bookings_kpis + WHERE check_in_date >= '2024-01-01' + GROUP BY check_in_month + ORDER BY check_in_month; +#} +{{ config(materialized='metric_view') }} + +version: 0.1 +source: samples.wanderbricks.bookings + +filter: "status = 'confirmed'" + +dimensions: + - name: check_in_date + expr: check_in + - name: check_in_month + expr: date_trunc('MONTH', check_in) + - name: status + expr: status + - name: guests_count + expr: guests_count + +measures: + - name: total_bookings + expr: COUNT(1) + - name: total_revenue + expr: SUM(total_amount) + - name: avg_booking_value + expr: AVG(total_amount) + - name: total_guests + expr: SUM(guests_count) diff --git a/knowledge_base/metric_view-dbt/src/models/schema.yml b/knowledge_base/metric_view-dbt/src/models/schema.yml new file mode 100644 index 0000000..571b5ec --- /dev/null +++ b/knowledge_base/metric_view-dbt/src/models/schema.yml @@ -0,0 +1,8 @@ +version: 2 + +models: + - name: bookings_kpis + description: | + Unity Catalog Metric View over the public sample `samples.wanderbricks.bookings`. + Exposes booking-count, revenue, and average-value measures sliceable by date, + status, and guest count. Query via `MEASURE() ... GROUP BY `. diff --git a/knowledge_base/metric_view/README.md b/knowledge_base/metric_view/README.md new file mode 100644 index 0000000..e96377f --- /dev/null +++ b/knowledge_base/metric_view/README.md @@ -0,0 +1,47 @@ +# Unity Catalog Metric View (via a DABs job) + +This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using a Databricks job that runs `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` on a SQL warehouse. See [`../metric_view-dbt`](../metric_view-dbt) for a dbt-based variant. + +## What it does + +Defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. Once deployed and run, you can query it like: + +```sql +SELECT + check_in_month, + MEASURE(total_bookings) AS bookings, + MEASURE(total_revenue) AS revenue, + MEASURE(avg_booking_value) AS aov +FROM main..bookings_kpis +WHERE check_in_date >= '2024-01-01' +GROUP BY check_in_month +ORDER BY check_in_month; +``` + +The metric view exposes these dimensions and measures (see [`src/bookings_kpis.metric_view.sql`](src/bookings_kpis.metric_view.sql)): + +- Dimensions: `check_in_date`, `check_in_month`, `status`, `guests_count` +- Measures: `total_bookings`, `total_revenue`, `avg_booking_value`, `total_guests` + +## Layout + +``` +metric_view/ + databricks.yml # bundle config (catalog/schema/warehouse_id vars) + resources/bookings_kpis.job.yml # job with one sql_task + src/bookings_kpis.metric_view.sql # CREATE VIEW ... WITH METRICS LANGUAGE YAML +``` + +The job runs the SQL file on the warehouse you point it at. `{{catalog}}` and `{{schema}}` in the SQL are substituted from job parameters at run time. + +## Getting started + +1. Replace `` in `databricks.yml` with one of your warehouse IDs (`databricks warehouses list`). +2. `databricks bundle deploy`. +3. `databricks bundle run bookings_kpis_metric_view`. +4. Query the view from any SQL editor. + +## Notes + +- Requires Databricks Runtime / SQL warehouse with Metric View support (DBR 16.4+; serverless SQL warehouses are fine). +- For production, point `source:` at a curated table from your own pipeline rather than the public sample. diff --git a/knowledge_base/metric_view/databricks.yml b/knowledge_base/metric_view/databricks.yml new file mode 100644 index 0000000..0476c8c --- /dev/null +++ b/knowledge_base/metric_view/databricks.yml @@ -0,0 +1,50 @@ +# This bundle shows how to create a Unity Catalog Metric View via a DABs job. +# +# Metric Views are a Unity Catalog feature (Public Preview, Databricks Runtime 16.4+) +# that let you declare reusable dimensions and measures over a base table, queryable +# via the MEASURE() SQL function. DABs does not yet have a first-class resource type +# for metric views, so this example uses a SQL task in a job to run +# `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML`. +# +# See also the dbt-based variant at ../metric_view-dbt. +# +# Docs: +# https://docs.databricks.com/aws/en/metric-views/ +# https://docs.databricks.com/aws/en/metric-views/yaml-ref + +bundle: + name: metric_view + +include: + - resources/*.yml + +variables: + catalog: + description: The Unity Catalog catalog where the metric view will be created + schema: + description: The schema where the metric view will be created + warehouse_id: + description: SQL warehouse ID used to run the CREATE VIEW statement. Find one via `databricks warehouses list`. + +targets: + dev: + mode: development + default: true + workspace: + host: https://company.databricks.com + variables: + catalog: main + schema: ${workspace.current_user.short_name} + warehouse_id: + prod: + mode: production + workspace: + host: https://company.databricks.com + root_path: /Workspace/Users/user@company.com/.bundle/${bundle.name}/${bundle.target} + variables: + catalog: main + schema: prod + warehouse_id: + permissions: + - user_name: user@company.com + level: CAN_MANAGE diff --git a/knowledge_base/metric_view/resources/bookings_kpis.job.yml b/knowledge_base/metric_view/resources/bookings_kpis.job.yml new file mode 100644 index 0000000..5c2d25e --- /dev/null +++ b/knowledge_base/metric_view/resources/bookings_kpis.job.yml @@ -0,0 +1,18 @@ +resources: + jobs: + bookings_kpis_metric_view: + name: bookings_kpis_metric_view + description: Creates/refreshes the `bookings_kpis` Unity Catalog metric view. + + parameters: + - name: catalog + default: ${var.catalog} + - name: schema + default: ${var.schema} + + tasks: + - task_key: create_metric_view + sql_task: + warehouse_id: ${var.warehouse_id} + file: + path: ../src/bookings_kpis.metric_view.sql diff --git a/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql new file mode 100644 index 0000000..06af16d --- /dev/null +++ b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql @@ -0,0 +1,41 @@ +-- Create (or replace) a Unity Catalog Metric View over samples.wanderbricks.bookings. +-- See https://docs.databricks.com/aws/en/metric-views/yaml-ref for the YAML syntax. +-- +-- Once deployed and run, query the metric view from any SQL editor with: +-- SELECT MEASURE(total_bookings), MEASURE(total_revenue) +-- FROM ..bookings_kpis +-- WHERE check_in_month >= '2024-01-01' +-- GROUP BY check_in_month; + +USE CATALOG IDENTIFIER({{catalog}}); +USE IDENTIFIER({{schema}}); + +CREATE OR REPLACE VIEW bookings_kpis +WITH METRICS +LANGUAGE YAML +AS $$ +version: 0.1 +source: samples.wanderbricks.bookings + +filter: "status = 'confirmed'" + +dimensions: + - name: check_in_date + expr: check_in + - name: check_in_month + expr: date_trunc('MONTH', check_in) + - name: status + expr: status + - name: guests_count + expr: guests_count + +measures: + - name: total_bookings + expr: COUNT(1) + - name: total_revenue + expr: SUM(total_amount) + - name: avg_booking_value + expr: AVG(total_amount) + - name: total_guests + expr: SUM(guests_count) +$$; From a18d3c62ffb2ed440ab308e3d251ef29bc2a414e Mon Sep 17 00:00:00 2001 From: Lennart Kats Date: Thu, 28 May 2026 11:22:55 +0200 Subject: [PATCH 2/7] knowledge_base: rename metric_view-dbt to metric_view_dbt for naming consistency Co-authored-by: Isaac --- knowledge_base/metric_view/README.md | 2 +- knowledge_base/{metric_view-dbt => metric_view_dbt}/README.md | 2 +- .../{metric_view-dbt => metric_view_dbt}/databricks.yml | 0 .../dbt_profiles/profiles.yml | 0 .../{metric_view-dbt => metric_view_dbt}/dbt_project.yml | 0 .../resources/metric_view_dbt.job.yml | 0 .../src/models/bookings_kpis.sql | 0 .../{metric_view-dbt => metric_view_dbt}/src/models/schema.yml | 0 8 files changed, 2 insertions(+), 2 deletions(-) rename knowledge_base/{metric_view-dbt => metric_view_dbt}/README.md (99%) rename knowledge_base/{metric_view-dbt => metric_view_dbt}/databricks.yml (100%) rename knowledge_base/{metric_view-dbt => metric_view_dbt}/dbt_profiles/profiles.yml (100%) rename knowledge_base/{metric_view-dbt => metric_view_dbt}/dbt_project.yml (100%) rename knowledge_base/{metric_view-dbt => metric_view_dbt}/resources/metric_view_dbt.job.yml (100%) rename knowledge_base/{metric_view-dbt => metric_view_dbt}/src/models/bookings_kpis.sql (100%) rename knowledge_base/{metric_view-dbt => metric_view_dbt}/src/models/schema.yml (100%) diff --git a/knowledge_base/metric_view/README.md b/knowledge_base/metric_view/README.md index e96377f..335218c 100644 --- a/knowledge_base/metric_view/README.md +++ b/knowledge_base/metric_view/README.md @@ -1,6 +1,6 @@ # Unity Catalog Metric View (via a DABs job) -This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using a Databricks job that runs `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` on a SQL warehouse. See [`../metric_view-dbt`](../metric_view-dbt) for a dbt-based variant. +This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using a Databricks job that runs `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` on a SQL warehouse. See [`../metric_view_dbt`](../metric_view_dbt) for a dbt-based variant. ## What it does diff --git a/knowledge_base/metric_view-dbt/README.md b/knowledge_base/metric_view_dbt/README.md similarity index 99% rename from knowledge_base/metric_view-dbt/README.md rename to knowledge_base/metric_view_dbt/README.md index 13b25d0..b597dec 100644 --- a/knowledge_base/metric_view-dbt/README.md +++ b/knowledge_base/metric_view_dbt/README.md @@ -28,7 +28,7 @@ The metric view exposes these dimensions and measures (see [`src/models/bookings ## Layout ``` -metric_view-dbt/ +metric_view_dbt/ databricks.yml # bundle config and targets dbt_project.yml # dbt project; points at src/models dbt_profiles/profiles.yml # profile the deployed job uses diff --git a/knowledge_base/metric_view-dbt/databricks.yml b/knowledge_base/metric_view_dbt/databricks.yml similarity index 100% rename from knowledge_base/metric_view-dbt/databricks.yml rename to knowledge_base/metric_view_dbt/databricks.yml diff --git a/knowledge_base/metric_view-dbt/dbt_profiles/profiles.yml b/knowledge_base/metric_view_dbt/dbt_profiles/profiles.yml similarity index 100% rename from knowledge_base/metric_view-dbt/dbt_profiles/profiles.yml rename to knowledge_base/metric_view_dbt/dbt_profiles/profiles.yml diff --git a/knowledge_base/metric_view-dbt/dbt_project.yml b/knowledge_base/metric_view_dbt/dbt_project.yml similarity index 100% rename from knowledge_base/metric_view-dbt/dbt_project.yml rename to knowledge_base/metric_view_dbt/dbt_project.yml diff --git a/knowledge_base/metric_view-dbt/resources/metric_view_dbt.job.yml b/knowledge_base/metric_view_dbt/resources/metric_view_dbt.job.yml similarity index 100% rename from knowledge_base/metric_view-dbt/resources/metric_view_dbt.job.yml rename to knowledge_base/metric_view_dbt/resources/metric_view_dbt.job.yml diff --git a/knowledge_base/metric_view-dbt/src/models/bookings_kpis.sql b/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql similarity index 100% rename from knowledge_base/metric_view-dbt/src/models/bookings_kpis.sql rename to knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql diff --git a/knowledge_base/metric_view-dbt/src/models/schema.yml b/knowledge_base/metric_view_dbt/src/models/schema.yml similarity index 100% rename from knowledge_base/metric_view-dbt/src/models/schema.yml rename to knowledge_base/metric_view_dbt/src/models/schema.yml From 768b56411fa237544b344ad878780bcaf7886ef1 Mon Sep 17 00:00:00 2001 From: Lennart Kats Date: Thu, 28 May 2026 11:24:34 +0200 Subject: [PATCH 3/7] knowledge_base: tighten metric_view README titles Co-authored-by: Isaac --- knowledge_base/metric_view/README.md | 2 +- knowledge_base/metric_view_dbt/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/knowledge_base/metric_view/README.md b/knowledge_base/metric_view/README.md index 335218c..296ccb5 100644 --- a/knowledge_base/metric_view/README.md +++ b/knowledge_base/metric_view/README.md @@ -1,4 +1,4 @@ -# Unity Catalog Metric View (via a DABs job) +# Unity Catalog Metric View This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using a Databricks job that runs `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` on a SQL warehouse. See [`../metric_view_dbt`](../metric_view_dbt) for a dbt-based variant. diff --git a/knowledge_base/metric_view_dbt/README.md b/knowledge_base/metric_view_dbt/README.md index b597dec..e6c9596 100644 --- a/knowledge_base/metric_view_dbt/README.md +++ b/knowledge_base/metric_view_dbt/README.md @@ -1,4 +1,4 @@ -# Unity Catalog Metric View (via dbt) +# Unity Catalog Metric View using dbt This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using the [`metric_view` materialization](https://github.com/databricks/dbt-databricks/pull/1285) added in **dbt-databricks 1.12.0**. The deploy runs a Databricks job whose `dbt_task` executes `dbt run`, which in turn issues `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML` against your warehouse. From 3fe775ed9d2717a65e2fb035fe555e962ca8020e Mon Sep 17 00:00:00 2001 From: Lennart Kats Date: Thu, 28 May 2026 12:25:22 +0200 Subject: [PATCH 4/7] knowledge_base: address fresh-eyes verification feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From a fresh-user walkthrough of both metric_view examples: - Add `CREATE SCHEMA IF NOT EXISTS` to the SQL variant so the example works even when the target schema doesn't pre-exist (the dbt variant already handles this via the adapter). - Mention the `host: https://company.databricks.com` placeholder in both READMEs' Getting started — previously a silent first-time-user trap. - Note that the default `catalog: main` is often not writable; flag this in the Getting started for both variants. - Align the YAML body with the official docs: - bump version 0.1 → 1.0 - drop the redundant `status` dimension (the filter already pins it) - drop outer quotes on `filter:` - add `comment:` at the metric view level and on each measure - Soften the DBR / wrapper-internals claims in both READMEs to match what the docs actually guarantee. - Drop the misleading "serverless" qualifier on the warehouse placeholder. Co-authored-by: Isaac --- knowledge_base/metric_view/README.md | 13 +++++++------ knowledge_base/metric_view/databricks.yml | 4 ++-- .../metric_view/src/bookings_kpis.metric_view.sql | 12 ++++++++---- knowledge_base/metric_view_dbt/README.md | 15 ++++++++------- .../metric_view_dbt/src/models/bookings_kpis.sql | 11 +++++++---- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/knowledge_base/metric_view/README.md b/knowledge_base/metric_view/README.md index 296ccb5..5308db3 100644 --- a/knowledge_base/metric_view/README.md +++ b/knowledge_base/metric_view/README.md @@ -20,7 +20,7 @@ ORDER BY check_in_month; The metric view exposes these dimensions and measures (see [`src/bookings_kpis.metric_view.sql`](src/bookings_kpis.metric_view.sql)): -- Dimensions: `check_in_date`, `check_in_month`, `status`, `guests_count` +- Dimensions: `check_in_date`, `check_in_month`, `guests_count` - Measures: `total_bookings`, `total_revenue`, `avg_booking_value`, `total_guests` ## Layout @@ -36,12 +36,13 @@ The job runs the SQL file on the warehouse you point it at. `{{catalog}}` and `{ ## Getting started -1. Replace `` in `databricks.yml` with one of your warehouse IDs (`databricks warehouses list`). -2. `databricks bundle deploy`. -3. `databricks bundle run bookings_kpis_metric_view`. -4. Query the view from any SQL editor. +1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL, and replace `` with one of your warehouse IDs (`databricks warehouses list`). +2. If you don't have write access to `main`, change `catalog:` under `targets.dev.variables` to a catalog you can write to. +3. `databricks bundle deploy`. +4. `databricks bundle run bookings_kpis_metric_view`. +5. Query the view from any SQL editor. ## Notes -- Requires Databricks Runtime / SQL warehouse with Metric View support (DBR 16.4+; serverless SQL warehouses are fine). +- Requires a SQL warehouse on a runtime that supports Unity Catalog Metric Views (Public Preview; any recent serverless or PRO warehouse). - For production, point `source:` at a curated table from your own pipeline rather than the public sample. diff --git a/knowledge_base/metric_view/databricks.yml b/knowledge_base/metric_view/databricks.yml index 0476c8c..2bad204 100644 --- a/knowledge_base/metric_view/databricks.yml +++ b/knowledge_base/metric_view/databricks.yml @@ -35,7 +35,7 @@ targets: variables: catalog: main schema: ${workspace.current_user.short_name} - warehouse_id: + warehouse_id: prod: mode: production workspace: @@ -44,7 +44,7 @@ targets: variables: catalog: main schema: prod - warehouse_id: + warehouse_id: permissions: - user_name: user@company.com level: CAN_MANAGE diff --git a/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql index 06af16d..4ab700d 100644 --- a/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql +++ b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql @@ -7,6 +7,7 @@ -- WHERE check_in_month >= '2024-01-01' -- GROUP BY check_in_month; +CREATE SCHEMA IF NOT EXISTS IDENTIFIER({{catalog}} || '.' || {{schema}}); USE CATALOG IDENTIFIER({{catalog}}); USE IDENTIFIER({{schema}}); @@ -14,28 +15,31 @@ CREATE OR REPLACE VIEW bookings_kpis WITH METRICS LANGUAGE YAML AS $$ -version: 0.1 +version: 1.0 +comment: Booking KPIs (count, revenue, AOV, guests) over samples.wanderbricks.bookings. source: samples.wanderbricks.bookings -filter: "status = 'confirmed'" +filter: status = 'confirmed' dimensions: - name: check_in_date expr: check_in - name: check_in_month expr: date_trunc('MONTH', check_in) - - name: status - expr: status - name: guests_count expr: guests_count measures: - name: total_bookings expr: COUNT(1) + comment: Number of confirmed bookings. - name: total_revenue expr: SUM(total_amount) + comment: Total revenue across confirmed bookings. - name: avg_booking_value expr: AVG(total_amount) + comment: Average revenue per confirmed booking. - name: total_guests expr: SUM(guests_count) + comment: Total guests across confirmed bookings. $$; diff --git a/knowledge_base/metric_view_dbt/README.md b/knowledge_base/metric_view_dbt/README.md index e6c9596..8c2ee7d 100644 --- a/knowledge_base/metric_view_dbt/README.md +++ b/knowledge_base/metric_view_dbt/README.md @@ -22,7 +22,7 @@ ORDER BY check_in_month; The metric view exposes these dimensions and measures (see [`src/models/bookings_kpis.sql`](src/models/bookings_kpis.sql)): -- Dimensions: `check_in_date`, `check_in_month`, `status`, `guests_count` +- Dimensions: `check_in_date`, `check_in_month`, `guests_count` - Measures: `total_bookings`, `total_revenue`, `avg_booking_value`, `total_guests` ## Layout @@ -40,14 +40,15 @@ metric_view_dbt/ ## Getting started -1. Point `dbt_profiles/profiles.yml` at one of your SQL warehouses (set `http_path`) and pick a `catalog`/`schema` you can write to. -2. `databricks bundle deploy`. -3. `databricks bundle run metric_view_dbt_job`. -4. Query the view from any SQL editor. +1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL. +2. In `dbt_profiles/profiles.yml`, set `http_path` to one of your warehouses (`databricks warehouses list`) and update `catalog`/`schema` to something you can write to (the default is `main`, which is not always writable). +3. `databricks bundle deploy`. +4. `databricks bundle run metric_view_dbt_job`. +5. Query the view from any SQL editor. ## Notes - **dbt-databricks 1.12.0+ is required** — that's the version that introduced the `metric_view` materialization. The job pins it in the task environment (`resources/metric_view_dbt.job.yml`). -- Requires a Databricks Runtime / SQL warehouse with Metric View support (DBR 16.4+; serverless SQL warehouses qualify). -- The model file is `.sql` even though its body is YAML — dbt model files must use `.sql`, and the `metric_view` materialization wraps the body in `CREATE OR REPLACE VIEW ... LANGUAGE YAML AS $$ ... $$`. +- Requires a SQL warehouse on a runtime that supports Unity Catalog Metric Views (Public Preview; any recent serverless or PRO warehouse). +- The model file is `.sql` even though its body is YAML — dbt model files must use `.sql`, and the `metric_view` materialization issues the equivalent `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` against the warehouse. - For production, replace `samples.wanderbricks.bookings` with a table from your own pipeline. diff --git a/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql b/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql index 9540026..05f7f11 100644 --- a/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql +++ b/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql @@ -25,27 +25,30 @@ #} {{ config(materialized='metric_view') }} -version: 0.1 +version: 1.0 +comment: Booking KPIs (count, revenue, AOV, guests) over samples.wanderbricks.bookings. source: samples.wanderbricks.bookings -filter: "status = 'confirmed'" +filter: status = 'confirmed' dimensions: - name: check_in_date expr: check_in - name: check_in_month expr: date_trunc('MONTH', check_in) - - name: status - expr: status - name: guests_count expr: guests_count measures: - name: total_bookings expr: COUNT(1) + comment: Number of confirmed bookings. - name: total_revenue expr: SUM(total_amount) + comment: Total revenue across confirmed bookings. - name: avg_booking_value expr: AVG(total_amount) + comment: Average revenue per confirmed booking. - name: total_guests expr: SUM(guests_count) + comment: Total guests across confirmed bookings. From c75e28d053aa7b9621dfd31348f70ccc0c8220a1 Mon Sep 17 00:00:00 2001 From: Lennart Kats Date: Thu, 28 May 2026 13:21:07 +0200 Subject: [PATCH 5/7] knowledge_base: add daily trigger + restructure metric_view READMEs - Both jobs gain a `trigger: periodic: {interval: 1, unit: DAYS}` so the view definition is re-applied daily in production. mode: development auto-pauses the trigger; only fires after `bundle deploy --target prod`. - Restructure both READMEs to match the udtf-operator shape: Concrete Example (Definition + SQL Usage), Getting Started (Prerequisites / Setup / Deployment for dev and prod), Advanced Topics, Learn More. - Link Advanced Topics' scheduling note to the deployment-modes docs. Co-authored-by: Isaac --- knowledge_base/metric_view/README.md | 91 +++++++++++++---- .../resources/bookings_kpis.job.yml | 7 ++ knowledge_base/metric_view_dbt/README.md | 99 ++++++++++++++----- .../resources/metric_view_dbt.job.yml | 7 ++ 4 files changed, 156 insertions(+), 48 deletions(-) diff --git a/knowledge_base/metric_view/README.md b/knowledge_base/metric_view/README.md index 5308db3..804727f 100644 --- a/knowledge_base/metric_view/README.md +++ b/knowledge_base/metric_view/README.md @@ -1,10 +1,41 @@ # Unity Catalog Metric View -This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using a Databricks job that runs `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` on a SQL warehouse. See [`../metric_view_dbt`](../metric_view_dbt) for a dbt-based variant. +This project demonstrates how to create a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using Databricks Asset Bundles. Once registered, the metric view becomes available to analysts and BI tools across your workspace, queryable via the `MEASURE()` SQL function. -## What it does +**Learn more:** [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) · dbt-based variant: [`../metric_view_dbt`](../metric_view_dbt) -Defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. Once deployed and run, you can query it like: +## Concrete example: Definition and Usage + +This project defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. + +### Metric View Definition + +A SQL task in the job runs `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` from [`src/bookings_kpis.metric_view.sql`](src/bookings_kpis.metric_view.sql): + +```sql +CREATE OR REPLACE VIEW bookings_kpis +WITH METRICS +LANGUAGE YAML +AS $$ +version: 1.0 +source: samples.wanderbricks.bookings +filter: status = 'confirmed' +dimensions: + - name: check_in_month + expr: date_trunc('MONTH', check_in) +measures: + - name: total_bookings + expr: COUNT(1) + - name: total_revenue + expr: SUM(total_amount) +$$; +``` + +`{{catalog}}` and `{{schema}}` in the SQL file are substituted from job parameters at run time. + +### SQL Usage + +Once registered, query the metric view from any SQL editor: ```sql SELECT @@ -18,31 +49,49 @@ GROUP BY check_in_month ORDER BY check_in_month; ``` -The metric view exposes these dimensions and measures (see [`src/bookings_kpis.metric_view.sql`](src/bookings_kpis.metric_view.sql)): +The view integrates seamlessly with: -- Dimensions: `check_in_date`, `check_in_month`, `guests_count` -- Measures: `total_bookings`, `total_revenue`, `avg_booking_value`, `total_guests` +- SQL editors and notebooks +- Databricks SQL dashboards / AI/BI Genie +- Any BI tool that connects to your workspace -## Layout +## Getting Started With This Project -``` -metric_view/ - databricks.yml # bundle config (catalog/schema/warehouse_id vars) - resources/bookings_kpis.job.yml # job with one sql_task - src/bookings_kpis.metric_view.sql # CREATE VIEW ... WITH METRICS LANGUAGE YAML -``` +### Prerequisites -The job runs the SQL file on the warehouse you point it at. `{{catalog}}` and `{{schema}}` in the SQL are substituted from job parameters at run time. +* Databricks workspace with Unity Catalog enabled +* A SQL warehouse on a runtime that supports Unity Catalog metric views (Public Preview; any recent serverless or PRO warehouse) +* Databricks CLI installed and configured -## Getting started +### Setup 1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL, and replace `` with one of your warehouse IDs (`databricks warehouses list`). 2. If you don't have write access to `main`, change `catalog:` under `targets.dev.variables` to a catalog you can write to. -3. `databricks bundle deploy`. -4. `databricks bundle run bookings_kpis_metric_view`. -5. Query the view from any SQL editor. -## Notes +### Deployment + +Deploy to dev: +```bash +databricks bundle deploy --target dev +databricks bundle run bookings_kpis_metric_view --target dev +``` + +Deploy to production: +```bash +databricks bundle deploy --target prod +databricks bundle run bookings_kpis_metric_view --target prod +``` + +The metric view will be created at `..bookings_kpis` (dev) or `.prod.bookings_kpis` (prod). + +## Advanced Topics + +**Scheduling:** The job has a daily `periodic` trigger so the view definition is re-applied in production. [Development-mode](https://docs.databricks.com/dev-tools/bundles/deployment-modes.html) deploys pause the trigger automatically, so it only fires after `bundle deploy --target prod`. + +**Custom source table:** Point `source:` in the YAML body at any UC table you read from. The sample `samples.wanderbricks.bookings` is convenient for getting started; for production, use a curated table from your own pipeline. + +## Learn More -- Requires a SQL warehouse on a runtime that supports Unity Catalog Metric Views (Public Preview; any recent serverless or PRO warehouse). -- For production, point `source:` at a curated table from your own pipeline rather than the public sample. +- [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) — Official documentation +- [Metric View YAML Reference](https://docs.databricks.com/aws/en/metric-views/yaml-ref) +- [Databricks Asset Bundles](https://docs.databricks.com/dev-tools/bundles/index.html) diff --git a/knowledge_base/metric_view/resources/bookings_kpis.job.yml b/knowledge_base/metric_view/resources/bookings_kpis.job.yml index 5c2d25e..ba4a49c 100644 --- a/knowledge_base/metric_view/resources/bookings_kpis.job.yml +++ b/knowledge_base/metric_view/resources/bookings_kpis.job.yml @@ -4,6 +4,13 @@ resources: name: bookings_kpis_metric_view description: Creates/refreshes the `bookings_kpis` Unity Catalog metric view. + trigger: + # Re-apply the view definition daily. Dev-mode deploys pause this trigger; + # in prod the job runs once per day. See https://docs.databricks.com/api/workspace/jobs/create#trigger + periodic: + interval: 1 + unit: DAYS + parameters: - name: catalog default: ${var.catalog} diff --git a/knowledge_base/metric_view_dbt/README.md b/knowledge_base/metric_view_dbt/README.md index 8c2ee7d..c46db90 100644 --- a/knowledge_base/metric_view_dbt/README.md +++ b/knowledge_base/metric_view_dbt/README.md @@ -1,12 +1,38 @@ # Unity Catalog Metric View using dbt -This example creates a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using the [`metric_view` materialization](https://github.com/databricks/dbt-databricks/pull/1285) added in **dbt-databricks 1.12.0**. The deploy runs a Databricks job whose `dbt_task` executes `dbt run`, which in turn issues `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML` against your warehouse. +This project demonstrates how to materialize a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) via dbt-databricks using Databricks Asset 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**. -See [`../metric_view`](../metric_view) for a variant that does the same thing with a raw `sql_task` (no dbt). +**Learn more:** [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) · SQL-job variant: [`../metric_view`](../metric_view) -## What it does +## Concrete example: Definition and Usage -Defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. Once `dbt run` has materialized it, query it like: +This project defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. + +### Metric View Definition (dbt model) + +The model lives in [`src/models/bookings_kpis.sql`](src/models/bookings_kpis.sql): + +```sql +{{ config(materialized='metric_view') }} + +version: 1.0 +source: samples.wanderbricks.bookings +filter: status = 'confirmed' +dimensions: + - name: check_in_month + expr: date_trunc('MONTH', check_in) +measures: + - name: total_bookings + expr: COUNT(1) + - name: total_revenue + expr: SUM(total_amount) +``` + +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. + +### SQL Usage + +Once materialized, query the metric view from any SQL editor: ```sql SELECT @@ -20,35 +46,54 @@ GROUP BY check_in_month ORDER BY check_in_month; ``` -The metric view exposes these dimensions and measures (see [`src/models/bookings_kpis.sql`](src/models/bookings_kpis.sql)): +The view integrates seamlessly with: + +- SQL editors and notebooks +- Databricks SQL dashboards / AI/BI Genie +- Any BI tool that connects to your workspace + +## Getting Started With This Project -- Dimensions: `check_in_date`, `check_in_month`, `guests_count` -- Measures: `total_bookings`, `total_revenue`, `avg_booking_value`, `total_guests` +### Prerequisites -## Layout +* Databricks workspace with Unity Catalog enabled +* A SQL warehouse on a runtime that supports Unity Catalog metric views (Public Preview; any recent serverless or PRO warehouse) +* Databricks CLI installed and configured +### Setup + +1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL. +2. In `dbt_profiles/profiles.yml`, set `http_path` to one of your warehouses (`databricks warehouses list`) and update `catalog` to one you can write to (the default `main` is often not writable). + +### Deployment + +Deploy to dev: +```bash +databricks bundle deploy --target dev +databricks bundle run metric_view_dbt_job --target dev ``` -metric_view_dbt/ - databricks.yml # bundle config and targets - dbt_project.yml # dbt project; points at src/models - dbt_profiles/profiles.yml # profile the deployed job uses - resources/metric_view_dbt.job.yml # job with one dbt_task - src/models/ - bookings_kpis.sql # the metric_view (jinja config + YAML body) - schema.yml # dbt model docs + +Deploy to production: +```bash +databricks bundle deploy --target prod +databricks bundle run metric_view_dbt_job --target prod ``` -## Getting started +The metric view will be created at `..bookings_kpis` (dev) or `.default.bookings_kpis` (prod). -1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL. -2. In `dbt_profiles/profiles.yml`, set `http_path` to one of your warehouses (`databricks warehouses list`) and update `catalog`/`schema` to something you can write to (the default is `main`, which is not always writable). -3. `databricks bundle deploy`. -4. `databricks bundle run metric_view_dbt_job`. -5. Query the view from any SQL editor. +## Advanced Topics + +**Scheduling:** 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) deploys pause the trigger automatically, so it only fires after `bundle deploy --target prod`. + +**dbt-databricks version:** Requires `dbt-databricks >= 1.12.0` (the `metric_view` materialization). The job pins this in its task environment (`resources/metric_view_dbt.job.yml`). + +**File extension:** 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. + +**Custom source table:** Point `source:` in the YAML body at any UC table you read from. For production, replace the public `samples.wanderbricks.bookings` with a curated table from your own pipeline. -## Notes +## Learn More -- **dbt-databricks 1.12.0+ is required** — that's the version that introduced the `metric_view` materialization. The job pins it in the task environment (`resources/metric_view_dbt.job.yml`). -- Requires a SQL warehouse on a runtime that supports Unity Catalog Metric Views (Public Preview; any recent serverless or PRO warehouse). -- The model file is `.sql` even though its body is YAML — dbt model files must use `.sql`, and the `metric_view` materialization issues the equivalent `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` against the warehouse. -- For production, replace `samples.wanderbricks.bookings` with a table from your own pipeline. +- [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) — Official documentation +- [Metric View YAML Reference](https://docs.databricks.com/aws/en/metric-views/yaml-ref) +- [`metric_view` materialization in dbt-databricks](https://github.com/databricks/dbt-databricks/pull/1285) +- [Databricks Asset Bundles](https://docs.databricks.com/dev-tools/bundles/index.html) diff --git a/knowledge_base/metric_view_dbt/resources/metric_view_dbt.job.yml b/knowledge_base/metric_view_dbt/resources/metric_view_dbt.job.yml index 90b7c38..970bd1b 100644 --- a/knowledge_base/metric_view_dbt/resources/metric_view_dbt.job.yml +++ b/knowledge_base/metric_view_dbt/resources/metric_view_dbt.job.yml @@ -4,6 +4,13 @@ resources: name: metric_view_dbt_job description: Materializes the `bookings_kpis` Unity Catalog metric view via dbt-databricks. + trigger: + # Re-apply the view definition daily. Dev-mode deploys pause this trigger; + # in prod the job runs once per day. See https://docs.databricks.com/api/workspace/jobs/create#trigger + periodic: + interval: 1 + unit: DAYS + tasks: - task_key: dbt environment_key: default From 31881cc4f20b84f4fda004235760287e82e9694e Mon Sep 17 00:00:00 2001 From: "Lennart Kats (databricks)" Date: Sat, 30 May 2026 10:22:13 +0200 Subject: [PATCH 6/7] Apply suggestions from code review Co-authored-by: Julia Crawford (Databricks) --- knowledge_base/metric_view/README.md | 21 +++++++++---------- knowledge_base/metric_view/databricks.yml | 16 +++++++------- .../resources/bookings_kpis.job.yml | 2 +- .../src/bookings_kpis.metric_view.sql | 2 +- knowledge_base/metric_view_dbt/README.md | 8 +++---- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/knowledge_base/metric_view/README.md b/knowledge_base/metric_view/README.md index 804727f..14173b2 100644 --- a/knowledge_base/metric_view/README.md +++ b/knowledge_base/metric_view/README.md @@ -1,14 +1,13 @@ # Unity Catalog Metric View -This project demonstrates how to create a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using Databricks Asset Bundles. Once registered, the metric view becomes available to analysts and BI tools across your workspace, queryable via the `MEASURE()` SQL function. +This project demonstrates how to create a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using Declarative Automation Bundles. Once registered, the metric view becomes available to analysts and BI tools across your workspace, queryable via the `MEASURE()` SQL function. -**Learn more:** [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) · dbt-based variant: [`../metric_view_dbt`](../metric_view_dbt) ## Concrete example: Definition and Usage -This project defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. +## `bookings_kpis` metric view -### Metric View Definition +This project defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. A SQL task in the job runs `CREATE OR REPLACE VIEW … WITH METRICS LANGUAGE YAML` from [`src/bookings_kpis.metric_view.sql`](src/bookings_kpis.metric_view.sql): @@ -60,7 +59,7 @@ The view integrates seamlessly with: ### Prerequisites * Databricks workspace with Unity Catalog enabled -* A SQL warehouse on a runtime that supports Unity Catalog metric views (Public Preview; any recent serverless or PRO warehouse) +* A SQL warehouse on a runtime that supports Unity Catalog metric views * Databricks CLI installed and configured ### Setup @@ -84,14 +83,14 @@ databricks bundle run bookings_kpis_metric_view --target prod The metric view will be created at `..bookings_kpis` (dev) or `.prod.bookings_kpis` (prod). -## Advanced Topics +### Notes -**Scheduling:** The job has a daily `periodic` trigger so the view definition is re-applied in production. [Development-mode](https://docs.databricks.com/dev-tools/bundles/deployment-modes.html) deploys pause the trigger automatically, so it only fires after `bundle deploy --target prod`. +- The job has a daily `periodic` trigger so the view definition is re-applied 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`. -**Custom source table:** Point `source:` in the YAML body at any UC table you read from. The sample `samples.wanderbricks.bookings` is convenient for getting started; for production, use a curated table from your own pipeline. +- Set `source:` in the YAML body to any UC table you read from. The sample `samples.wanderbricks.bookings` is convenient for getting started. For production, use a table from your own pipeline. ## Learn More -- [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) — Official documentation -- [Metric View YAML Reference](https://docs.databricks.com/aws/en/metric-views/yaml-ref) -- [Databricks Asset Bundles](https://docs.databricks.com/dev-tools/bundles/index.html) +- [Unity Catalog Metric Views](https://docs.databricks.com/metric-views/) — Official documentation +- [Metric View YAML Reference](https://docs.databricks.com/metric-views/yaml-ref) +- [Declarative Automation Bundles](https://docs.databricks.com/dev-tools/bundles/index.html) diff --git a/knowledge_base/metric_view/databricks.yml b/knowledge_base/metric_view/databricks.yml index 2bad204..3838f1d 100644 --- a/knowledge_base/metric_view/databricks.yml +++ b/knowledge_base/metric_view/databricks.yml @@ -1,16 +1,16 @@ -# This bundle shows how to create a Unity Catalog Metric View via a DABs job. +# This example bundle defines a job that creates a Unity Catalog Metric View. # -# Metric Views are a Unity Catalog feature (Public Preview, Databricks Runtime 16.4+) +# Metric Views are a Unity Catalog feature # that let you declare reusable dimensions and measures over a base table, queryable -# via the MEASURE() SQL function. DABs does not yet have a first-class resource type -# for metric views, so this example uses a SQL task in a job to run +# via the MEASURE() SQL function. Bundles do not yet support metric views as +# a resource type, so this example uses a SQL task in a job to run # `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML`. # # See also the dbt-based variant at ../metric_view-dbt. # # Docs: -# https://docs.databricks.com/aws/en/metric-views/ -# https://docs.databricks.com/aws/en/metric-views/yaml-ref +# https://docs.databricks.com/metric-views/ +# https://docs.databricks.com/metric-views/yaml-ref bundle: name: metric_view @@ -20,11 +20,11 @@ include: variables: catalog: - description: The Unity Catalog catalog where the metric view will be created + description: The Unity Catalog where the metric view will be created schema: description: The schema where the metric view will be created warehouse_id: - description: SQL warehouse ID used to run the CREATE VIEW statement. Find one via `databricks warehouses list`. + description: SQL warehouse ID used to run the CREATE VIEW statement. To list available warehouses, use `databricks warehouses list`. targets: dev: diff --git a/knowledge_base/metric_view/resources/bookings_kpis.job.yml b/knowledge_base/metric_view/resources/bookings_kpis.job.yml index ba4a49c..a3a9fa5 100644 --- a/knowledge_base/metric_view/resources/bookings_kpis.job.yml +++ b/knowledge_base/metric_view/resources/bookings_kpis.job.yml @@ -5,7 +5,7 @@ resources: description: Creates/refreshes the `bookings_kpis` Unity Catalog metric view. trigger: - # Re-apply the view definition daily. Dev-mode deploys pause this trigger; + # Re-apply the view definition daily. Dev deployment mode pauses this trigger; # in prod the job runs once per day. See https://docs.databricks.com/api/workspace/jobs/create#trigger periodic: interval: 1 diff --git a/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql index 4ab700d..b261564 100644 --- a/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql +++ b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql @@ -1,5 +1,5 @@ -- Create (or replace) a Unity Catalog Metric View over samples.wanderbricks.bookings. --- See https://docs.databricks.com/aws/en/metric-views/yaml-ref for the YAML syntax. +-- See https://docs.databricks.com/metric-views/yaml-ref for the YAML syntax. -- -- Once deployed and run, query the metric view from any SQL editor with: -- SELECT MEASURE(total_bookings), MEASURE(total_revenue) diff --git a/knowledge_base/metric_view_dbt/README.md b/knowledge_base/metric_view_dbt/README.md index c46db90..29960c5 100644 --- a/knowledge_base/metric_view_dbt/README.md +++ b/knowledge_base/metric_view_dbt/README.md @@ -1,6 +1,6 @@ # Unity Catalog Metric View using dbt -This project demonstrates how to materialize a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) via dbt-databricks using Databricks Asset 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**. +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**. **Learn more:** [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) · SQL-job variant: [`../metric_view`](../metric_view) @@ -93,7 +93,7 @@ The metric view will be created at `..bookings_kpis` (de ## Learn More -- [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) — Official documentation -- [Metric View YAML Reference](https://docs.databricks.com/aws/en/metric-views/yaml-ref) +- [Unity Catalog Metric Views](https://docs.databricks.com/metric-views/) — Official documentation +- [Metric View YAML Reference](https://docs.databricks.com/metric-views/yaml-ref) - [`metric_view` materialization in dbt-databricks](https://github.com/databricks/dbt-databricks/pull/1285) -- [Databricks Asset Bundles](https://docs.databricks.com/dev-tools/bundles/index.html) +- [Declarative Automation Bundles](https://docs.databricks.com/dev-tools/bundles/index.html) From 0c2e8789e650a6bf541d3d7984552efca63425f0 Mon Sep 17 00:00:00 2001 From: Lennart Kats Date: Mon, 1 Jun 2026 13:00:02 +0200 Subject: [PATCH 7/7] knowledge_base: apply review feedback to metric_view examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apply Julia's PR #156 inline suggestions (and parallel changes for the metric_view_dbt variant) plus a few related cleanups surfaced by a clean-context review: - Drop the dbt-variant cross-link from metric_view/README.md (don't advertise dbt from the canonical example). Keep the inverse link. - Drop the `workspace:` block (and prod root_path / permissions placeholders) from both databricks.yml — let the user's profile pin the host. - Tighten databricks.yml comments: drop the dbt-PR/version reference, drop "Bundles do not yet support" staleness risk. - Periods on variable descriptions. - README structure: drop redundant subheaders, `## Get started` (not "Getting Started With This Project"), `## Learn more` (sentence case), `### Notes` instead of `## Advanced Topics`. - Snippet matches query (no undefined fields), placeholder consistency (., no main. mix-up). - Drop `aws/en/` from doc URLs (also in dbt model file). - "Once registered" instead of "Once deployed" — accurate, matches Julia's suggestion. - Unify prod schema across variants (dbt prod schema was `default`, SQL was `prod` — now both `prod`). - Drop redundant "view integrates seamlessly with..." marketing line. - Individual code blocks per command for easy copy-paste. Co-authored-by: Isaac --- knowledge_base/metric_view/README.md | 38 ++++++------- knowledge_base/metric_view/databricks.yml | 22 ++------ .../src/bookings_kpis.metric_view.sql | 2 +- knowledge_base/metric_view_dbt/README.md | 56 ++++++++----------- knowledge_base/metric_view_dbt/databricks.yml | 29 +++------- .../metric_view_dbt/dbt_profiles/profiles.yml | 2 +- .../src/models/bookings_kpis.sql | 4 +- 7 files changed, 56 insertions(+), 97 deletions(-) diff --git a/knowledge_base/metric_view/README.md b/knowledge_base/metric_view/README.md index 14173b2..1466eee 100644 --- a/knowledge_base/metric_view/README.md +++ b/knowledge_base/metric_view/README.md @@ -1,9 +1,6 @@ # Unity Catalog Metric View -This project demonstrates how to create a [Unity Catalog Metric View](https://docs.databricks.com/aws/en/metric-views/) using Declarative Automation Bundles. Once registered, the metric view becomes available to analysts and BI tools across your workspace, queryable via the `MEASURE()` SQL function. - - -## Concrete example: Definition and Usage +This project demonstrates how to create a [Unity Catalog Metric View](https://docs.databricks.com/metric-views/) using Declarative Automation Bundles. Once registered, the metric view becomes available to analysts and BI tools across your workspace, queryable via the `MEASURE()` SQL function. ## `bookings_kpis` metric view @@ -32,29 +29,19 @@ $$; `{{catalog}}` and `{{schema}}` in the SQL file are substituted from job parameters at run time. -### SQL Usage - Once registered, query the metric view from any SQL editor: ```sql SELECT check_in_month, - MEASURE(total_bookings) AS bookings, - MEASURE(total_revenue) AS revenue, - MEASURE(avg_booking_value) AS aov -FROM main..bookings_kpis -WHERE check_in_date >= '2024-01-01' + MEASURE(total_bookings) AS bookings, + MEASURE(total_revenue) AS revenue +FROM ..bookings_kpis GROUP BY check_in_month ORDER BY check_in_month; ``` -The view integrates seamlessly with: - -- SQL editors and notebooks -- Databricks SQL dashboards / AI/BI Genie -- Any BI tool that connects to your workspace - -## Getting Started With This Project +## Get started ### Prerequisites @@ -64,20 +51,28 @@ The view integrates seamlessly with: ### Setup -1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL, and replace `` with one of your warehouse IDs (`databricks warehouses list`). -2. If you don't have write access to `main`, change `catalog:` under `targets.dev.variables` to a catalog you can write to. +1. In `databricks.yml`, replace `` (in both `targets.dev` and `targets.prod`) with one of your warehouse IDs (`databricks warehouses list`). +2. If you don't have write access to `main`, change `catalog:` under `variables` to a catalog you can write to. ### Deployment Deploy to dev: + ```bash databricks bundle deploy --target dev +``` + +```bash databricks bundle run bookings_kpis_metric_view --target dev ``` Deploy to production: + ```bash databricks bundle deploy --target prod +``` + +```bash databricks bundle run bookings_kpis_metric_view --target prod ``` @@ -86,10 +81,9 @@ The metric view will be created at `..bookings_kpis` (de ### Notes - The job has a daily `periodic` trigger so the view definition is re-applied 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`. - - Set `source:` in the YAML body to any UC table you read from. The sample `samples.wanderbricks.bookings` is convenient for getting started. For production, use a table from your own pipeline. -## Learn More +## Learn more - [Unity Catalog Metric Views](https://docs.databricks.com/metric-views/) — Official documentation - [Metric View YAML Reference](https://docs.databricks.com/metric-views/yaml-ref) diff --git a/knowledge_base/metric_view/databricks.yml b/knowledge_base/metric_view/databricks.yml index 3838f1d..abe5582 100644 --- a/knowledge_base/metric_view/databricks.yml +++ b/knowledge_base/metric_view/databricks.yml @@ -1,12 +1,8 @@ # This example bundle defines a job that creates a Unity Catalog Metric View. # -# Metric Views are a Unity Catalog feature -# that let you declare reusable dimensions and measures over a base table, queryable -# via the MEASURE() SQL function. Bundles do not yet support metric views as -# a resource type, so this example uses a SQL task in a job to run -# `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML`. -# -# See also the dbt-based variant at ../metric_view-dbt. +# Metric Views are a Unity Catalog feature that let you declare reusable +# dimensions and measures over a base table, queryable via the MEASURE() SQL +# function. # # Docs: # https://docs.databricks.com/metric-views/ @@ -20,9 +16,9 @@ include: variables: catalog: - description: The Unity Catalog where the metric view will be created + description: The Unity Catalog where the metric view will be created. schema: - description: The schema where the metric view will be created + description: The schema where the metric view will be created. warehouse_id: description: SQL warehouse ID used to run the CREATE VIEW statement. To list available warehouses, use `databricks warehouses list`. @@ -30,21 +26,13 @@ targets: dev: mode: development default: true - workspace: - host: https://company.databricks.com variables: catalog: main schema: ${workspace.current_user.short_name} warehouse_id: prod: mode: production - workspace: - host: https://company.databricks.com - root_path: /Workspace/Users/user@company.com/.bundle/${bundle.name}/${bundle.target} variables: catalog: main schema: prod warehouse_id: - permissions: - - user_name: user@company.com - level: CAN_MANAGE diff --git a/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql index b261564..7e90400 100644 --- a/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql +++ b/knowledge_base/metric_view/src/bookings_kpis.metric_view.sql @@ -3,7 +3,7 @@ -- -- Once deployed and run, query the metric view from any SQL editor with: -- SELECT MEASURE(total_bookings), MEASURE(total_revenue) --- FROM ..bookings_kpis +-- FROM ..bookings_kpis -- WHERE check_in_month >= '2024-01-01' -- GROUP BY check_in_month; diff --git a/knowledge_base/metric_view_dbt/README.md b/knowledge_base/metric_view_dbt/README.md index 29960c5..608d63e 100644 --- a/knowledge_base/metric_view_dbt/README.md +++ b/knowledge_base/metric_view_dbt/README.md @@ -1,15 +1,13 @@ # Unity Catalog Metric View using dbt -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**. +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. -**Learn more:** [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) · SQL-job variant: [`../metric_view`](../metric_view) +For the SQL-job variant, see [`../metric_view`](../metric_view). -## Concrete example: Definition and Usage +## `bookings_kpis` metric view This project defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`. -### Metric View Definition (dbt model) - The model lives in [`src/models/bookings_kpis.sql`](src/models/bookings_kpis.sql): ```sql @@ -30,68 +28,62 @@ measures: 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. -### SQL Usage - Once materialized, query the metric view from any SQL editor: ```sql SELECT check_in_month, - MEASURE(total_bookings) AS bookings, - MEASURE(total_revenue) AS revenue, - MEASURE(avg_booking_value) AS aov -FROM main..bookings_kpis -WHERE check_in_date >= '2024-01-01' + MEASURE(total_bookings) AS bookings, + MEASURE(total_revenue) AS revenue +FROM ..bookings_kpis GROUP BY check_in_month ORDER BY check_in_month; ``` -The view integrates seamlessly with: - -- SQL editors and notebooks -- Databricks SQL dashboards / AI/BI Genie -- Any BI tool that connects to your workspace - -## Getting Started With This Project +## Get started ### Prerequisites * Databricks workspace with Unity Catalog enabled -* A SQL warehouse on a runtime that supports Unity Catalog metric views (Public Preview; any recent serverless or PRO warehouse) +* A SQL warehouse on a runtime that supports Unity Catalog metric views * Databricks CLI installed and configured ### Setup -1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL. -2. In `dbt_profiles/profiles.yml`, set `http_path` to one of your warehouses (`databricks warehouses list`) and update `catalog` to one you can write to (the default `main` is often not writable). +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). ### Deployment Deploy to dev: + ```bash databricks bundle deploy --target dev +``` + +```bash databricks bundle run metric_view_dbt_job --target dev ``` Deploy to production: + ```bash databricks bundle deploy --target prod -databricks bundle run metric_view_dbt_job --target prod ``` -The metric view will be created at `..bookings_kpis` (dev) or `.default.bookings_kpis` (prod). - -## Advanced Topics - -**Scheduling:** 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) deploys pause the trigger automatically, so it only fires after `bundle deploy --target prod`. +```bash +databricks bundle run metric_view_dbt_job --target prod +``` -**dbt-databricks version:** Requires `dbt-databricks >= 1.12.0` (the `metric_view` materialization). The job pins this in its task environment (`resources/metric_view_dbt.job.yml`). +The metric view will be created at `..bookings_kpis` (dev) or `.prod.bookings_kpis` (prod). -**File extension:** 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. +### Notes -**Custom source table:** Point `source:` in the YAML body at any UC table you read from. For production, replace the public `samples.wanderbricks.bookings` with a curated table from your own pipeline. +- 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`. +- Requires `dbt-databricks >= 1.12.0` (the version that introduced the `metric_view` materialization). The job pins this in its task environment. +- 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. +- 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. -## Learn More +## Learn more - [Unity Catalog Metric Views](https://docs.databricks.com/metric-views/) — Official documentation - [Metric View YAML Reference](https://docs.databricks.com/metric-views/yaml-ref) diff --git a/knowledge_base/metric_view_dbt/databricks.yml b/knowledge_base/metric_view_dbt/databricks.yml index cc4f132..bce62b0 100644 --- a/knowledge_base/metric_view_dbt/databricks.yml +++ b/knowledge_base/metric_view_dbt/databricks.yml @@ -1,15 +1,14 @@ -# This bundle creates a Unity Catalog Metric View using dbt-databricks. +# This example bundle materializes a Unity Catalog Metric View using dbt-databricks. # -# Metric Views are a Unity Catalog feature (Public Preview, Databricks Runtime 16.4+) -# that let you declare reusable dimensions and measures over a base table, queryable -# via the MEASURE() SQL function. dbt-databricks added a first-class `metric_view` -# materialization in v1.12.0 (https://github.com/databricks/dbt-databricks/pull/1285). +# Metric Views are a Unity Catalog feature that let you declare reusable +# dimensions and measures over a base table, queryable via the MEASURE() SQL +# function. # # See also the SQL-job variant at ../metric_view. # # Docs: -# https://docs.databricks.com/aws/en/metric-views/ -# https://docs.databricks.com/aws/en/metric-views/yaml-ref +# https://docs.databricks.com/metric-views/ +# https://docs.databricks.com/metric-views/yaml-ref # https://docs.getdbt.com/reference/resource-configs/databricks-configs bundle: @@ -19,24 +18,10 @@ include: - resources/*.yml # Deployment targets. -# The default schema/catalog for dbt are in dbt_profiles/profiles.yml. +# The schema and catalog for dbt are configured in dbt_profiles/profiles.yml. targets: dev: - # 'mode: development' creates a development copy: - # - Deployed resources are prefixed with '[dev my_user_name]'. - # - Job schedules/triggers are paused by default. - # See https://docs.databricks.com/dev-tools/bundles/deployment-modes.html. mode: development default: true - workspace: - host: https://company.databricks.com - prod: mode: production - workspace: - host: https://company.databricks.com - # Pin the deploy root so a single copy of the bundle lives in production. - root_path: /Workspace/Users/user@company.com/.bundle/${bundle.name}/${bundle.target} - permissions: - - user_name: user@company.com - level: CAN_MANAGE diff --git a/knowledge_base/metric_view_dbt/dbt_profiles/profiles.yml b/knowledge_base/metric_view_dbt/dbt_profiles/profiles.yml index 4e744bf..81059dc 100644 --- a/knowledge_base/metric_view_dbt/dbt_profiles/profiles.yml +++ b/knowledge_base/metric_view_dbt/dbt_profiles/profiles.yml @@ -24,7 +24,7 @@ metric_view_dbt: type: databricks method: http catalog: main - schema: default + schema: prod http_path: /sql/1.0/warehouses/abcdef1234567890 diff --git a/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql b/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql index 05f7f11..f9cf46d 100644 --- a/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql +++ b/knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql @@ -2,7 +2,7 @@ A Unity Catalog Metric View, materialized by dbt-databricks. Everything below the `config(...)` line is the metric-view YAML body (see - https://docs.databricks.com/aws/en/metric-views/yaml-ref). The metric_view + https://docs.databricks.com/metric-views/yaml-ref). The metric_view materialization wraps it in: CREATE OR REPLACE VIEW WITH METRICS LANGUAGE YAML AS @@ -18,7 +18,7 @@ MEASURE(total_bookings) AS bookings, MEASURE(total_revenue) AS revenue, MEASURE(avg_booking_value) AS aov - FROM main..bookings_kpis + FROM ..bookings_kpis WHERE check_in_date >= '2024-01-01' GROUP BY check_in_month ORDER BY check_in_month;