我有2个任务。在第一个中,python运算符计算某些内容,在第二个中,我想在Http运算符中使用python运算符的输出。这是我的代码:
source_list = ['account', 'sales']
for source_type in source_list:
t2 = PythonOperator(
task_id='compute_next_gather_time_for_' + source_type,
python_callable=compute_next_gather_time,
provide_context=True,
trigger_rule=TriggerRule.ALL_SUCCESS,
op_args=[source_type],
retries=3
)
t3 = SimpleHttpOperator(
task_id='request_' + source_type + '_report',
method='POST',
http_conn_id='abc',
endpoint=endpoint,
data=json.dumps({
"query": {
"start": "{{ task_instance.xcom_pull(task_ids='prev_task_id') }}",
"stop": str(yesterday),
"fields": [
1
]
}
}),
headers={"Content-Type": "application/json", "Authorization": 'abc'},
response_check=lambda response: True if len(response.json()) == 0 else False,
log_response=True,
retries=3
)
查询:我想在t3的数据变量中传递以前的任务ID。我不确定该怎么做,因为t2任务ID不是恒定的。它随着source_type的变化而变化。显然,当我尝试时它没有呈现出来。
答案 0 :(得分:1)
我能够通过这样做得到它:
next(iter(context['task'].upstream_task_ids))
答案 1 :(得分:0)
以前我没有在任何DAG中使用Jinja模板,但是遇到了类似的问题,我需要从具有动态生成的task_id的特定任务中检索XCOM值。
您可以按照在T2中定义task_ids
的相同方式在T3中定义task_id
。例如:
source_list = ['account', 'sales']
for source_type in source_list:
task_id='compute_next_gather_time_for_' + source_type
t2 = PythonOperator(
task_id=task_id,
python_callable=compute_next_gather_time,
provide_context=True,
trigger_rule=TriggerRule.ALL_SUCCESS,
op_args=[source_type],
retries=3
)
t3 = SimpleHttpOperator(
task_id='request_' + source_type + '_report',
method='POST',
http_conn_id='abc',
endpoint=endpoint,
data=json.dumps({
"query": {
"start": "{{ task_instance.xcom_pull(task_ids=task_id) }}",
"stop": str(yesterday),
"fields": [
1
]
}
}),
headers={"Content-Type": "application/json", "Authorization": 'abc'},
response_check=lambda response: True if len(response.json()) == 0 else False,
log_response=True,
retries=3
)