Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

Airflow dag and task decorator in 2.0: how to pass config params to task?

I'm struggling to understand how to read DAG config parameters inside a task using Airflow 2.0 dag and task decorators.

Consider this simple DAG definition file:

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()

I can trigger the dag using the UI or the console and pass to it some (key,value) config, for example:

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

How can I access {"hello":"there"} inside the task1 function?

My use case is I want to pass 2 parameters to dag and want task1 to see them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can access the context as follows:

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

where dagrun_conf will be the variable containing the DAG config parameters

Source: http://airflow.apache.org/docs/apache-airflow/2.0.0/concepts.html#accessing-current-context


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...