[英]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.