Airflow 如何在Dag中创建分支?
我有一个这样的dag(这是一个半伪代码),我希望根据不同的分支的输出执行任务Airflow 如何在Dag中创建分支?,airflow,airflow-scheduler,airflow-operator,Airflow,Airflow Scheduler,Airflow Operator,我有一个这样的dag(这是一个半伪代码),我希望根据不同的分支的输出执行任务 #This is a method that return a or b def dosth(): ..... return a or b t1 = PythonOperator( 't1', python_callable = dosth ) branchA = BashOperator( 'branchA',.... ) branchB = BashOperator(
#This is a method that return a or b
def dosth():
.....
return a or b
t1 = PythonOperator(
't1',
python_callable = dosth
)
branchA = BashOperator(
'branchA',....
)
branchB = BashOperator(
'branchB',....
)
我想要的是,如果
dosth
返回a,我希望dag在branchA中执行任务,如果它返回b,我希望dag在branchB中执行任务。有人知道我们该怎么做吗?查看有关分支的文档:
您需要使用BranchPythonOperator
来指定要评估的条件,以决定下一步应该运行哪个任务
基于半伪代码的示例:
def dosth():
如果出现以下情况:
返回“branchA”
其他:
返回'branchB'
t1=分支操作器(
任务_id='t1',
提供上下文=True,
python_callable=dosth,
dag=dag)
branchA=bash算子(
“布兰卡”,。。。。
)
branchB=bash算子(
‘branchB’,。。。。
)
传递给python\u callable
的函数应该返回应该运行的下一个任务的task\u id
另一个例子:
def branch_func(**kwargs):
ti=kwargs['ti']
xcom\u value=int(ti.xcom\u pull(task\u id='start\u task'))
如果xcom_值>=5:
返回“继续任务”
其他:
返回“停止任务”
start_op=bash运算符(
task\u id='start\u task',
bash_command=“echo 5”,
xcom_push=True,
dag=dag)
branch_op=BranchPythonOperator(
task\u id='branch\u task',
提供上下文=True,
python\u callable=branch\u func,
dag=dag)
continue\u op=DummyOperator(任务id='continue\u task',dag=dag)
stop\u op=DummyOperator(任务id='stop\u任务',dag=dag)
开始操作>>分支操作>>[继续操作,停止操作]
谢谢!这很有帮助