gpt4 book ai didi

Airflow:如何扩展SubDagOperator?

转载 作者:行者123 更新时间:2023-12-02 03:10:21 27 4
gpt4 key购买 nike

当我尝试扩展 Airflow API 中提供的 SubDagOperator 时, Airflow 网络服务器 GUI 无法将其识别为 SubDagOperator,从而使我无法放大 subdag。

如何扩展 SubDagOperator,同时保留将其放大为 subdag 的能力?我错过了什么吗?

最佳答案

请参阅下面的示例,了解如何扩展 SubDagOperator。您的案例的关键是覆盖 task_type 函数

from airflow import DAG
from airflow.operators.subdag_operator import SubDagOperator
from airflow.operators.dummy_operator import DummyOperator
from airflow.utils.decorators import apply_defaults


class ExampleSubdagSubclassOperator(SubDagOperator):
template_fields = ()
template_ext = ()

@apply_defaults
def __init__(self, *args, **kwargs):

dag = kwargs.get('dag')
task_id = kwargs.get('task_id')

subdag = DAG(
'{}.{}'.format(dag.dag_id, task_id),
schedule_interval=dag.schedule_interval,
start_date=dag.start_date
)

# Replace the following 3 lines with code to automatically generate the desired tasks in the subdag
t1 = DummyOperator(dag=subdag, task_id='t1')
t2 = DummyOperator(dag=subdag, task_id='t2')
t3 = DummyOperator(dag=subdag, task_id='t3')

super(ExampleSubdagSubclassOperator, self).__init__(subdag=subdag, *args, **kwargs)

# This property needs to be overridden so that the airflow UI recognises the task as a subdag and enables
# the "Zoom into Sub Dag" button
@property
def task_type(self):
return 'SubDagOperator'

关于Airflow:如何扩展SubDagOperator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42256675/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com