gpt4 book ai didi

airflow - 对于Apache Airflow,如何通过CLI手动触发DAG时如何传递参数?

转载 作者:行者123 更新时间:2023-12-03 07:32:21 25 4
gpt4 key购买 nike

我使用Airflow来管理ETL任务的执行和计划。已创建DAG,并且工作正常。但是通过cli手动触发dag时可以传递参数。

例如:
我的DAG每天在01:30运行,并处理昨天的数据(时间范围为昨天01:30到今天01:30)。数据源可能存在一些问题。我需要重新处理这些数据(手动指定时间范围)。

因此,我可以在预定的时候创建这样的气流DAG,使其默认时间范围为昨天的01:30到今天的01:30。然后,如果数据源有任何问题,我需要手动触发DAG并手动将时间范围作为参数传递。

据我所知airflow test具有可以将参数传递给任务的-tp。但这仅用于测试特定任务。并且airflow trigger_dag没有-tp选项。那么有没有办法将tigger_dag传递给DAG,然后运算符(operator)可以读取这些参数?

谢谢!

最佳答案

您可以使用--conf '{"key":"value"}'从CLI传递参数,然后在DAG文件中将其用作模板字段中的"{{ dag_run.conf["key"] }}"

CLI :

airflow trigger_dag 'example_dag_conf' -r 'run_id' --conf '{"message":"value"}'

DAG文件:

args = {
'start_date': datetime.utcnow(),
'owner': 'airflow',
}

dag = DAG(
dag_id='example_dag_conf',
default_args=args,
schedule_interval=None,
)

def run_this_func(ds, **kwargs):
print("Remotely received value of {} for key=message".
format(kwargs['dag_run'].conf['message']))


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

# You can also access the DagRun object in templates
bash_task = BashOperator(
task_id="bash_task",
bash_command='echo "Here is the message: '
'{{ dag_run.conf["message"] if dag_run else "" }}" ',
dag=dag,
)

关于airflow - 对于Apache Airflow,如何通过CLI手动触发DAG时如何传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53663534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com