Databricks Metric Views
Sync Databricks Metric Views into anfra to build apps without maintaining a duplicated definition.
Upcoming
This integration is in development and not available yet.
If your metrics already live in Databricks Metric Views, you don't need to redefine them in AML. anfra syncs their dimensions and measures into data models that keep only a pointer to each definition. When a query runs, anfra wraps each measure in AGG() and Databricks computes it from the definition it holds.
Metrics are defined once in Unity Catalog, and every anfra app, dashboard and agent answer uses them as-is.
How it works
Each Databricks Metric View syncs into anfra as a data model. At query time, anfra sends SQL with AGG() back to Databricks, which computes the measures.
- Databricks
- Metric view — Dimensions and measures defined in Unity Catalog
- anfra
- Model — Generated .model.aml: dimensions, and measures wrapped in AGG()
- Dataset — You add the model to a dataset to query it
- Apps & agents — Data apps, dashboards and agent answers built on the dataset
- metric-view → model (anfra sl pull)
- model → dataset (add to)
- dataset → data-apps (query)
- dataset → metric-view (SQL with AGG(), computed by Databricks)
Say you have this metric view in Databricks:
CREATE VIEW main.sales.orders_metric_view
WITH METRICS LANGUAGE YAML AS $$
version: 1.1
source: main.sales.orders
dimensions:
- name: order_month
expr: DATE_TRUNC('MONTH', source.order_date)
display_name: Order Month
comment: First day of the order month
measures:
- name: total_revenue
expr: SUM(source.order_value)
display_name: Total Revenue
comment: Gross revenue before discounts
$$;Step 1: Connect Databricks
Add a Databricks connection to .anfra/data_sources.yml and note its name.
Step 2: Add the models
anfra sl add --data-source <your-datasource-name> \
--schema sales \
--table orders| Flag | Description |
|---|---|
--data-source | The Databricks connection to create models from. Required. |
--schema | Only add from schemas whose names start with this prefix. Case-sensitive. |
--table | Only add objects whose names start with this prefix. Case-sensitive. |
Without --schema and --table, anfra adds every table, view and metric view in the connection's catalog, up to 1,000 objects. Objects past that limit are skipped without a warning, so use the prefixes on large catalogs.
sl add never overwrites a model. If a model for the same table already exists, it's skipped; use sl update instead.
Step 3: Review the generated AML models
Model orders_metric_view {
type: 'table'
data_source_name: 'databricks'
table_name: '`main`.`sales`.`orders_metric_view`'
dimension order_month {
label: 'Order Month'
type: 'date'
description: 'First day of the order month'
definition: @sql {{ #SOURCE.order_month }};;
}
measure total_revenue {
label: 'Total Revenue'
type: 'number'
description: 'Gross revenue before discounts'
definition: @sql AGG({{ #SOURCE.total_revenue }});;
}
}To query the model, add it to a dataset.
Here's what each part of the metric view mapped to in the generated model:
| Databricks | anfra |
|---|---|
| Dimension | dimension |
| Measure | measure wrapped in AGG(), computed by Databricks |
comment | description |
display_name | label, generated from the name when missing |
On top of the synced fields, you can define your own measures and metrics, such as a ratio of two Databricks measures.
Updating models
After the metric view changes, run:
anfra sl update --data-source <your-datasource-name>This updates every model. To update only some, name them: anfra sl update orders_metric_view --data-source <your-datasource-name>. It tries to keep the fields and edits you added. Review the diff before you commit.
Lineage
anfra sl add records each metric view in the Semantic Catalog as the upstream of its model. Trace a field with anfra lineage, or show it in a data app with Anfra.lineage():
anfra lineage --targets '[{"parent":"orders_metric_view","child":"total_revenue"}]'Lineage stops at the metric view. It doesn't trace the source tables behind it.
Capabilities
| Capability | Support | Details |
|---|---|---|
| Sync from Databricks | Supported | Dimensions, measures, comments and display names, with anfra sl add and anfra sl update. |
| Write back to Databricks | Not supported | Changes in anfra are not written back to Databricks. |
| Relationship in dataset | Not supported | Databricks rejects joins on a metric view, so a synced model can't have relationships to other models. |
| Synonyms | Not supported | Synonyms defined on the metric view are not synced. |
| Data format | Not supported | Display formats defined on the metric view are not synced. |