Skip to content

Commit 0c2e878

Browse files
knowledge_base: apply review feedback to metric_view examples
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 (<catalog>.<your_schema>, no main.<your_schema> 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
1 parent 31881cc commit 0c2e878

7 files changed

Lines changed: 56 additions & 97 deletions

File tree

knowledge_base/metric_view/README.md

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Unity Catalog Metric View
22

3-
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.
4-
5-
6-
## Concrete example: Definition and Usage
3+
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.
74

85
## `bookings_kpis` metric view
96

@@ -32,29 +29,19 @@ $$;
3229

3330
`{{catalog}}` and `{{schema}}` in the SQL file are substituted from job parameters at run time.
3431

35-
### SQL Usage
36-
3732
Once registered, query the metric view from any SQL editor:
3833

3934
```sql
4035
SELECT
4136
check_in_month,
42-
MEASURE(total_bookings) AS bookings,
43-
MEASURE(total_revenue) AS revenue,
44-
MEASURE(avg_booking_value) AS aov
45-
FROM main.<your_schema>.bookings_kpis
46-
WHERE check_in_date >= '2024-01-01'
37+
MEASURE(total_bookings) AS bookings,
38+
MEASURE(total_revenue) AS revenue
39+
FROM <catalog>.<your_schema>.bookings_kpis
4740
GROUP BY check_in_month
4841
ORDER BY check_in_month;
4942
```
5043

51-
The view integrates seamlessly with:
52-
53-
- SQL editors and notebooks
54-
- Databricks SQL dashboards / AI/BI Genie
55-
- Any BI tool that connects to your workspace
56-
57-
## Getting Started With This Project
44+
## Get started
5845

5946
### Prerequisites
6047

@@ -64,20 +51,28 @@ The view integrates seamlessly with:
6451

6552
### Setup
6653

67-
1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL, and replace `<your-warehouse-id>` with one of your warehouse IDs (`databricks warehouses list`).
68-
2. If you don't have write access to `main`, change `catalog:` under `targets.dev.variables` to a catalog you can write to.
54+
1. In `databricks.yml`, replace `<your-warehouse-id>` (in both `targets.dev` and `targets.prod`) with one of your warehouse IDs (`databricks warehouses list`).
55+
2. If you don't have write access to `main`, change `catalog:` under `variables` to a catalog you can write to.
6956

7057
### Deployment
7158

7259
Deploy to dev:
60+
7361
```bash
7462
databricks bundle deploy --target dev
63+
```
64+
65+
```bash
7566
databricks bundle run bookings_kpis_metric_view --target dev
7667
```
7768

7869
Deploy to production:
70+
7971
```bash
8072
databricks bundle deploy --target prod
73+
```
74+
75+
```bash
8176
databricks bundle run bookings_kpis_metric_view --target prod
8277
```
8378

@@ -86,10 +81,9 @@ The metric view will be created at `<catalog>.<your_username>.bookings_kpis` (de
8681
### Notes
8782

8883
- 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`.
89-
9084
- 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.
9185

92-
## Learn More
86+
## Learn more
9387

9488
- [Unity Catalog Metric Views](https://docs.databricks.com/metric-views/) — Official documentation
9589
- [Metric View YAML Reference](https://docs.databricks.com/metric-views/yaml-ref)
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
# This example bundle defines a job that creates a Unity Catalog Metric View.
22
#
3-
# Metric Views are a Unity Catalog feature
4-
# that let you declare reusable dimensions and measures over a base table, queryable
5-
# via the MEASURE() SQL function. Bundles do not yet support metric views as
6-
# a resource type, so this example uses a SQL task in a job to run
7-
# `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML`.
8-
#
9-
# See also the dbt-based variant at ../metric_view-dbt.
3+
# Metric Views are a Unity Catalog feature that let you declare reusable
4+
# dimensions and measures over a base table, queryable via the MEASURE() SQL
5+
# function.
106
#
117
# Docs:
128
# https://docs.databricks.com/metric-views/
@@ -20,31 +16,23 @@ include:
2016

2117
variables:
2218
catalog:
23-
description: The Unity Catalog where the metric view will be created
19+
description: The Unity Catalog where the metric view will be created.
2420
schema:
25-
description: The schema where the metric view will be created
21+
description: The schema where the metric view will be created.
2622
warehouse_id:
2723
description: SQL warehouse ID used to run the CREATE VIEW statement. To list available warehouses, use `databricks warehouses list`.
2824

2925
targets:
3026
dev:
3127
mode: development
3228
default: true
33-
workspace:
34-
host: https://company.databricks.com
3529
variables:
3630
catalog: main
3731
schema: ${workspace.current_user.short_name}
3832
warehouse_id: <your-warehouse-id>
3933
prod:
4034
mode: production
41-
workspace:
42-
host: https://company.databricks.com
43-
root_path: /Workspace/Users/user@company.com/.bundle/${bundle.name}/${bundle.target}
4435
variables:
4536
catalog: main
4637
schema: prod
4738
warehouse_id: <your-warehouse-id>
48-
permissions:
49-
- user_name: user@company.com
50-
level: CAN_MANAGE

knowledge_base/metric_view/src/bookings_kpis.metric_view.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--
44
-- Once deployed and run, query the metric view from any SQL editor with:
55
-- SELECT MEASURE(total_bookings), MEASURE(total_revenue)
6-
-- FROM <catalog>.<schema>.bookings_kpis
6+
-- FROM <catalog>.<your_schema>.bookings_kpis
77
-- WHERE check_in_month >= '2024-01-01'
88
-- GROUP BY check_in_month;
99

knowledge_base/metric_view_dbt/README.md

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# Unity Catalog Metric View using dbt
22

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**.
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.
44

5-
**Learn more:** [Unity Catalog Metric Views](https://docs.databricks.com/aws/en/metric-views/) · SQL-job variant: [`../metric_view`](../metric_view)
5+
For the SQL-job variant, see [`../metric_view`](../metric_view).
66

7-
## Concrete example: Definition and Usage
7+
## `bookings_kpis` metric view
88

99
This project defines `bookings_kpis`, a metric view over the public sample dataset `samples.wanderbricks.bookings`.
1010

11-
### Metric View Definition (dbt model)
12-
1311
The model lives in [`src/models/bookings_kpis.sql`](src/models/bookings_kpis.sql):
1412

1513
```sql
@@ -30,68 +28,62 @@ measures:
3028

3129
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.
3230

33-
### SQL Usage
34-
3531
Once materialized, query the metric view from any SQL editor:
3632

3733
```sql
3834
SELECT
3935
check_in_month,
40-
MEASURE(total_bookings) AS bookings,
41-
MEASURE(total_revenue) AS revenue,
42-
MEASURE(avg_booking_value) AS aov
43-
FROM main.<your_schema>.bookings_kpis
44-
WHERE check_in_date >= '2024-01-01'
36+
MEASURE(total_bookings) AS bookings,
37+
MEASURE(total_revenue) AS revenue
38+
FROM <catalog>.<your_schema>.bookings_kpis
4539
GROUP BY check_in_month
4640
ORDER BY check_in_month;
4741
```
4842

49-
The view integrates seamlessly with:
50-
51-
- SQL editors and notebooks
52-
- Databricks SQL dashboards / AI/BI Genie
53-
- Any BI tool that connects to your workspace
54-
55-
## Getting Started With This Project
43+
## Get started
5644

5745
### Prerequisites
5846

5947
* Databricks workspace with Unity Catalog enabled
60-
* A SQL warehouse on a runtime that supports Unity Catalog metric views (Public Preview; any recent serverless or PRO warehouse)
48+
* A SQL warehouse on a runtime that supports Unity Catalog metric views
6149
* Databricks CLI installed and configured
6250

6351
### Setup
6452

65-
1. In `databricks.yml`, replace `https://company.databricks.com` with your workspace URL.
66-
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).
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).
6754

