我正在努力理解如何使用 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()
Run Code Online (Sandbox Code Playgroud)
我可以使用 UI 或控制台触发 dag 并将一些(键,值)配置传递给它,例如:
airflow dags trigger --conf '{"hello":"there"}' lovely_dag
Run Code Online (Sandbox Code Playgroud)
如何在 task1 函数中访问 {"hello":"there"} ?
我的用例是我想将 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
Run Code Online (Sandbox Code Playgroud)
dagrun_conf
包含 DAG 配置参数的变量在哪里
来源:http : //airflow.apache.org/docs/apache-airflow/2.0.0/concepts.html#accessing-current-context
归档时间: |
|
查看次数: |
1895 次 |
最近记录: |