Python 如何在PostgresOperator中提取XCOM值

Python 如何在PostgresOperator中提取XCOM值,python,airflow,apache-airflow-xcom,Python,Airflow,Apache Airflow Xcom,这里我推XCOM值: task_get_username_bash = BashOperator( task_id='execute_bash', bash_command='whoami', xcom_push=True) 因此,在XCOM中,它像{'return_value':'$USER'}(在我的例子中,$USER='afflow')一样存储 然后我想从XCOM中提取这个返回值: task_

这里我推XCOM值:

task_get_username_bash = BashOperator(
                task_id='execute_bash',
                bash_command='whoami',
                xcom_push=True)
因此,在XCOM中,它像{'return_value':'$USER'}(在我的例子中,$USER='afflow')一样存储

然后我想从XCOM中提取这个返回值:

task_insert_new_row = PostgresOperator(
                task_id='insert_new_row',
                trigger_rule=TriggerRule.ALL_DONE,
                sql='''INSERT INTO table_name VALUES
                (%s, %s, %s);''',
                parameters=(uuid.uuid4().int % 123456789,
                            "{{ ti.xcom_pull(task_ids='execute_bash', key='return_value') }}",
                            datetime.now()))
但PostgresOperator将宏引用解释为str。如何在PostgresOperator中提取XCOM?

问题已解决:

task_insert_new_row = PostgresOperator(
                task_id='insert_new_row',
                trigger_rule=TriggerRule.ALL_DONE,
                sql='''INSERT INTO table_name VALUES
                (%s, '{{ ti.xcom_pull(task_ids='execute_bash', key='return_value') }}', %s);''',
                parameters=(uuid.uuid4().int % 123456789, datetime.now()))

@eddy85br-您也可以在“*.sql”文件中使用jinja模板和宏(例如,如果您想传入执行日期以过滤结果集)。退房


如果我有一个
sql=file.sql
参数,我不能修改它,我只能传递一些来自DAG内部变量的参数,其中一个参数有
ti.xcom\u pull(…)
内容?@Vladyslav你知道如何通过postgres将返回值推送到其他操作符吗。想象一下,您使用的不是bash脚本,而是sql操作符。如果你知道的话,请帮我解决这个问题