2.0中的气流dag和任务装饰器:如何将配置参数传递给任务



我很难理解如何使用Airflow 2.0 DAG和任务装饰器读取任务中的DAG配置参数。

考虑一下这个简单的DAG定义文件:

from airflow.decorators import dag, task
from airflow.utils.dates import days_ago
@dag()
def lovely_dag():

@task(start_date=days_ago(1))
def task1():
return 1
something = task1()
my_dag = lovely_dag()

我可以使用UI或控制台触发dag,并将一些(键、值(配置传递给它,例如:

airflow dags trigger --conf '{"hello":"there"}' lovely_dag

如何访问task1函数内部的{"你好":"有"}?

我的用例是,我想将2个参数传递给dag,并希望task1看到它们。

您可以访问如下上下文:

from airflow.operators.python import task, get_current_context
@task
def my_task():
context = get_current_context()
dag_run = context["dag_run"]
dagrun_conf = dag_run.conf

其中dagrun_conf将是包含DAG配置参数的变量

来源:http://airflow.apache.org/docs/apache-airflow/2.0.0/concepts.html#accessing-当前上下文

最新更新