我的目标是根据气流宏变量 {{ds}} 返回上个月的第一天并使用它,例如在 Hive 操作符中。
例如。对于 ds = 2020-05-09 我预计返回:2020-04-01
我找到并尝试的解决方案是:
SET hivevar:LAST_MONTH='{{ (ds.replace(day=1) - macros.timedelta(days=1)).replace(day=1) }}';
SET hivevar:LAST_MONTH='{{ ds + macros.dateutil.relativedelta.relativedelta(months=-1, day=1) }}'
但两者都导致了错误:
Error rendering template: replace() takes no keyword arguments
Error rendering template: must be str, not relativedelta
并且渲染时没有显示任何日期。
我究竟做错了什么?
您可以使用:
{{ (execution_date + macros.dateutil.relativedelta.relativedelta(months=-1, day=1)).strftime("%Y-%m-%d") }}
Example:
from airflow.operators.bash_operator import BashOperator
default_args = {
'owner': 'airflow',
'start_date': datetime(2020, 4, 1),
}
with DAG(dag_id='stackoverflow',
default_args=default_args,
schedule_interval=None,
catchup=False
) as dag:
run_this = BashOperator(
task_id='example',
bash_command='echo ds is {{ ds }} modified ds is {{ (execution_date + macros.dateutil.relativedelta.relativedelta(months=-1, day=1)).strftime("%Y-%m-%d") }}',
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)