Airflow 中的全局变量

Naz*_*eva 2 global-variables airflow airflow-scheduler apache-airflow-xcom

我正在尝试使用 Airflow 实现基本的 ETL 作业,但有一点:

我有3个功能。我想为每个变量定义全局变量,例如:

function a():
   return a_result

function b():
     use a
     return b_result
function c():
     use a and b
Run Code Online (Sandbox Code Playgroud)

然后在python_callable.

像往常一样定义global a_result是行不通的。任何解决方案?

abs*_*ted 7

正如我在评论中所写,

当您在您的 中返回某些内容时python_callable,如果您将任务上下文传递给下一个操作符,您就可以访问返回的值。https://airflow.apache.org/concepts.html?highlight=xcom

以下是说明这个想法的半伪代码

# inside a PythonOperator called 'pushing_task' 
def push_function(): 
    return value 

# inside another PythonOperator where provide_context=True 
def pull_function(**context): 
    value = context['task_instance'].xcom_pull(task_ids='pushing_task')

pushing_task = PythonOperator('pushing_task', 
                              push_function, ...)

pulling_task = PythonOperator('pulling_task', 
                              pull_function, 
                              provide_context=True ...)
Run Code Online (Sandbox Code Playgroud)