繁体   English   中英

气流:从PostgresOperator中的模板化SQL访问ti.xcom_pull()

[英]Airflow: access ti.xcom_pull() from templated SQL in PostgresOperator

我有一个任务是从PostgresOperator实例化的,如下所示:

sql = "select * from {{ ti.xcom_pull(key='foo') }};"
task = PostgresOperator(sql=sql)

dag无法加载,显示“ jinja2.exceptions.UndefinedError:'ti'未定义。

当我用task_instance替换'ti'时,出现相同的错误。 任何帮助将非常感激!

奇怪的是,如果我将ti.xcom_pull(...)替换为ti ,而没有任何抱怨,则返回<TaskInstance: foo.bar 2019-04-25T14:27:06.822835+00:00 [None]>

我用以下方法进行了测试: {{task_instance.xcom_pull(task_ids='taskidwherexcomisset', key='foo').encode('utf-8')}}但我确定{{ ti.xcom_pull(key='foo') }}也可以。

尝试:

sql = 'select * from ' + "{{ti.xcom_pull(key='foo').encode('utf-8')}}" + ';',

示例日志行只是为了显示它的工作原理:

[2019-04-25 15:39:24,715] {base_task_runner.py:98} INFO - Subtask: [2019-04-25 15:39:24,714] {postgres_operator.py:52} INFO - Executing: select * from 2019/04/24/;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM