We use Airflow to run dbt tasks automatically and in order. This helps keep data fresh and organized without doing it by hand.
You want to update your data models every day without manual work.
You need to run tests on your data after it changes.
You want to run multiple dbt commands in a specific order.
You want to get alerts if something goes wrong in your data pipeline.
You want to combine dbt with other data tasks like loading or cleaning.
Syntax
dbt
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime
with DAG('dbt_dag', start_date=datetime(2024, 1, 1), schedule_interval='@daily') as dag:
run_dbt = BashOperator(
task_id='run_dbt',
bash_command='dbt run --profiles-dir /path/to/profiles'
)
Use BashOperator to run dbt commands as shell commands.
Set schedule_interval to control how often dbt runs.
C. set_upstream expects a task object, not a string
D. BashOperator cannot run dbt commands
Solution
Step 1: Check set_upstream usage
set_upstream expects another task object, not a string task id.
Step 2: Identify correct usage
It should be dbt_test.set_upstream(dbt_run) where dbt_run is the task object, not 'dbt_run' string.
Final Answer:
set_upstream expects a task object, not a string -> Option C
Quick Check:
set_upstream needs task object = B [OK]
Hint: set_upstream needs task object, not string name [OK]
Common Mistakes:
Passing string instead of task object to set_upstream
Thinking bash_command must be a list
Believing task_id has naming restrictions
Assuming BashOperator can't run dbt
5. You want to create an Airflow DAG that runs dbt run, then dbt test, and finally sends an email notification only if tests pass. Which sequence correctly sets task dependencies?
hard
A. dbt_run >> email_notify >> dbt_test
B. email_notify >> dbt_run >> dbt_test
C. dbt_test >> dbt_run >> email_notify
D. dbt_run >> dbt_test >> email_notify
Solution
Step 1: Define correct task order
First, run models with dbt run, then test them with dbt test, and finally notify if tests pass.
Step 2: Set dependencies accordingly
Use '>>' to chain tasks in order: dbt_run >> dbt_test >> email_notify.
Final Answer:
dbt_run >> dbt_test >> email_notify -> Option D
Quick Check:
Run then test then notify = D [OK]
Hint: Chain tasks in order with >>: run, test, then notify [OK]
Common Mistakes:
Placing email notification before tests
Reversing run and test order
Skipping dependencies causing parallel runs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.