Skip to content

Instantly share code, notes, and snippets.

@TimmyCarbone
Last active January 28, 2021 09:15
Show Gist options
  • Save TimmyCarbone/958aaa08fbe6281f4bf4d98fd6fce1de to your computer and use it in GitHub Desktop.
Save TimmyCarbone/958aaa08fbe6281f4bf4d98fd6fce1de to your computer and use it in GitHub Desktop.
Example of dbt scheduling with Airflow
# How to schedule dbt runs with Airflow using gocardless/airflow-dbt
# Folder structure is as such:
#
# /app
# /airflow
# /dags
# /dbt_dag.py
# /dbt
# /config
# /profiles.yml
# /models
# /web
# /traffic.py
from airflow import DAG
from airflow_dbt.operators.dbt_operator import DbtRunOperator
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': '2020-01-01',
'email': ['email@example.com'],
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
'dbt_dir': dbt_path
}
dbt_path = dbt_path = os.environ['DBT_PATH'] # /app/dbt
# Use a cron schedule in `schedule_interval`, here it will run every 20 minutes
dag = DAG(dag_id='update_models', default_args=default_args, schedule_interval='*/20 * * * *', catchup=False)
# DbtRunOperator from gocardless/airflow-dbt
# This will run the /app/dbt/models/web/traffic.py dbt model
dbt_run = DbtRunOperator(
task_id='dbt_update_models',
profiles_dir=dbt_path + '/config', # The folder where is profiles.yml (/app/dbt/config)
dir=dbt_path,
queue='airflow_queue_1',
models='web.traffic',
dag=dag
)
# Airflow dependency graph (with a single task here)
dbt_run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment