Airflow 尝试从XCom传递值时与PostgresOperator一起使用时,气流模板未呈现
我编写了一个非常简单的DAG,它首先执行一些python操作,然后用返回的值更新数据库表。以下是DAG代码:Airflow 尝试从XCom传递值时与PostgresOperator一起使用时,气流模板未呈现,airflow,apache-airflow-xcom,Airflow,Apache Airflow Xcom,我编写了一个非常简单的DAG,它首先执行一些python操作,然后用返回的值更新数据库表。以下是DAG代码: tmpl_search_path = Variable.get('sql_path') dag = DAG('test_dag', description='', schedule_interval='@daily', template_searchpath=tmpl_search_path, start_
tmpl_search_path = Variable.get('sql_path')
dag = DAG('test_dag',
description='',
schedule_interval='@daily',
template_searchpath=tmpl_search_path,
start_date=datetime(2017, 3, 20),
catchup=False)
# Returns a string
t1 = PythonOperator(task_id='t1',
python_callable= someCallable,
dag=dag)
update_monitoring_table_last_run = PostgresOperator(task_id='update_monitoring_table_last_run',
sql='update_monitoring_table_last_run.sql',
postgres_conn_id='conn_id',
params={"script_name": t1.task_id},
dag=dag)
update_monitoring_table_status_msg = PostgresOperator(task_id='update_monitoring_table_status_msg',
sql='update_monitoring_table_status_msg.sql',
postgres_conn_id='conn_id',
params={"script_name": str(t1.task_id),
"status_msg": "{{ ti.xcom_pull(key='return_value') }}"},
dag=dag)
t1 >> update_monitoring_table_last_run >> update_monitoring_table_status_msg
update\u monitoring\u table\u last\u run
的PostgresOperator运行完全正常,因此无需担心。如何update\u monitoring\u table\u status\u msg
无法按预期工作。
下面是基础SQL模板:
UPDATE table
SET status_msg = '{{ params.status_msg }}'
WHERE script_name = '{{ params.script_name }}'
但呈现的模板如下所示:
UPDATE bi.bi_scripts_monitoring
SET status_msg = '{{ ti.xcom_pull(key='return_value') }}'
WHERE script_name = 'script_name'
UPDATE bi.bi_scripts_monitoring
SET status_msg = 'StringThatWasReturnedByPythonOperator'
WHERE script_name = 'script_name'
但我希望它看起来像这样:
UPDATE bi.bi_scripts_monitoring
SET status_msg = '{{ ti.xcom_pull(key='return_value') }}'
WHERE script_name = 'script_name'
UPDATE bi.bi_scripts_monitoring
SET status_msg = 'StringThatWasReturnedByPythonOperator'
WHERE script_name = 'script_name'
为什么它不在这里呈现值?我用一个bash操作符尝试了同样的方法,结果模板被正确呈现了?
任何帮助都将不胜感激。我想我缺少了一些特定于XCom的内容。params
关键字未模板化,因此您需要将文件更改为以下内容:
更新表
设置状态_msg='{{params.status_msg}}'
其中script_name='{{ti.xcom_pull(key=“return_value”)}'
哦,那太糟糕了。无论如何,非常感谢你