【Airflow】TaskGroup使用实例

💡 A collection of closely related tasks on the same DAG that should be grouped together visually.

高度相关的任务成为一个任务组

文档简介

When set_downstream() or set_upstream() are called on the TaskGroup, it is applied across all tasks within the group if necessary.
设置上下游任务的时候可以设为任务组,有必要时会应用于组内所有的任务。

源码收获

参数

  • group_id:任务组名称,对应task_id
  • prefix_group_id:子任务id和子任务组id前是否会加上这个group_id,默认加上(True)
  • parent_group:当前任务组的上一级任务组。parent_group设置为None,则为根任务组。
  • dag:任务组所属的dag。
  • default_args
  • tooltip: TaskGroup节点在UI中显示的工具提示
  • ui_color: TaskGroup节点在UI中显示时的填充颜色
  • ui_fgcolor: TaskGroup节点在UI中显示时的标签颜色
  • add_suffix_on_collision:如果任务组名称已经存在,自动添加’ __1 '等后缀

使用实例

import datetime
from airflow import DAG
from airflow.decorators import task_group
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator
from airflow.operators.empty import EmptyOperator

with DAG(
    dag_id="test_task_group",
    tags=['test'],
    start_date=datetime.datetime(2024, 8, 1),
    schedule="@daily",
    default_args={"retries": 1},
):
    
    def print_log():
        print(f'task is running at {datetime.datetime.now()}.')

    @task_group(default_args={"retries": 3})
    def group1():
        """This docstring will become the tooltip for the TaskGroup."""
        task1 = PythonOperator(
            task_id="task1",
            python_callable=print_log
            )
        task2 = BashOperator(task_id="task2", bash_command="echo Hello World!", retries=2)
        print(task1.retries)  # 3
        print(task2.retries) 

    task3 = PythonOperator(
        task_id="task3",
        python_callable=print_log
        )

    group1() >> task3

任务组中的任务可以收起

总结

task group本质是将多个任务同时进行触发时,使用的。也就是一个节点需要有多个操作时,可以考虑使用task group。

参考链接

airflow.decorators.task_group — Airflow Documentation

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache Airflow中的SubDAG(子任务图)是一种强大的功能,它允许你在一个主DAG中组织和管理多个独立但相关的任务序列。SubDAGs可以帮助你保持代码结构清晰,便于维护,并且可以灵活地进行调度和扩展。 使用SubDAG的步骤如下: 1. **创建子任务图**: - 在Airflow DAG文件中,定义一个新的类,继承自`airflow.models.DAG`,并覆盖`__init__`方法,指定子任务的名称、描述、默认起始时间等信息。 ```python from airflow import DAG from airflow.operators.dummy_operator import DummyOperator class SubDAGExample(DAG): default_args = { 'owner': 'airflow', 'start_date': datetime(2023, 1, 1), } dag_id = 'subdag_example' sub_dag = SubDAGExample() ``` 2. **添加任务**: - 在子任务图类里,你可以像在常规DAG中一样添加各种任务,如`DummyOperator`、`PythonOperator`、`BranchPythonOperator`等。 ```python task_1 = DummyOperator(task_id='task_1', dag=sub_dag) task_2 = DummyOperator(task_id='task_2', dag=sub_dag) ``` 3. **连接任务**: - 如果需要,可以使用`sub_dag >> task_2`的方式将子任务链起来,或者使用`apply_pressure`函数将任务添加到特定的时间点。 4. **包含在主DAG中**: - 在主DAG中,调用子DAG实例,并将其添加到调度中,比如使用`DAG.add_subdag`或`DAG.schedule_on_demand`。 ```python main_dag = DAG('main_dag', default_args={...}) main_dag.add_subdag(sub_dag) ``` 5. **调度和运行**: - 配置主DAG的调度规则,然后运行`airflow dags run main_dag`命令启动整个流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值