如何获取气流dag运行的JobID? [英] How to get the JobID for the airflow dag runs?

查看:79
本文介绍了如何获取气流dag运行的JobID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们进行一次dagrun时,在Airflow UI上的图形视图中,我们将获得每个作业运行的详细信息。

When we do a dagrun, on the Airflow UI, in the "Graph View" we get details of each job run.

JobID类似于 scheduled__2017-04-11T10:47:00

我需要此JobID来跟踪和创建日志,在其中我维护每个任务/调试运行所花费的时间。

I need this JobID for tracking and log creation in which I maintain time each task/dagrun took.

所以我的问题是我如何在正在运行的同一dag中获取JobID

谢谢谢坦

推荐答案

此值实际上称为 run_id ,可以通过上下文或宏进行访问。

This value is actually called run_id and can be accessed via the context or macros.

在python运算符中,可以通过上下文进行访问,而在bash运算符中,可以通过jinja进行模板访问 bash_command 字段。

In the python operator this is accessed via context, and in the bash operator this is accessed via jinja templating on the bash_command field.

有关宏中可用内容的更多信息:

More info on what's available in macros:

https://airflow.apache.org/docs/stable/macros .html

有关jinja的更多信息:

More info on jinja:

https://airflow.apache.org/docs/stable/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 日志。

Use this dag as an example, and check the log for each operator, you should see the run_id printed in the log.

这篇关于如何获取气流dag运行的JobID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