Skip to content

Commit 10c7576

Browse files
committed
docs: Update README to reference documentation
1 parent da149fe commit 10c7576

File tree

1 file changed

+82
-98
lines changed

1 file changed

+82
-98
lines changed

README.md

Lines changed: 82 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,116 @@
55
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
66
[![Test coverage](https://codecov.io/gh/tomasfarias/airflow-dbt-python/branch/master/graph/badge.svg?token=HBKZ78F11F)](https://codecov.io/gh/tomasfarias/airflow-dbt-python)
77

8-
An [Airflow](https://airflow.apache.org/) operator and hook to interface with the [`dbt-core`](https://pypi.org/project/dbt-core/) Python package.
8+
A collection of [Airflow](https://airflow.apache.org/) operators and hooks to interface with [`dbt`](https://pypi.org/project/dbt-core/).
99

10-
# Motivation
10+
Read the [documentation](https://tomasfarias.github.io/airflow-dbt-python/) for examples, installation instructions, and a full reference.
1111

12-
## Airflow running in a managed environment
12+
# Installing
1313

14-
Although [`dbt`](https://docs.getdbt.com/) is meant to be installed and used as a CLI, we may not have control of the environment where Airflow is running, disallowing us the option of using `dbt` as a CLI.
14+
## Requirements
1515

16-
This is exactly what happens when using [Amazon's Managed Workflows for Apache Airflow](https://aws.amazon.com/managed-workflows-for-apache-airflow/) or MWAA: although a list of Python requirements can be passed, the CLI cannot be found in the worker's PATH.
16+
airflow-dbt-python requires the latest major version of [`dbt-core`](https://pypi.org/project/dbt-core/) which at the time of writing is version 1. Since `dbt-core` follows [semantic versioning](https://semver.org/), we do not impose any restrictions on the minor and patch versions, but do keep in mind that the latest `dbt-core` features incorporated as minor releases may not yet be supported.
1717

18-
There is a workaround which involves using Airflow's `BashOperator` and running Python from the command line:
18+
To line up with `dbt-core`, airflow-dbt-python supports Python 3.7, 3.8, and 3.9. We also include Python 3.10 in our testing pipeline, although as of the time of writing `dbt-core` does not yet support it.
1919

20-
``` python
21-
from airflow.operators.bash import BashOperator
20+
Due to the dependency conflict, airflow-dbt-python **does not include Airflow as a dependency**. We expect airflow-dbt-python to be installed into an environment with Airflow already in it. For more detailed instructions see the [docs](https://tomasfarias.github.io/airflow-dbt-python/getting_started.html).
2221

23-
BASH_COMMAND = "python -c 'from dbt.main import main; main()' run"
24-
operator = BashOperator(
25-
task_id="dbt_run",
26-
bash_command=BASH_COMMAND,
27-
)
22+
## From PyPI:
23+
24+
``` shell
25+
pip install airflow-dbt-python
2826
```
2927

30-
But it can get sloppy when appending all potential arguments a `dbt run` command (or other subcommand) can take.
28+
Any `dbt` adapters you require may be installed by specifying extras:
3129

32-
That's where `airflow-dbt-python` comes in: it abstracts the complexity of interfacing with `dbt-core` and exposes one operator for each `dbt` subcommand that can be instantiated with all the corresponding arguments that the `dbt` CLI would take.
30+
``` shell
31+
pip install airflow-dby-python[snowflake,postgres]
32+
```
3333

34-
## An alternative to `airflow-dbt` that works without the dbt CLI
34+
## From this repo:
3535

36-
The alternative [`airflow-dbt`](https://pypi.org/project/airflow-dbt/) package, by default, would not work if the `dbt` CLI is not in PATH, which means it would not be usable in MWAA. There is a workaround via the `dbt_bin` argument, which can be set to `"python -c 'from dbt.main import main; main()' run"`, in similar fashion as the `BashOperator` example. Yet this approach is not without its limitations:
37-
* `airflow-dbt` works by wrapping the `dbt` CLI, which makes our code dependent on the environment in which it runs.
38-
* `airflow-dbt` does not support the full range of arguments a command can take. For example, `DbtRunOperator` does not have an attribute for `fail_fast`.
39-
* `airflow-dbt` does not offer access to `dbt` artifacts created during execution. `airflow-dbt-python` does so by pushing any artifacts to [XCom](https://airflow.apache.org/docs/apache-airflow/stable/concepts/xcoms.html).
36+
Clone the repo:
37+
``` shell
38+
git clone https://github.com/tomasfarias/airflow-dbt-python.git
39+
cd airflow-dbt-python
40+
```
41+
42+
With poetry:
43+
``` shell
44+
poetry install
45+
```
46+
47+
Install any extras you need, and only those you need:
48+
``` shell
49+
poetry install -E postgres -E redshift
50+
```
51+
52+
## In MWAA:
53+
54+
Add `airflow-dbt-python` to your `requirements.txt` file and edit your Airflow environment to use this new `requirements.txt` file.
4055

41-
# Additional features
56+
# Features
4257

43-
Besides running `dbt` as one would do if doing so manually, `airflow-dbt-python` also supports a few additional features to bring `dbt` closer to being a first-class citizen of Airflow.
58+
Airflow-dbt-python aims to make dbt a **first-class citizen** of Airflow by supporting additional features that integrate both tools. As you would expect, airflow-dbt-python can run all your dbt workflows in Airflow with the same interface you are used to from the CLI, but without being a mere wrapper: airflow-dbt-python directly interfaces with internal `dbt-core <https://pypi.org/project/dbt-core/>`_ classes, bridging the gap between them and Airflow's operator interface.
4459

45-
## Download dbt projects from S3
60+
As this integration was completed, several features were developed to **extend the capabilities of `dbt`** to leverage Airflow as much as possible. Can you think of a way `dbt` could leverage Airflow that is not currently supported? Let us know in a [GitHub issue](https://github.com/tomasfarias/airflow-dbt-python/issues/new/choose)! The current list of supported features is as follows:
4661

47-
The arguments `profiles_dir` and `project_dir` would normally point to a directory containing a `profiles.yml` file and a dbt project in the local environment respectively. `airflow-dbt-python` extends these arguments to also take an [AWS S3](https://aws.amazon.com/s3/) URL (identified by an `s3://` scheme):
62+
## Independent task execution
63+
64+
Airflow executes [Tasks](https://airflow.apache.org/docs/apache-airflow/stable/concepts/tasks.html) independent of one another: even though downstream and upstream dependencies between tasks exist, the execution of an individual task happens entirely independently of any other task execution (see: [Tasks Relationships](https://airflow.apache.org/docs/apache-airflow/stable/concepts/tasks.html#relationships).
65+
66+
In order to work with this constraint, airflow-dbt-python runs each dbt command in a **temporary and isolated directory**. Before execution, all the relevant dbt files are copied from supported backends, and after executing the command any artifacts are exported. This ensures dbt can work with any Airflow deployment, including most production deployments as they are usually running [Remote Executors](https://airflow.apache.org/docs/apache-airflow/stable/executor/index.html#executor-types) and do not guarantee any files will be shared by default between tasks, since each task may run in a completely different environment.
67+
68+
69+
## Download dbt files from S3
70+
71+
The dbt parameters `profiles_dir` and `project_dir` would normally point to a directory containing a `profiles.yml` file and a dbt project in the local environment respectively (defined by the presence of a `dbt_project.yml` file). airflow-dbt-python extends these parameters to also accept an [AWS S3](https://aws.amazon.com/s3/) URL (identified by a `s3://` scheme):
4872

4973
* If an S3 URL is used for `profiles_dir`, then this URL must point to a directory in S3 that contains a `profiles.yml` file. The `profiles.yml` file will be downloaded and made available for the operator to use when running.
50-
* If an S3 URL is used for `project_dir`, then this URL must point to a directory in S3 containing all the files required for a `dbt` project to run. All of the contents of this directory will be downloaded and made available for the operator. The URL may also point to a zip file containing all the files of a `dbt` project, which will be downloaded, uncompressed, and made available for the operator.
74+
* If an S3 URL is used for `project_dir`, then this URL must point to a directory in S3 containing all the files required for a dbt project to run. All of the contents of this directory will be downloaded and made available for the operator. The URL may also point to a zip file containing all the files of a dbt project, which will be downloaded, uncompressed, and made available for the operator.
5175

5276
This feature is intended to work in line with Airflow's [description of the task concept](https://airflow.apache.org/docs/apache-airflow/stable/concepts/tasks.html#relationships):
5377

5478
> Tasks don’t pass information to each other by default, and run entirely independently.
5579
56-
In our world, that means task should be responsible of fetching all the `dbt` related files it needs in order to run independently. This is particularly relevant for an Airflow deployment with a [remote executor](https://airflow.apache.org/docs/apache-airflow/stable/executor/index.html#executor-types), as Airflow does not guarantee which worker will run a particular task.
80+
In our world, that means task should be responsible of fetching all the dbt related files it needs in order to run independently, as already described in [Independent Task Execution](#independent-task-execution).
81+
82+
As of the time of writing S3 is the only supported backend for dbt projects, but we have plans to extend this to support more backends, initially targeting other file storages that are commonly used in Airflow connections.
5783

5884
## Push dbt artifacts to XCom
5985

60-
Each `dbt` execution produces several JSON [artifacts](https://docs.getdbt.com/reference/artifacts/dbt-artifacts/) that may be valuable to obtain metrics, build conditional workflows, for reporting purposes, or other uses. `airflow-dbt-python` can push these artifacts to XCom as requested by exposing a `do_xcom_push_artifacts` argument, which takes a list of artifacts to push. This way, artifacts may be pulled and operated on by downstream tasks. For example:
86+
Each dbt execution produces one or more [JSON artifacts](https://docs.getdbt.com/reference/artifacts/dbt-artifacts/) that are valuable to produce meta-metrics, build conditional workflows, for reporting purposes, and other uses. airflow-dbt-python can push these artifacts to [XCom](https://airflow.apache.org/docs/apache-airflow/stable/concepts/xcoms.html) as requested via the `do_xcom_push_artifacts` parameter, which takes a list of artifacts to push.
87+
88+
# Motivation
89+
90+
## Airflow running in a managed environment
91+
92+
Although [`dbt`](https://docs.getdbt.com/) is meant to be installed and used as a CLI, we may not have control of the environment where Airflow is running, disallowing us the option of using `dbt` as a CLI.
93+
94+
This is exactly what happens when using [Amazon's Managed Workflows for Apache Airflow](https://aws.amazon.com/managed-workflows-for-apache-airflow/) or MWAA: although a list of Python requirements can be passed, the CLI cannot be found in the worker's PATH.
95+
96+
There is a workaround which involves using Airflow's `BashOperator` and running Python from the command line:
6197

6298
``` python
63-
with DAG(
64-
dag_id="example_dbt_artifacts",
65-
schedule_interval="0 0 * * *",
66-
start_date=days_ago(1),
67-
catchup=False,
68-
dagrun_timeout=dt.timedelta(minutes=60),
69-
) as dag:
70-
dbt_run = DbtRunOperator(
71-
task_id="dbt_run_daily",
72-
project_dir="/path/to/my/dbt/project/",
73-
profiles_dir="~/.dbt/",
74-
select=["+tag:daily"],
75-
exclude=["tag:deprecated"],
76-
target="production",
77-
profile="my-project",
78-
full_refresh=True,See the full example [here](examples/use_dbt_artifacts_dag.py).
79-
80-
do_xcom_push_artifacts=["manifest.json", "run_results.json"],
81-
)
99+
from airflow.operators.bash import BashOperator
82100

83-
process_artifacts = PythonOperator(
84-
task_id="process_artifacts",
85-
python_callable=process_dbt_artifacts,
86-
provide_context=True,
87-
)
88-
dbt_run >> process_artifacts
101+
BASH_COMMAND = "python -c 'from dbt.main import main; main()' run"
102+
operator = BashOperator(
103+
task_id="dbt_run",
104+
bash_command=BASH_COMMAND,
105+
)
89106
```
90107

91-
See the full example [here](examples/use_dbt_artifacts_dag.py).
108+
But it can get sloppy when appending all potential arguments a `dbt run` command (or other subcommand) can take.
109+
110+
That's where `airflow-dbt-python` comes in: it abstracts the complexity of interfacing with `dbt-core` and exposes one operator for each `dbt` subcommand that can be instantiated with all the corresponding arguments that the `dbt` CLI would take.
111+
112+
## An alternative to `airflow-dbt` that works without the dbt CLI
113+
114+
The alternative [`airflow-dbt`](https://pypi.org/project/airflow-dbt/) package, by default, would not work if the `dbt` CLI is not in PATH, which means it would not be usable in MWAA. There is a workaround via the `dbt_bin` argument, which can be set to `"python -c 'from dbt.main import main; main()' run"`, in similar fashion as the `BashOperator` example. Yet this approach is not without its limitations:
115+
* `airflow-dbt` works by wrapping the `dbt` CLI, which makes our code dependent on the environment in which it runs.
116+
* `airflow-dbt` does not support the full range of arguments a command can take. For example, `DbtRunOperator` does not have an attribute for `fail_fast`.
117+
* `airflow-dbt` does not offer access to `dbt` artifacts created during execution. `airflow-dbt-python` does so by pushing any artifacts to [XCom](https://airflow.apache.org/docs/apache-airflow/stable/concepts/xcoms.html).
92118

93119
# Usage
94120

@@ -154,51 +180,7 @@ with DAG(
154180
dbt_test >> dbt_seed >> dbt_run
155181
```
156182

157-
More examples can be found in the [`examples/`](examples/) directory.
158-
159-
# Requirements
160-
161-
To line up with `dbt-core`, `airflow-dbt-python` supports Python 3.7, 3.8, and 3.9. We also include Python 3.10 in our testing pipeline, although `dbt-core` does not yet support it.
162-
163-
On the Airflow side, we unit test with version 1.10.12 and the latest version 2 release.
164-
165-
Finally, `airflow-dbt-python` requires at least `dbt-core` version 1.0.0. Since `dbt-core` follows [semantic versioning](https://semver.org/), we do not impose any restrictions on the minor and patch versions, but do keep in mind that the latest `dbt-core` features incorporated as minor releases may not yet be supported.
166-
167-
# Installing
168-
169-
## From PyPI:
170-
171-
``` shell
172-
pip install airflow-dbt-python
173-
```
174-
175-
Any `dbt` adapters you require may be installed by specifying extras:
176-
177-
``` shell
178-
pip install airflow-dby-python[snowflake,postgres]
179-
```
180-
181-
## From this repo:
182-
183-
Clone the repo:
184-
``` shell
185-
git clone https://github.com/tomasfarias/airflow-dbt-python.git
186-
cd airflow-dbt-python
187-
```
188-
189-
With poetry:
190-
``` shell
191-
poetry install
192-
```
193-
194-
Install any extras you need, and only those you need:
195-
``` shell
196-
poetry install -E postgres -E redshift
197-
```
198-
199-
## In MWAA:
200-
201-
Add `airflow-dbt-python` to your `requirements.txt` file and edit your Airflow environment to use this new `requirements.txt` file.
183+
More examples can be found in the [`examples/`](examples/) directory and the [documentation](https://tomasfarias.github.io/airflow-dbt-python/example_dags.html).
202184

203185
# Testing
204186

@@ -208,6 +190,8 @@ Tests are written using `pytest`, can be located in `tests/`, and they can be ru
208190
poetry run pytest tests/ -vv
209191
```
210192

193+
See development and testing instructions in the [documentation](https://tomasfarias.github.io/airflow-dbt-python/development.html).
194+
211195
# License
212196

213197
This project is licensed under the MIT license. See ![LICENSE](LICENSE).

0 commit comments

Comments
 (0)