gpt4 book ai didi

python - 将命令行参数传递给 airflow BashOperator

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:32 26 4
gpt4 key购买 nike

有没有办法将命令行参数传递给 Airflow BashOperator。目前,我有一个 python 脚本,它接受日期参数并执行一些特定的事件,例如清理比给定日期早的特定文件夹。

在只有一个任务的简化代码中,我想做的是

from __future__ import print_function
from airflow.operators import BashOperator
from airflow.models import DAG
from datetime import datetime, timedelta

default_args = {
'owner' : 'airflow'
,'depends_on_past' : False
,'start_date' : datetime(2017, 01, 18)
,'email' : ['abc@xyz.com']
,'retries' : 1
,'retry_delay' : timedelta(minutes=5)
}

dag = DAG(
dag_id='data_dir_cleanup'
,default_args=default_args
,schedule_interval='0 13 * * *'
,dagrun_timeout=timedelta(minutes=10)
)

cleanup_task = BashOperator(
task_id='task_1_data_file_cleanup'
,bash_command='python cleanup.py --date $DATE 2>&1 >> /tmp/airflow/data_dir_cleanup.log'
#--------------------------------------^^^^^^-- (DATE variable which would have been given on command line)
#,env=env
,dag=dag
)

提前致谢

最佳答案

BashOperator 是使用 Jinja2 模板化的,这意味着您可以传递任意值。在你的情况下,它会是这样的:

cleanup_task = BashOperator(
task_id='task_1_data_file_cleanup'
,bash_command="python cleanup.py --date {{ params.DATE }} 2>&1 >> /tmp/airflow/data_dir_cleanup.log"
,params = {'DATE' : 'this-should-be-a-date'}
,dag=dag
)

另请参阅:https://airflow.incubator.apache.org/tutorial.html#templating-with-jinja更广泛的示例。

关于python - 将命令行参数传递给 airflow BashOperator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42016491/

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