【问题标题】:Accessing airflow operator value outside of operator在操作员之外访问气流操作员值
【发布时间】:2018-10-17 19:59:43
【问题描述】:

在运算符之外,我需要调用 SubdagOperator 并使用 xcom 将运算符的返回值传递给它。我见过很多解决方案(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
)

【问题讨论】:

    标签: python jinja2 airflow


    【解决方案1】:

    引用太多?试试这个

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

    【讨论】:

      【解决方案2】:

      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

      所以恐怕你不能以这种方式传递变量。

      【讨论】:

        猜你喜欢
        • 2020-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多