简体   繁体   中英

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

I have a task, instantiated from a PostgresOperator like this:

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

the dag fails to load saying "jinja2.exceptions.UndefinedError: 'ti' is undefined.

I get the same error when I replace 'ti' with task_instance. Any help would be much appreciated!

The weird thing is if I replace ti.xcom_pull(...) with ti it renders with no complaint, returning <TaskInstance: foo.bar 2019-04-25T14:27:06.822835+00:00 [None]>

I tested it with: {{task_instance.xcom_pull(task_ids='taskidwherexcomisset', key='foo').encode('utf-8')}} but I am sure {{ ti.xcom_pull(key='foo') }} will work too.

try:

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

example log line just to show it works:

[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/;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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