简体   繁体   English

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

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

Im using Airflow 1.10.11我正在使用 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 using a TriggerDagRunOperator? 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.html ). https://airflow.apache.org/docs/apache-airflow/1.10.11/_api/airflow/operators/dagrun_operator/index.ZFC35FDC70D5FC69D2698Z83A.8

Reading https://github.com/apache/airflow/blob/master/airflow/example_dags/example_trigger_target_dag.py , https://github.com/apache/airflow/blob/master/airflow/example_dags/example_trigger_controller_dag.py it seems like I can, but when I tried this script myself I was not able to retrieve the conf message (probably because it seems they are using airflow 2.0 while I am using 1.10).阅读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)。

So my question is: is there a way to send parameter to the target dag when TriggerDagRunOperator that target?所以我的问题是:有没有办法在 TriggerDagRunOperator 那个目标时向目标 dag 发送参数?

Thanks in advance提前致谢

For airflow2.0.x , you'll need to use conf to pass the parameters.对于airflow2.0.x ,您需要使用conf来传递参数。

The use of python_callable is deprecated as mentioned in the PR如 PR 中所述,不推荐使用python_callable

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

For more details, refer有关更多详细信息,请参阅

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

Here is an example that demonstrates how to set the conf sent with dagruns triggered by TriggerDagRunOperator (in 1.10.11) .这是一个示例,演示如何设置由TriggerDagRunOperator (in 1.10.11)触发的 dagruns 发送的 conf。

trigger.py触发器.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 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,
)

Here is the output from the run_this task in the my_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'}

The TriggerDagRunOperator takes in a python_callable that allows you to modify the DagRunOrder. TriggerDagRunOperator 接受一个允许您修改 DagRunOrder 的 python_callable。 DagRunOrder is an abstract construct that holds the run id and payload which will be sent as conf when the target DAG is triggered . DagRunOrder 是一个抽象结构,它保存运行 id 和有效负载, 当目标 DAG 被触发时,它们将作为 conf 发送

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

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