Cloud Composer 1 | Cloud Composer 2 | Cloud Composer 3
This page describes how you can group tasks in your Airflow pipelines using the following design patterns:
- Grouping tasks in the DAG graph.
- Triggering children DAGs from a parent DAG.
- Grouping tasks with the
TaskGroup
operator (only with Airflow 2).
Grouping tasks in the DAG graph
To group tasks in certain phases of your pipeline, you can use relationships between the tasks in your DAG file.
Consider the following example:
In this workflow, tasks op-1
and op-2
run together after the initial
task start
.
You can achieve this by grouping tasks together with the statement
start >> [task_1, task_2]
.
The following code provides a complete implementation of the DAG above:
Airflow 2
Airflow 1
Triggering children DAGs from a parent DAG
You can trigger one DAG from another DAG with the
TriggerDagRunOperator
.
Depending on your Airflow version, you can find
the TriggerDagRunOperator
operator in a different module:
- For Airflow 1.10.*, use the
airflow.operators.dagrun_operator
module. - For Airflow 2, use the
airflow.operators.trigger_dagrun
module.
Consider the following example:
In this workflow, the blocks dag_1
and dag_2
represent a series of tasks
that are grouped together in a separate DAG in the Cloud Composer
environment.
The implementation of this workflow requires two separate DAG files. The controlling DAG file looks like the following:
Airflow 2
Airflow 1
The implementation of the child DAG, which is triggered by the controlling DAG, looks like the following:
Airflow 2
Airflow 1
You must upload both DAG files in your Cloud Composer environment for the workflow to work.
Grouping tasks with the TaskGroup
operator
In Airflow 2, you can use the
TaskGroup
operator
to group tasks together in your DAG.
Tasks defined within a TaskGroup
block are still part of the main DAG.
Consider the following example:
The tasks op-1
and op-2
are grouped together in a block with ID
taskgroup_1
.
An implementation of this workflow looks like the following code: