0
0
dbtdata~10 mins

Orchestrating dbt with Airflow - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Orchestrating dbt with Airflow
Start Airflow DAG
Trigger dbt run task
dbt runs models
Check dbt run success?
NoHandle failure
Yes
Trigger dbt test task
dbt runs tests
Check dbt test success?
NoHandle failure
Yes
DAG completes successfully
Airflow starts a DAG that runs dbt models, checks success, then runs dbt tests, handling failures if any.
Execution Sample
dbt
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

default_args = {'start_date': datetime(2024, 1, 1)}

dag = DAG('dbt_airflow', default_args=default_args, schedule_interval='@daily')

dbt_run = BashOperator(task_id='dbt_run', bash_command='dbt run', dag=dag)
dbt_test = BashOperator(task_id='dbt_test', bash_command='dbt test', dag=dag)
dbt_run >> dbt_test
This Airflow DAG runs dbt models first, then runs dbt tests after the models finish.
Execution Table
StepTaskActionResultNext Step
1Start DAGTrigger DAG runDAG startedRun dbt_run task
2dbt_runExecute 'dbt run'Models built successfullyRun dbt_test task
3dbt_testExecute 'dbt test'Tests passedDAG completes successfully
4DAG CompletionAll tasks doneSuccessEnd
💡 DAG ends after dbt tests pass successfully
Variable Tracker
VariableStartAfter dbt_runAfter dbt_testFinal
DAG StatusNot starteddbt_run successdbt_test successSuccess
dbt_run StatusNot startedSuccessN/ASuccess
dbt_test StatusNot startedN/ASuccessSuccess
Key Moments - 2 Insights
Why does dbt_test run only after dbt_run finishes?
Because in the execution_table, step 2 shows dbt_run completes before step 3 starts dbt_test, ensuring models exist before testing.
What happens if dbt_run fails?
The DAG stops and handles failure as shown by the 'Check dbt run success?' decision in the concept_flow, so dbt_test does not run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the dbt_run task at step 2?
ADAG started
BTests passed
CModels built successfully
DFailure
💡 Hint
Check the 'Result' column for step 2 in the execution_table.
At which step does the DAG complete successfully?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Task' and 'Result' columns in the execution_table for the final step.
If dbt_run fails, what happens to dbt_test?
Adbt_test is skipped
BDAG restarts from beginning
Cdbt_test runs anyway
Ddbt_test runs before dbt_run
💡 Hint
Refer to the concept_flow where failure after dbt_run leads to handling failure and no dbt_test run.
Concept Snapshot
Airflow DAG runs dbt tasks in order:
1. dbt run builds models
2. dbt test runs tests
Tasks run sequentially
Failures stop the DAG
Use BashOperator to run dbt commands
Full Transcript
This visual execution shows how Airflow orchestrates dbt workflows. The DAG starts and triggers the dbt run task to build models. After successful completion, it triggers the dbt test task to run tests on those models. If any task fails, the DAG stops and handles the failure. Variables track the status of each task and the overall DAG. This ensures dbt models are built before tests run, maintaining data pipeline integrity.