Apache Airflow integration for dbt
Project description
airflow-dbt
This is a collection of Airflow operators to provide easy integration with dbt.
from airflow import DAG
from airflow_dbt.operators.dbt_operator import (
DbtSeedOperator,
DbtSnapshotOperator,
DbtRunOperator,
DbtTestOperator
)
from airflow.utils.dates import days_ago
default_args = {
'dir': '/srv/app/dbt',
'start_date': days_ago(0)
}
with DAG(dag_id='dbt', default_args=default_args, schedule_interval='@daily') as dag:
dbt_seed = DbtSeedOperator(
task_id='dbt_seed',
)
dbt_snapshot = DbtSnapshotOperator(
task_id='dbt_snapshot',
)
dbt_run = DbtRunOperator(
task_id='dbt_run',
)
dbt_test = DbtTestOperator(
task_id='dbt_test',
retries=0, # Failing tests would fail the task, and we don't want Airflow to try again
)
dbt_seed >> dbt_snapshot >> dbt_run >> dbt_test
Installation
Install from PyPI:
pip install airflow-dbt
It will also need access to the dbt
CLI, which should either be on your PATH
or can be set with the dbt_bin
argument in each operator.
Usage
There are five operators currently implemented:
DbtDocsGenerateOperator
- Calls
dbt docs generate
- Calls
DbtDepsOperator
- Calls
dbt deps
- Calls
DbtSeedOperator
- Calls
dbt seed
- Calls
DbtSnapshotOperator
- Calls
dbt snapshot
- Calls
DbtRunOperator
- Calls
dbt run
- Calls
DbtTestOperator
- Calls
dbt test
- Calls
Each of the above operators accept the following arguments:
profiles_dir
- If set, passed as the
--profiles-dir
argument to thedbt
command
- If set, passed as the
target
- If set, passed as the
--target
argument to thedbt
command
- If set, passed as the
dir
- The directory to run the
dbt
command in
- The directory to run the
full_refresh
- If set to
True
, passes--full-refresh
- If set to
vars
- If set, passed as the
--vars
argument to thedbt
command. Should be set as a Python dictionary, as will be passed to thedbt
command as YAML
- If set, passed as the
models
- If set, passed as the
--models
argument to thedbt
command
- If set, passed as the
exclude
- If set, passed as the
--exclude
argument to thedbt
command
- If set, passed as the
select
- If set, passed as the
--select
argument to thedbt
command
- If set, passed as the
dbt_bin
- The
dbt
CLI. Defaults todbt
, so assumes it's on yourPATH
- The
verbose
- The operator will log verbosely to the Airflow logs
warn_error
- If set to
True
, passes--warn-error
argument todbt
command and will treat warnings as errors
- If set to
Typically you will want to use the DbtRunOperator
, followed by the DbtTestOperator
, as shown earlier.
You can also use the hook directly. Typically this can be used for when you need to combine the dbt
command with another task in the same operators, for example running dbt docs
and uploading the docs to somewhere they can be served from.
Building Locally
To install from the repository: First it's recommended to create a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
Install using pip
:
pip install .
Testing
To run tests locally, first create a virtual environment (see Building Locally section)
Install dependencies:
pip install . pytest
Run the tests:
pytest tests/
Code style
This project uses flake8.
To check your code, first create a virtual environment (see Building Locally section):
pip install flake8
flake8 airflow_dbt/ tests/ setup.py
Package management
If you use dbt's package manager you should include all dependencies before deploying your dbt project.
For Docker users, packages specified in packages.yml
should be included as part your docker image by calling dbt deps
in your Dockerfile
.
Amazon Managed Workflows for Apache Airflow (MWAA)
If you use MWAA, you just need to update the requirements.txt
file and add airflow-dbt
and dbt
to it.
Then you can have your dbt code inside a folder {DBT_FOLDER}
in the dags folder on S3 and configure the dbt task like below:
dbt_run = DbtRunOperator(
task_id='dbt_run',
dbt_bin='/usr/local/airflow/.local/bin/dbt',
profiles_dir='/usr/local/airflow/dags/{DBT_FOLDER}/',
dir='/usr/local/airflow/dags/{DBT_FOLDER}/'
)
License & Contributing
- This is available as open source under the terms of the MIT License.
- Bug reports and pull requests are welcome on GitHub at https://github.com/gocardless/airflow-dbt.
GoCardless ♥ open source. If you do too, come join us.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file airflow_dbt-0.4.0.tar.gz
.
File metadata
- Download URL: airflow_dbt-0.4.0.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d077a6d26e0571724f050673becf64a5e4c429c1d0ee3f90611e07e3f4469ec8 |
|
MD5 | 7d93c0f549884125ba8c2762ff883cff |
|
BLAKE2b-256 | 60c32519922ca9550170975299ff0c918421dcf382ae60181750b96226a1f098 |
File details
Details for the file airflow_dbt-0.4.0-py2.py3-none-any.whl
.
File metadata
- Download URL: airflow_dbt-0.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a662f3bd5db13d35d326b7ce16b5947ad0f7d54b233ba75544898dce06076d36 |
|
MD5 | e16fb29dd6f3ba9d402e6909203ad646 |
|
BLAKE2b-256 | 6985010b1fab4e5f2176f48aaebf5975625f8dc1b80923bc66e728c075f4c48d |