繁体   English   中英

使用从 POST Json 传递的 Kwargs 定义全局 Airflow 变量

[英]Defining Global Airflow Variables Using Kwargs Passed From POST Json

我正在创建一个 DAG,它需要使用从用于触发作业的 POST Json 传入的 kwargs 设置全局变量的功能。 到目前为止,我已经尝试过这种方式:

import airflow
from airflow import DAG
from datetime import timedelta

DAG_Name = 'dag_test'

DEFAULT_ARGS = {
    'owner': '...',
    'depends_on_past': False,
    'email': ['...'],
    'email_on_failure': True,
    'start_date': datetime(2020,8,31)
}

dag = DAG(DAG_Name, default_args=DEFAULT_ARGS, dagrun_timeout=timedelta(hours=2))

snap_date = ''
output_loc = ''
recast = ''

def define_param(**kwargs):
    global snap_date
    global output_loc
    global recast
    snapshot = str(kwargs['dag_run'].conf['snap_date'])
    output_s3 = kwargs['dag_run'].conf['output_loc']
    recast = str(kwargs['dag_run'].conf['recast'])


DEFINE_PARAMETERS = PythonOperator(
    task_id='DEFINE_PARAMETERS',
    python_callable=define_param,
    provide_context=True,
    dag=dag)

但这不起作用。 我将如何使用 kwargs 设置全局 dag 变量?

使用Variable.set因为它将对数据库进行实际更新,并在需要时为您处理 session 和序列化。

Variable.set("snap_date", "2019-09-17")

参考: https://github.com/apache/airflow/blob/1.10.1/airflow/models.py#L4558-L4569

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM