在操作员之外访问气流操作员值

时间:2018-10-17 19:59:44

标签: python jinja2 airflow apache-airflow-xcom

除了运算符之外,我还需要使用xcom调用SubdagOperator并将其传递给运算符的返回值。我见过很多解决方案(Airflow - How to pass xcom variable into Python functionHow to retrieve a value from Airflow XCom pushed via SSHExecuteOperator等)。

他们基本上都说'variable_name':“ {{ti.xcom_pull(task_ids ='some_task_id')}}”“

但是我的Jinja模板一直以字符串形式呈现,并且不返回实际变量。有什么想法吗?

这是我当前在主dag中的代码:

PARENT_DAG_NAME = 'my_main_dag'
CHILD_DAG_NAME = 'run_featurization_dag'

run_featurization_task = SubDagOperator(
    task_id=CHILD_DAG_NAME,
    subdag=run_featurization_sub_dag(PARENT_DAG_NAME, CHILD_DAG_NAME, default_args, cur_date, "'{{ ti.xcom_pull(task_ids='get_num_accounts', dag_id='" + PARENT_DAG_NAME + "') }}'" ),  
    default_args=default_args,
    dag=main_dag
)

2 个答案:

答案 0 :(得分:1)

引号过多?试试这个

"{{ ti.xcom_pull(task_ids='get_num_accounts', dag_id='" + PARENT_DAG_NAME + "') }}"

答案 1 :(得分:0)

Jinja模板仅适用于某些参数,而不是全部。

You can use Jinja templating with every parameter that is marked as “templated” in the documentation. Template substitution occurs just before the pre_execute function of your operator is called.

https://airflow.apache.org/concepts.html#jinja-templating

因此,恐怕您不能以这种方式传递变量。