Airflow Generate Dynamic Tasks in Single DAG , Task N+1 is Dependent on TaskN

后端 未结 2 904
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 06:02

When generating tasks dynamically, I need to have Task 2 be dependent of Task 1, Task1 >> Task 2 or task2.set_upstream(task1).

Since the task_ids are evaluated, or s

相关标签:
2条回答
  • 2020-12-09 06:09

    Use the following code:

    a = []
    for i in range(0,10):
        a.append(BashOperator(
            task_id='Component'+str(i),
            bash_command="echo  {{ ti.xcom_pull task_ids='SomeOtherTaskXcom', key='return_value') }} -z " + str(i) ,
            xcom_push=True,
            dag=dag))
        if i not in [0]: 
            a[i-1] >> a[i]
    

    Using a DummyOperator, the codes looks like:

    a = []
    for i in range(0,10):
        a.append(DummyOperator(
            task_id='Component'+str(i),
            dag=dag))
        if i not in [0]: 
            a[i-1] >> a[i]
    

    This would generate the following DAG:

    0 讨论(0)
  • 2020-12-09 06:13

    You can follow a pattern like this:

    with dag:
    
    d1 = DummyOperator(task_id='kick_off_dag')
    
    for i in range(0, 5):
        d2 = DummyOperator(task_id='generate_data_{0}'.format(i))
        d1 >> d2
    

    This will generate 5 tasks downstream from d1.

    0 讨论(0)
提交回复
热议问题