6855
### Deployment
6956

7057
Deploy to dev:
58+
7159
```bash
7260
databricks bundle deploy --target dev
61+
```
62+
63+
```bash
7364
databricks bundle run metric_view_dbt_job --target dev
7465
```
7566

7667
Deploy to production:
68+
7769
```bash
7870
databricks bundle deploy --target prod
79-
databricks bundle run metric_view_dbt_job --target prod
8071
```
8172

82-
The metric view will be created at `<catalog>.<your_username>.bookings_kpis` (dev) or `<catalog>.default.bookings_kpis` (prod).
83-
84-
## Advanced Topics
85-
86-
**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`.
73+
```bash
74+
databricks bundle run metric_view_dbt_job --target prod
75+
```
8776

88-
**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`).
77+
The metric view will be created at `<catalog>.<your_username>.bookings_kpis` (dev) or `<catalog>.prod.bookings_kpis` (prod).
8978

90-
**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.
79+
### Notes
9180

92-
**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.
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.
9385

94-
## Learn More
86+
## Learn more
9587

9688
- [Unity Catalog Metric Views](https://docs.databricks.com/metric-views/) — Official documentation
9789
- [Metric View YAML Reference](https://docs.databricks.com/metric-views/yaml-ref)
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
# This bundle creates a Unity Catalog Metric View using dbt-databricks.
1+
# This example bundle materializes a Unity Catalog Metric View using dbt-databricks.
22
#
3-
# Metric Views are a Unity Catalog feature (Public Preview, Databricks Runtime 16.4+)
4-
# that let you declare reusable dimensions and measures over a base table, queryable
5-
# via the MEASURE() SQL function. dbt-databricks added a first-class `metric_view`
6-
# materialization in v1.12.0 (https://github.com/databricks/dbt-databricks/pull/1285).
3+
# Metric Views are a Unity Catalog feature that let you declare reusable
4+
# dimensions and measures over a base table, queryable via the MEASURE() SQL
5+
# function.
76
#
87
# See also the SQL-job variant at ../metric_view.
98
#
109
# Docs:
11-
# https://docs.databricks.com/aws/en/metric-views/
12-
# https://docs.databricks.com/aws/en/metric-views/yaml-ref
10+
# https://docs.databricks.com/metric-views/
11+
# https://docs.databricks.com/metric-views/yaml-ref
1312
# https://docs.getdbt.com/reference/resource-configs/databricks-configs
1413

1514
bundle:
@@ -19,24 +18,10 @@ include:
1918
- resources/*.yml
2019

2120
# Deployment targets.
22-
# The default schema/catalog for dbt are in dbt_profiles/profiles.yml.
21+
# The schema and catalog for dbt are configured in dbt_profiles/profiles.yml.
2322
targets:
2423
dev:
25-
# 'mode: development' creates a development copy:
26-
# - Deployed resources are prefixed with '[dev my_user_name]'.
27-
# - Job schedules/triggers are paused by default.
28-
# See https://docs.databricks.com/dev-tools/bundles/deployment-modes.html.
2924
mode: development
3025
default: true
31-
workspace:
32-
host: https://company.databricks.com
33-
3426
prod:
3527
mode: production
36-
workspace:
37-
host: https://company.databricks.com
38-
# Pin the deploy root so a single copy of the bundle lives in production.
39-
root_path: /Workspace/Users/user@company.com/.bundle/${bundle.name}/${bundle.target}
40-
permissions:
41-
- user_name: user@company.com
42-
level: CAN_MANAGE

knowledge_base/metric_view_dbt/dbt_profiles/profiles.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ metric_view_dbt:
2424
type: databricks
2525
method: http
2626
catalog: main
27-
schema: default
27+
schema: prod
2828

2929
http_path: /sql/1.0/warehouses/abcdef1234567890
3030

knowledge_base/metric_view_dbt/src/models/bookings_kpis.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
A Unity Catalog Metric View, materialized by dbt-databricks.
33

44
Everything below the `config(...)` line is the metric-view YAML body (see
5-
https://docs.databricks.com/aws/en/metric-views/yaml-ref). The metric_view
5+
https://docs.databricks.com/metric-views/yaml-ref). The metric_view
66
materialization wraps it in:
77

88
CREATE OR REPLACE VIEW <relation> WITH METRICS LANGUAGE YAML AS <yaml>
@@ -18,7 +18,7 @@
1818
MEASURE(total_bookings) AS bookings,
1919
MEASURE(total_revenue) AS revenue,
2020
MEASURE(avg_booking_value) AS aov
21-
FROM main.<your_schema>.bookings_kpis
21+
FROM <catalog>.<your_schema>.bookings_kpis
2222
WHERE check_in_date >= '2024-01-01'
2323
GROUP BY check_in_month
2424
ORDER BY check_in_month;

0 commit comments

Comments
 (0)