繁体   English   中英

我可以使用 TriggerDagRunOperator 将参数传递给触发的 dag 吗? Airflow

[英]Can I use a TriggerDagRunOperator to pass a parameter to the triggered dag? Airflow

我正在使用 Airflow 1.10.11

I can read that I can send parameters to a dag execution using the UI and CLI ( https://airflow.apache.org/docs/apache-airflow/1.10.11/dag-run.html ), but can I do it使用 TriggerDagRunOperator? https://airflow.apache.org/docs/apache-airflow/1.10.11/_api/airflow/operators/dagrun_operator/index.ZFC35FDC70D5FC69D2698Z83A.8

阅读https://github.com/apache/airflow/blob/master/airflow/example_dags/example_trigger_target_dag.pyhttps://github.com/apache/airflow/blob/master/airflow/example_dagpy .像我一样,但是当我自己尝试这个脚本时,我无法检索到 conf 消息(可能是因为他们似乎在使用 airflow 2.0 而我使用的是 1.10)。

所以我的问题是:有没有办法在 TriggerDagRunOperator 那个目标时向目标 dag 发送参数?

提前致谢

对于airflow2.0.x ,您需要使用conf来传递参数。

如 PR 中所述,不推荐使用python_callable

https://github.com/apache/airflow/pull/6317

有关更多详细信息,请参阅

https://stackoverflow.com/a/67254524/3152654

这是一个示例,演示如何设置由TriggerDagRunOperator (in 1.10.11)触发的 dagruns 发送的 conf。

触发器.py

from datetime import datetime
from airflow.models import DAG
from airflow.operators.dagrun_operator import TriggerDagRunOperator

dag = DAG(
    dag_id='trigger',
    schedule_interval='@once',
    start_date=datetime(2021, 1, 1)
)


def modify_dro(context, dagrun_order):
    print(context)
    print(dagrun_order)
    dagrun_order.payload = {
        "message": "This is my conf message"
    }
    return dagrun_order


run_this = TriggerDagRunOperator(
    task_id='run_this',
    trigger_dag_id='my_dag',
    python_callable=modify_dro,
    dag=dag
)

dag.py

from datetime import datetime
from airflow.models import DAG
from airflow.operators.python_operator import PythonOperator

dag = DAG(
    dag_id='my_dag',
    schedule_interval='@once',
    start_date=datetime(2021, 1, 1)
)


def run_this_func(**context):
    print(context["dag_run"].conf)


run_this = PythonOperator(
    task_id='run_this',
    provide_context=True,
    python_callable=run_this_func,
    dag=dag,
)

这是 my_dag DAG 中run_this任务中的my_dag

[2021-03-05 16:39:15,649] {logging_mixin.py:112} INFO - {'message': 'This is my conf message'}

TriggerDagRunOperator 接受一个允许您修改 DagRunOrder 的 python_callable。 DagRunOrder 是一个抽象结构,它保存运行 id 和有效负载, 当目标 DAG 被触发时,它们将作为 conf 发送

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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