Airflow 如何在气流中捕获调用的DAG中传递的--conf参数

Airflow 如何在气流中捕获调用的DAG中传递的--conf参数,airflow,Airflow,我正在尝试从RESTAPI运行DAG,并向其传递一些参数。DAG应该能够捕获参数并使用它。问题是我能够从RESTAPI触发DAG,但DAG无法捕获传递的参数。有没有办法做到这一点 我正在从RESTAPI触发DAG,如下所示 如何在被调用的DAG中捕获conf value中传递的值。据我所知,conf应该采用URL编码的JSON格式数据 DAG代码:` def run_this_func(**kwargs): print(kwargs) run_this = PythonOperator(

我正在尝试从RESTAPI运行DAG,并向其传递一些参数。DAG应该能够捕获参数并使用它。问题是我能够从RESTAPI触发DAG,但DAG无法捕获传递的参数。有没有办法做到这一点

我正在从RESTAPI触发DAG,如下所示

如何在被调用的DAG中捕获conf value中传递的值。据我所知,conf应该采用URL编码的JSON格式数据

DAG代码:`

def run_this_func(**kwargs):
print(kwargs)

run_this = PythonOperator(
    task_id='run_this',
    python_callable=run_this_func,
    dag=dag
)`

不幸的是,这不是一个文档化的功能,但是有一些示例表明,DAG使用
conf
集合触发另一个DAG,目标DAG使用它。见和。由操作员、REST API或CLI触发的DAG都应以相同的方式传递
conf
参数

conf
可在上下文中访问,因此在使用
PythonOperator
时,您需要确保通过
provide\u context=True

def run_this_func(**kwargs):
    print(kwargs['conf'])

run_this = PythonOperator(
    task_id='run_this',
    python_callable=run_this_func,
    dag=dag,
    provide_context=True,
)

我不知道您可以使用HTTPGET触发DAG,但我已经使用POST并遵循文档成功地使用conf触发了DAG

例如,触发dag“trigger_test_dag”:


注意撇号的转义,因为conf需要是字符串。如果愿意,我想您可以对字符串进行base64编码,然后在DAG中解码。

传递的外部参数是
DAG\u run
对象的一部分。可通过以下方式访问这些文件:

API请求

def run_this_func(**kwargs):
    print(kwargs['conf'])

run_this = PythonOperator(
    task_id='run_this',
    python_callable=run_this_func,
    dag=dag,
    provide_context=True,
)
curl -X POST --data '"conf":"{\"key\":\"value\"}"' \
"http://abcairflow.com:8090/api/experimental/dags/trigger_test_dag/dag_runs"
import requests

headers = {
    'Cache-Control': 'no-cache',
    'Content-Type': 'application/json',
}

data = '{"conf":"{\\"Key1\\":\\"Value1\\"}"}'

response = requests.post('http://localhost:8080/api/experimental/dags/<dag_id>/dag_runs', headers=headers, data=data)
 def run_this_func(**context):
    print("Received {} for key=message".format(context["dag_run"].conf))


run_this = PythonOperator(task_id="run_this", python_callable=run_this_func, provide_context=True, dag=dag)