Airflow 中的全局变量
Global variables in Airflow
我正在尝试使用 Airflow 实现基本的 ETL 作业,但卡在了一点:
我有3个功能。我想为每个变量定义全局变量,例如:
function a():
return a_result
function b():
use a
return b_result
function c():
use a and b
然后在python_callable
中使用这些函数。
照常定义 global a_result
无效。有什么解决办法吗?
正如我在评论中所写,
When you return something in your python_callable
, you can access the returned value if you pass the task context to the next operator. 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 ...)
我正在尝试使用 Airflow 实现基本的 ETL 作业,但卡在了一点:
我有3个功能。我想为每个变量定义全局变量,例如:
function a():
return a_result
function b():
use a
return b_result
function c():
use a and b
然后在python_callable
中使用这些函数。
照常定义 global a_result
无效。有什么解决办法吗?
正如我在评论中所写,
When you return something in your
python_callable
, you can access the returned value if you pass the task context to the next operator. 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 ...)