anfra docsAlpha
Semantic layerExisting semantic layers

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 sync
  • 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
FlagDescription
--data-sourceThe Databricks connection to create models from. Required.
--schemaOnly add from schemas whose names start with this prefix. Case-sensitive.
--tableOnly 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

orders_metric_view.model.aml
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:

Databricksanfra
Dimensiondimension
Measuremeasure wrapped in AGG(), computed by Databricks
commentdescription
display_namelabel, 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

CapabilitySupportDetails
Sync from DatabricksSupportedDimensions, measures, comments and display names, with anfra sl add and anfra sl update.
Write back to DatabricksNot supportedChanges in anfra are not written back to Databricks.
Relationship in datasetNot supportedDatabricks rejects joins on a metric view, so a synced model can't have relationships to other models.
SynonymsNot supportedSynonyms defined on the metric view are not synced.
Data formatNot supportedDisplay formats defined on the metric view are not synced.

On this page