Airflow 动态任务依赖设置仅适用于列表中的最后一个元素
Airflow dynamic task dependency setting works only for last element on the list
我创建了一个 airflow 作业来检查 Google Cloud Platform 中客户端服务器上的文件,然后将其复制到指定的文件夹。
一切顺利,直到我决定在气流中创建动态任务。现在我的依赖项不想像这样正确设置:
相反,我得到了这个:
这是我为此目的创建的代码(我只包含相关部分):
EXPORT_BUCKET='gs://export'
SOURCE_BUCKET='gs://source'
system_list=["main"]
type_list=["road_cell_counters", "road_counters", "impact_counters", "cell_counters"]
...
with models.DAG(dag_id='return_feed',
schedule_interval=None,
description='return feed',
default_args=default_dag_args) as dag:
...
for system in system_list:
for type in type_list:
feed_check = BashOperator(
task_id=str(type)+'_check_'+str(system),
project_id=project,
xcom_push=True,
bash_command='''gsutil -q stat {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}}/_SUCCESS'''.format(source_bucket=SOURCE_BUCKET, system=system, type=type),
dag = dag
)
feed_copy = BashOperator(
task_id=str(type)+'_copy_'+str(system),
project_id=project,
bash_command='''gsutil cp -r {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}} {export_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/'''.format(source_bucket=SOURCE_BUCKET, export_bucket=EXPORT_BUCKET, system=system, type=type),
dag = dag
)
get_parameters >> feed_check >> feed_copy
因此,就我而言,我想查看第一个列表,然后再查看第二个列表,以涵盖任务的所有可能性。为此,我创建了两个 for 循环来遍历列表中的所有元素。
如您所见,出于某种原因,只有列表中的最后一个元素具有正确的依赖关系,这是为什么呢??
尝试在 for type in type_list:
循环中移动任务依赖表达式。否则,当在循环外设置依赖项时,将使用 feed_check
和 feed_copy
的最后一个实例。
for system in system_list:
for type in type_list:
feed_check = BashOperator(
task_id=str(type)+'_check_'+str(system),
project_id=project,
bash_command='''gsutil -q stat {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}}/_SUCCESS'''.format(source_bucket=SOURCE_BUCKET, system=system, type=type),
dag = dag
)
feed_copy = BashOperator(
task_id=str(type)+'_copy_'+str(system),
project_id=project,
bash_command='''gsutil cp -r {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}} {export_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/'''.format(source_bucket=SOURCE_BUCKET, export_bucket=EXPORT_BUCKET, system=system, type=type),
dag = dag
)
get_parameters >> feed_check >> feed_copy
我创建了一个 airflow 作业来检查 Google Cloud Platform 中客户端服务器上的文件,然后将其复制到指定的文件夹。
一切顺利,直到我决定在气流中创建动态任务。现在我的依赖项不想像这样正确设置:
相反,我得到了这个:
这是我为此目的创建的代码(我只包含相关部分):
EXPORT_BUCKET='gs://export'
SOURCE_BUCKET='gs://source'
system_list=["main"]
type_list=["road_cell_counters", "road_counters", "impact_counters", "cell_counters"]
...
with models.DAG(dag_id='return_feed',
schedule_interval=None,
description='return feed',
default_args=default_dag_args) as dag:
...
for system in system_list:
for type in type_list:
feed_check = BashOperator(
task_id=str(type)+'_check_'+str(system),
project_id=project,
xcom_push=True,
bash_command='''gsutil -q stat {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}}/_SUCCESS'''.format(source_bucket=SOURCE_BUCKET, system=system, type=type),
dag = dag
)
feed_copy = BashOperator(
task_id=str(type)+'_copy_'+str(system),
project_id=project,
bash_command='''gsutil cp -r {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}} {export_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/'''.format(source_bucket=SOURCE_BUCKET, export_bucket=EXPORT_BUCKET, system=system, type=type),
dag = dag
)
get_parameters >> feed_check >> feed_copy
因此,就我而言,我想查看第一个列表,然后再查看第二个列表,以涵盖任务的所有可能性。为此,我创建了两个 for 循环来遍历列表中的所有元素。
如您所见,出于某种原因,只有列表中的最后一个元素具有正确的依赖关系,这是为什么呢??
尝试在 for type in type_list:
循环中移动任务依赖表达式。否则,当在循环外设置依赖项时,将使用 feed_check
和 feed_copy
的最后一个实例。
for system in system_list:
for type in type_list:
feed_check = BashOperator(
task_id=str(type)+'_check_'+str(system),
project_id=project,
bash_command='''gsutil -q stat {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}}/_SUCCESS'''.format(source_bucket=SOURCE_BUCKET, system=system, type=type),
dag = dag
)
feed_copy = BashOperator(
task_id=str(type)+'_copy_'+str(system),
project_id=project,
bash_command='''gsutil cp -r {source_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/day={{{{ ti.xcom_pull(key="DAY") }}}} {export_bucket}/system={system}/type={type}/year={{{{ ti.xcom_pull(key="YEAR") }}}}/month={{{{ ti.xcom_pull(key="MONTH") }}}}/'''.format(source_bucket=SOURCE_BUCKET, export_bucket=EXPORT_BUCKET, system=system, type=type),
dag = dag
)
get_parameters >> feed_check >> feed_copy