anfra docsAlpha
Semantic layerExisting semantic layers

dbt

Generate anfra data models from the models in your dbt project, with their columns, descriptions and tests.

Upcoming

This integration is in development and not available yet.

If your warehouse tables are built and documented with dbt, you don't need to rewrite that work in AML. anfra reads your dbt project's manifest and generates a data model for each dbt model: its table, its columns as dimensions, and the descriptions you wrote in dbt.

dbt stays the source of truth for how tables are built and documented. anfra queries the tables dbt builds, and you add the measures and metrics on top.

How it works

anfra reads target/manifest.json, the file dbt writes when it compiles your project. Each dbt model becomes an AML model that points at the table or view dbt built. When a query runs, anfra compiles it to SQL on that table, like any other model.

Step 1: Add the dbt project as a context source

List the dbt project in .anfra/context_sources.yml, with the connection it builds into as data_source_name:

.anfra/context_sources.yml
sources:
  - name: dbt_main
    type: dbt
    local: { path: ../analytics-dbt }
    data_source_name: warehouse

Make sure that the dbt folder contains a target/manifest.json file.

Step 2: Add the models

anfra sl add --dbt-source dbt_main --model orders --model customers
FlagDescription
--dbt-sourceThe dbt source to create models from. Required.
--modelOnly add dbt models whose name starts with this prefix. Case-sensitive. Repeat it to add several prefixes.

Without --model, anfra adds every model in the dbt project.

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

models/orders.model.aml
Model orders {
  type: 'table'
  label: 'Orders'
  description: 'One row per order.'
  data_source_name: 'warehouse'
  table_name: '"analytics"."marts"."orders"'

  dimension order_id {
    label: 'Order Id'
    type: 'number'
    description: 'Primary key.'
    primary_key: true
    definition: @sql {{ #SOURCE.order_id }};;
  }

  dimension customer_id {
    label: 'Customer Id'
    type: 'number'
    definition: @sql {{ #SOURCE.customer_id }};;
  }

  dimension ordered_at {
    label: 'Ordered At'
    type: 'datetime'
    description: 'When the customer placed the order.'
    definition: @sql {{ #SOURCE.ordered_at }};;
  }

  dimension order_total {
    label: 'Order Total'
    type: 'number'
    description: 'Order value after discounts, in USD.'
    definition: @sql {{ #SOURCE.order_total }};;
  }
}

Here's what each part of the dbt project mapped to:

dbtanfra
ModelModel, with table_name from the model's relation
Columndimension
descriptiondescription
Column type, from catalog.jsonDimension type. Without the catalog, anfra reads the types from the warehouse.
unique and not_null tests on one columnprimary_key: true

dbt models have no measures, so add your own measures and metrics, such as sum(orders.order_total).

Updating models

After your dbt project changes, rebuild the manifest and run:

anfra sl update --dbt-source dbt_main

This updates every model. To update only some, name them: anfra sl update orders customers --dbt-source dbt_main. It tries to keep the measures and edits you added. Review the diff before you commit.

Capabilities

CapabilitySupportDetails
Add and update dbt modelsSupportedTables, columns, descriptions and column types, with anfra sl add and anfra sl update.
Primary keysSupportedFrom unique and not_null tests.
Write back to dbtNot supportedChanges in anfra are not written back to your dbt project.
dbt Semantic Layer (MetricFlow)Not supportedSemantic models and metrics in the manifest are not added.
dbt CloudNot supportedThe project must be on disk. anfra doesn't fetch the manifest from dbt Cloud.

On this page