如何在airflow中使用--conf选项

如何在airflow中使用--conf选项,airflow,apache-airflow,airflow-scheduler,Airflow,Apache Airflow,Airflow Scheduler,我正在尝试运行气流DAG,需要为任务传递一些参数 如何读取python dag文件中命令行trigger\u dag命令中作为--conf参数传递的JSON字符串 例如:气流触发器\u dag'dag\u name'-r'run\u id'-conf'{“key”:“value”}'两种方式。从模板字段或文件内部: {{ dag_run.conf['key'] }} 或者当上下文可用时,例如在python中可调用的PythonOperator: context['dag_run'].conf[

我正在尝试运行气流DAG,需要为任务传递一些参数

如何读取python dag文件中命令行
trigger\u dag
命令中作为--conf参数传递的JSON字符串


例如:
气流触发器\u dag'dag\u name'-r'run\u id'-conf'{“key”:“value”}'

两种方式。从模板字段或文件内部:

{{ dag_run.conf['key'] }}
或者当上下文可用时,例如在python中可调用的
PythonOperator

context['dag_run'].conf['key']

在这里提供的示例中,当试图解析在restapi调用中传递的'conf'时,请在pythonOperator中使用
provide\u context=True

另外,在RESTAPI调用中以json格式传递的键值对可以在bashOperator和sparkOperator中作为
'\'{{dag\u run.conf[“key”]if dag\u run else”“}\'


在上为希望从终端完成此操作的任何人找到了一个示例,请查看此我在Airflow server“dag_run”中遇到损坏的dag错误是否也可以从操作员之外的
dag
上下文中获取?在以DAG()作为DAG:上下文的
中?@nikste你做到了吗?
dag = DAG(
    dag_id="example_dag",
    default_args={"start_date": days_ago(2), "owner": "airflow"},
    schedule_interval=None
)

def run_this_func(**context):
    """
    Print the payload "message" passed to the DagRun conf attribute.
    :param context: The execution context
    :type context: dict
    """
    print("context", context)
    print("Remotely received value of {} for key=message".format(context["dag_run"].conf["key"]))

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

#BashOperator usage
bash_task = BashOperator(
    task_id="bash_task",
    bash_command='echo "Here is the message: \'{{ dag_run.conf["key"] if dag_run else "" }}\'"',
    dag=dag
)

#SparkSubmitOperator usage
spark_task = SparkSubmitOperator(
        task_id="task_id",
        conn_id=spark_conn_id,
        name="task_name",
        application="example.py",
        application_args=[
            '--key', '\'{{ dag_run.conf["key"] if dag_run else "" }}\''
        ],
        num_executors=10,
        executor_cores=5,
        executor_memory='30G',
        #driver_memory='2G',
        conf={'spark.yarn.maxAppAttempts': 1},
        dag=dag)