-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save tasks map as DbtToAirflowConverter property (#1362)
Currently, if you want to modify a DAG after it has been rendered, you have to walk through the dag.dbt_graph, then puzzle the task IDs and task group IDs together by reverse-engineering your task rendering strategy. This is cumbersome and error-prone, hence it makes sense to expose the mapping from DbtNode to Airflow Task ID as a DAG property. This allows you to walk the DBT graph while directly accessing any corresponding Airflow tasks, which makes e.g. adding Airflow sensors upstream of all source tasks much easier. Co-authored-by: hheemskerk <[email protected]>
- Loading branch information
1 parent
82d476b
commit 6d4a239
Showing
6 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
An example DAG that demonstrates how to walk over the dbt graph. It also shows how to use the mapping from | ||
{dbt graph unique_id} -> {Airflow tasks/task groups}. | ||
""" | ||
|
||
import os | ||
from datetime import datetime | ||
from pathlib import Path | ||
|
||
from airflow.operators.empty import EmptyOperator | ||
|
||
from cosmos import DbtDag, DbtResourceType, ProfileConfig, ProjectConfig | ||
from cosmos.profiles import PostgresUserPasswordProfileMapping | ||
|
||
DEFAULT_DBT_ROOT_PATH = Path(__file__).parent / "dbt" | ||
DBT_ROOT_PATH = Path(os.getenv("DBT_ROOT_PATH", DEFAULT_DBT_ROOT_PATH)) | ||
|
||
profile_config = ProfileConfig( | ||
profile_name="default", | ||
target_name="dev", | ||
profile_mapping=PostgresUserPasswordProfileMapping( | ||
conn_id="example_conn", | ||
profile_args={"schema": "public"}, | ||
disable_event_tracking=True, | ||
), | ||
) | ||
|
||
# [START example_tasks_map] | ||
with DbtDag( | ||
# dbt/cosmos-specific parameters | ||
project_config=ProjectConfig( | ||
DBT_ROOT_PATH / "jaffle_shop", | ||
), | ||
profile_config=profile_config, | ||
operator_args={ | ||
"install_deps": True, # install any necessary dependencies before running any dbt command | ||
"full_refresh": True, # used only in dbt commands that support this flag | ||
}, | ||
# normal dag parameters | ||
schedule_interval="@daily", | ||
start_date=datetime(2023, 1, 1), | ||
catchup=False, | ||
dag_id="customized_cosmos_dag", | ||
default_args={"retries": 2}, | ||
) as dag: | ||
# Walk the dbt graph | ||
for unique_id, dbt_node in dag.dbt_graph.filtered_nodes.items(): | ||
# Filter by any dbt_node property you prefer. In this case, we are adding upstream tasks to source nodes. | ||
if dbt_node.resource_type == DbtResourceType.SOURCE: | ||
# Look up the corresponding Airflow task or task group in the DbtToAirflowConverter.tasks_map property. | ||
task = dag.tasks_map[unique_id] | ||
# Create a task upstream of this Airflow source task/task group. | ||
upstream_task = EmptyOperator(task_id=f"upstream_of_{unique_id}") | ||
upstream_task >> task | ||
# [END example_tasks_map] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.. _dag_customization: | ||
|
||
Post-rendering DAG customization | ||
================ | ||
|
||
.. note:: | ||
The DbtToAirflowConverter.tasks_map property is only available for cosmos >= 1.8.0 | ||
|
||
After Cosmos has rendered an Airflow DAG from a dbt project, you may want to add some extra Airflow tasks that interact | ||
with the tasks created by Cosmos. This document explains how to do this. | ||
|
||
An example use case you can think of is implementing sensor tasks that wait for an external DAG task to complete before | ||
running a source node task (or task group, if the source contains a test). | ||
|
||
Mapping from dbt nodes to Airflow tasks | ||
---------------------- | ||
|
||
To interact with Airflow tasks created by Cosmos, | ||
you can iterate over the dag.dbt_graph.filtered_nodes property like so: | ||
|
||
.. | ||
This is an abbreviated copy of example_tasks_map.py, as GitHub does not render literalinclude blocks | ||
.. code-block:: python | ||
with DbtDag( | ||
dag_id="customized_cosmos_dag", | ||
# Other arguments omitted for brevity | ||
) as dag: | ||
# Walk the dbt graph | ||
for unique_id, dbt_node in dag.dbt_graph.filtered_nodes.items(): | ||
# Filter by any dbt_node property you prefer. In this case, we are adding upstream tasks to source nodes. | ||
if dbt_node.resource_type == DbtResourceType.SOURCE: | ||
# Look up the corresponding Airflow task or task group in the DbtToAirflowConverter.tasks_map property. | ||
task = dag.tasks_map[unique_id] | ||
# Create a task upstream of this Airflow source task/task group. | ||
upstream_task = EmptyOperator(task_id=f"upstream_of_{unique_id}") | ||
upstream_task >> task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters