Hierarchical models in dbt

Introduction to dbt

Mike Metzger

Data Engineer

What is a hierarchy in dbt?

  • Represents the dependencies within a dbt project
  • Also known as a directed acyclic graph (DAG), or lineage graph
    • Note this being specific to dbt, rather than general DAG
  • Allows models to be built / updated according to dependencies

dbt DAG / Lineage graph

Introduction to dbt

Hierarchy details

  • avg_fare_per_day and total_creditcard_riders_per_day depend on taxi_rides_raw
  • dbt builds taxi_rides_raw first
  • dbt creates entries alphabetically without a lineage graph
    • avg_fare_per_day would fail without taxi_rides_raw

dbt DAG / Lineage graph

Introduction to dbt

How are hierarchies defined?

  • Built using the Jinja template language in the model file(s)
  • Most often using the ref function
  • Replace table name with {{ ref('model_name') }} in SQL
  • (Re-)Materialize table with dbt run
  • ref templates are substituted with actual table names
Introduction to dbt

Hierarchy example

SELECT 
  first_name, last_name
FROM taxi_rides_raw
SELECT 
  first_name, last_name
FROM {{ ref('taxi_rides_raw') }}
Introduction to dbt

Let's practice!

Introduction to dbt

Preparing Video For Download...