Python 如何在Airflow中访问任务功能外的xcom_pull?
代码: 我想要Python 如何在Airflow中访问任务功能外的xcom_pull?,python,airflow,Python,Airflow,代码: 我想要addition()显示相同的结果: import datetime import logging from airflow import DAG from airflow.operators.python_operator import PythonOperator def hello_world(ti, execution_date, **context): logging.info("Hello World") return "
addition()
显示相同的结果:
import datetime
import logging
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
def hello_world(ti, execution_date, **context):
logging.info("Hello World")
return "Gorgeous"
def addition(ti, **context):
# Want belows are same each other
logging.info(context['params']["please1"])
logging.info(ti.xcom_pull(task_ids="hello_world"))
dag = DAG(
"test",
schedule_interval="@hourly",
start_date=datetime.datetime.now() - datetime.timedelta(days=1),
)
t1 = PythonOperator(
task_id="hello_world", python_callable=hello_world, dag=dag, provide_context=True
)
t2 = PythonOperator(
task_id="abc",
python_callable=addition,
dag=dag,
params={"please1": "{{{{ ti.xcom_pull(task_ids='{}') }}}}".format(t1.task_id)},
provide_context=True,
)
t1 >> t2
但结果是:
# Want belows are same each other
logging.info(context['params']["please1"])
logging.info(ti.xcom_pull(task_ids="hello_world"))
我想知道的是:是否可以在任务功能之外访问xcom\u pull
?e、 g.将值从xcom
传递到PythonOperator
时
谢谢 运算符的Jinja模板化参数只能用于运算符类中列为
template\u字段的字段。对于PythonOperator
,即op_args
、op_kwargs
和模板dict
。首先,将params
参数替换为op_-kwargs
,并删除Jinja的额外花括号——表达式两侧仅2个。其次,不幸的是,您需要在ti.xcom\u pull(task\u id='')调用中显式列出task\u id
修订守则:
导入日期时间
导入日志记录
从气流导入DAG
从afflow.operators.python_operator导入PythonOperator
def hello_world(ti,执行日期,**上下文):
logging.info(“你好世界”)
返回“华丽”
def添加(ti,**上下文):
logging.info(上下文[“请1”])
logging.info(ti.xcom\u pull(task\u id=“hello\u world”))
dag=dag(
“测试”,
计划时间间隔=无,
开始日期=datetime.datetime(2021,5,17),
catchup=False,
)
对于dag:
t1=蟒蛇算子(
task\u id=“你好”,
python\u callable=hello\u world,
提供上下文=True,
)
t2=蟒蛇算子(
task_id=“abc”,
python_callable=加法,
奥普·夸格斯={
“请1”:“{ti.xcom_pull(task_id='hello_world')}”,
},
提供上下文=True,
)
t1>>t2
从“t2”记录:
如果您使用的是Airflow 2.0,代码实际上可以简化为使用新的XComArg
feaure。此功能允许您使用简单的task.output
表达式访问任务的输出
使用2.0和XComArg
修改代码,用于访问作为“请1”参数的“t1”输出:
导入日期时间
导入日志记录
从气流导入DAG
从afflow.operators.python_operator导入PythonOperator
def hello_world(ti,执行日期,**上下文):
logging.info(“你好世界”)
返回“华丽”
def添加(ti,**上下文):
logging.info(上下文[“请1”])
logging.info(ti.xcom\u pull(task\u id=“hello\u world”))
dag=dag(
“测试”,
计划时间间隔=无,
开始日期=datetime.datetime(2021,5,17),
catchup=False,
)
对于dag:
t1=蟒蛇算子(
task\u id=“你好”,
python\u callable=hello\u world,
)
t2=蟒蛇算子(
task_id=“abc”,
python_callable=加法,
op_kwargs={“plesse1”:t1.output},
)
t1>>t2
更多关于使用2.0进行DAG创作的信息。只需添加一点,因为2.0.0版provide\u context
已被弃用,因此无需使用它。PythonOperator
code中有一个警告。@NicoE捕捉得很好。相应地编辑了代码示例。非常有用的答案,谢谢@还有一个问题:似乎有人在其他参数中使用Jinja,除了您提到的template_字段
:。在这种情况下,如何在数据
参数中使用Jinja?@user3595632对于SimpleHttpOperator
在该示例中,数据
参数是一个模板字段,因此使用Jinja模板是完全合适的。我猜想您可能想知道它是如何用于数据
dict中的开始
键的。这是因为整个数据
参数都可以模板化。即使整个data
参数不完全在一个Jinja表达式中,该参数的任何部分都可以是。这就是你要问的吗?
[2021-05-17 23:47:15,286] {test_dag.py:14} INFO - {{ ti.xcom_pull(task_ids='hello_world') }}
[2021-05-17 23:47:15,291] {test_dag.py:15} INFO - Gorgeous