如何获取气流dag运行的作业ID?

2024-04-25 23:31:41 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
1条回答
网友
1楼 · 发布于 2024-04-25 23:31:41

这个值实际上称为run_id,可以通过上下文或宏访问。

在python操作符中,这是通过上下文访问的,在bash操作符中,这是通过bash_command字段上的jinja模板访问的。

有关宏中可用内容的详细信息:

https://airflow.incubator.apache.org/code.html#macros

更多关于jinja的信息:

https://airflow.incubator.apache.org/concepts.html#jinja-templating

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


dag = DAG(
    dag_id='run_id',
    schedule_interval=None,
    start_date=datetime(2017, 2, 26)
)

def my_func(**kwargs):
    context = kwargs
    print(context['dag_run'].run_id)

t1 = PythonOperator(
    task_id='python_run_id',
    python_callable=my_func,
    provide_context=True,
    dag=dag
    )

t2 = BashOperator(
    task_id='bash_run_id',
    bash_command='echo {{run_id}}',
    dag=dag)

t1.set_downstream(t2)

以这个dag为例,检查每个操作符的日志,您应该会看到日志中打印的run_id

相关问题 更多 >