简体   繁体   English

将 dag_run_id 值从一个任务传递到另一个 airflow 任务

[英]pass dag_run_id value from one task to another airflow task

def my_function(**kwargs):
    global dag_run_id
    dag_run_id = kwargs['dag_run'].run_id

example_task = PythonOperator(
    task_id='example_task',
    python_callable=my_function,
    provide_context=True,
    dag=dag)

bash_task = BashOperator(
        task_id = 'bash_task' ,
        bash_command = 'echo {{ dag_run_id }}' ,
        dag = dag ,
        )

bash_task is not printing value of dag_run_id and i want to use dag_run_id in other tasks bash_task 不打印 dag_run_id 的值,我想在其他任务中使用 dag_run_id

XCom (Cross-Communication) is the mechanism in Airflow that allows you to share data between tasks. XCom(Cross-Communication)是 Airflow 中的一种机制,它允许您在任务之间共享数据。 Returning a value from a PythonOperator's callable automatically stores the value as an XCom.从 PythonOperator 的可调用对象返回一个值会自动将该值存储为 XCom。 So your Python function could do:所以你的 Python function 可以这样做:

def my_function(**kwargs):
    dag_run_id = kwargs["run_id"]
    return dag_run_id

Note that run_id is one of the templated variables given by Airflow, see the full list here: https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#variables .请注意, run_id是 Airflow 给出的模板变量之一,请在此处查看完整列表: https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#variables

This stores the returned value as an "XCom" in Airflow. You can observe XComs via the Grid View -> select task -> XCom, or see all XCom values via Admin -> XComs.这会将返回值作为“XCom”存储在 Airflow 中。您可以通过网格视图 -> select 任务 -> XCom 观察 XComs,或者通过管理 -> XComs 查看所有 XCom 值。 The task-specific XCom view shows something like this:特定于任务的 XCom 视图显示如下所示:

在此处输入图像描述

You can then fetch (known as "pull" in Airflow) the value in another task:然后,您可以获取(在 Airflow 中称为“拉取”)另一个任务中的值:

bash_task = BashOperator(
    task_id="bash_task",
    bash_command="echo {{ ti.xcom_pull(task_ids='example_task') }}",
)

This will fetch the XCom value from the task with id example_task and echo it.这将从具有 id example_task的任务中获取 XCom 值并回显它。

The full DAG code looks like this:完整的 DAG 代码如下所示:

import datetime

from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator

with DAG(
    dag_id="so_75213078",
    start_date=datetime.datetime(2023, 1, 1),
    schedule_interval=None,
):

    def my_function(**kwargs):
        dag_run_id = kwargs["run_id"]
        return dag_run_id

    example_task = PythonOperator(task_id="example_task", python_callable=my_function)

    bash_task = BashOperator(
        task_id="bash_task",
        bash_command="echo {{ ti.xcom_pull(task_ids='example_task') }}",
    )

    example_task >> bash_task

Tasks are executed by separate processes in Airflow (and sometimes on separate machines), therefore you cannot rely on eg global or a local file path to exist for all tasks.任务由 Airflow 中的单独进程执行(有时在单独的机器上),因此您不能依赖global或本地文件路径等所有任务的存在。

To pass a variable value from a task to another one, you can use Airflow Cross-Communication (XCom) as explained in the other answer.要将变量值从一个任务传递到另一个任务,您可以使用 Airflow Cross-Communication (XCom),如其他答案中所述。

But if you just want to pass the dag_run id, you don't need to do all of this, where it's available on all the tasks:但是如果你只想传递dag_run id,你不需要做所有这些,它在所有任务上都可用:

bash_task = BashOperator(
    task_id = 'bash_task' ,
    bash_command = 'echo {{ dag_run.run_id }}' ,
    dag = dag ,
)

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

相关问题 如何在 airflow 中将变量从一个任务传递到另一个任务 - How to pass a variable from one task to another in airflow 从 Airflow DAG 级别失败回调中获取失败任务的 task_id - Get task_id of failed task from within an Airflow DAG level failure callback 是否可以根据同一个 dag 中另一个任务的状态在一个 dag 中运行一个任务? - Is it possible to run a task in a dag based on the status of another task in the same dag? 气流:如何访问变量 run_id、task_id、dag_id 和尝试 nr。 在气流? - Airflow: How do I access the variables run_id, task_id, dag_id and attempt nr. in Airflow? 从一个 Airflow DAG 返回值到另一个 - Return value from one Airflow DAG into another one 如何在外部传感器气流中检查 DAG 中任务的不同运行时间 - How to check different run times of a task in a DAG in an External Sensor Airflow Airflow dag 级别重试值是否会覆盖任务级别重试值? - Will Airflow dag level retries value override task level retries value? Airflow DAG中的动态任务生成 - Dynamic task generation in an Airflow DAG airflow - 创建 dag 和任务为一个 object 动态创建管道 - airflow - creating dag and task dynamically create the pipeline for one object 气流,标记任务成功或在dag运行之前跳过它 - Airflow, mark a task success or skip it before dag run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM