Airflow 气流:在下一个任务中获取上一个任务id

Airflow 气流:在下一个任务中获取上一个任务id,airflow,apache-airflow-xcom,Airflow,Apache Airflow Xcom,我有两项任务。在第一个例子中,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操作符计算一些东西,在第二个例子中,我想在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不是常量。它随源类型的改变而改变。显然,当我尝试时,它没有呈现它。

我以前没有在任何DAG中使用Jinja模板,但我遇到过类似的问题,我需要从具有动态生成任务id的特定任务中检索XCOM值

您可以在T3中定义
任务id
,方法与在T2中定义
任务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
            )

我可以通过这样做得到:

next(iter(context['task'].upstream_task_ids))

谢谢你,伙计。成功了。我把“数据”改成了这个,效果很好。我没有创建变量,而是直接在jinja中使用它。data=json.dumps({“query”:{“start”:“{{task_instance.xcom_pull(task_ids='”+“compute_next__gather_time_for_uu+source_type+”)}”,“stop”:str(昨天),“fields”:[1]}),@Gagan,由于您的答案说明了如何实施建议,您能否将此作为新答案添加?这是否回答了您的问题?