我正在尝试定义需要在Apache Airffore中使用运行时参数的任务(我们称其为“ batch_dt')。
我正在使用 oraclestoredprocedureopererator ,并且该过程的参数是数据库类型 date 。
。
procedure use_dates (
i_date in date,
i_date2 in date,
i_date3 in date
);
但是,我很难将此参数定义为运行时变量。我可以在特定数据库中使用精确的格式字符串,但不想依赖数据库中的当前NLS设置。
-
气流宏不起作用 {{dag_run.conf ['batch_dt']}}}}
否则
{{macros.dateTime.strptime.strtptime(dag_run.conf ['batch_dttt to '],'%y-%m-%d')}}
返回始终导致
的字符串
ORA-01861:字面不匹配格式字符串
使用 to_date('{{dag_run.conf ['batch_dt']}}}','dd-mm yyyy'结果
ORA-01858:在预期数字
的情况下发现了一个非数字字符
当我定义 date.today()
时,它可以正常工作,但是我需要使用运行时变量。
res_task = OracleStoredProcedureOperator(
task_id = 'mytask',
procedure = 'use_dates',
parameters =
{"i_date": date.today(), #works but is not runtime
"i_date2": "to_date('{{ dag_run.conf['batch_dt'] }}', 'DD-MM-YYYY')",
#returns a string "to_date('13-06-2022', 'DD-MM-YYYY')" which results in ORA-01858: a non-numeric character was found where a numeric was expected
"i_date3": "{{ macros.datetime.strptime(dag_run.conf['batch_dt'], '%Y-%m-%d' ) }}"
#returns a string '2022-06-13 00:00:00' which results in ORA-01861: literal does not match format string
}
)
我正在考虑在运行时返回DateTime对象的宏,但是宏似乎只能返回字符串。知道如何实现这一目标吗?
I'm trying to define task that requires a run-time parameter (let's call it 'batch_dt') in Apache Airflow.
I'm using OracleStoredProcedureOperator and the parameter of the procedure is of database type date.
procedure use_dates (
i_date in date,
i_date2 in date,
i_date3 in date
);
However I'm having hard time defining this parameter as runtime variable. I could use an exactly formatted string for particular database but don't want to depend on current NLS setting in the database.
-
Airflow macros don't work {{ dag_run.conf['batch_dt'] }}
or even
{{ macros.datetime.strptime(dag_run.conf['batch_dt'], '%Y-%m-%d') }}
returns always a string resulting in
ORA-01861: literal does not match format string
-
Using to_date('{{ dag_run.conf['batch_dt'] }}', 'DD-MM-YYYY')
results in
ORA-01858: a non-numeric character was found where a numeric was expected
-
When I define date.today()
right in the task, it works fine, however I need to use run-time variable.
res_task = OracleStoredProcedureOperator(
task_id = 'mytask',
procedure = 'use_dates',
parameters =
{"i_date": date.today(), #works but is not runtime
"i_date2": "to_date('{{ dag_run.conf['batch_dt'] }}', 'DD-MM-YYYY')",
#returns a string "to_date('13-06-2022', 'DD-MM-YYYY')" which results in ORA-01858: a non-numeric character was found where a numeric was expected
"i_date3": "{{ macros.datetime.strptime(dag_run.conf['batch_dt'], '%Y-%m-%d' ) }}"
#returns a string '2022-06-13 00:00:00' which results in ORA-01861: literal does not match format string
}
)
I was thinking about macro that returns a datetime object at runtime, however it seems macros can return only strings. Any idea how this can be achieved?
发布评论
评论(2)
jinja templating ,用于apache气流中,将模板字段呈现为字符串,默认情况下。因此,您的 strptime 方法调用的结果在渲染时将转换为字符串。
要覆盖这种行为,并能够渲染本机Python对象而不是普通字符串,您必须将DAG的 render_template_as_native_obj 选项设置为true (默认选项的值的值为false)。
现在,您的 oraclestoredProcedureOpererator 呼叫现在应按预期运行,将Jinja传递为本机Python对象(在这种情况下为DateTime)。
有关更多信息,请参阅官方气流的文档:
Jinja templating, used in Apache Airflow, renders template fields as strings by default. Therefore the result of your strptime method call is converted into string when rendered.
To override this behavior and be able to render native Python objects instead of plain strings, you have to set DAG's render_template_as_native_obj option to True (default option's value is False).
Your OracleStoredProcedureOperator call should now function as expected, passing Jinja rendered parameters as native Python objects (datetime in this case).
For more information please refer to official Airflow's documentation:
https://airflow.apache.org/docs/apache-airflow/stable/concepts/operators.html#rendering-fields-as-native-python-objects
您可以尝试1)airflow__core__enable_xcom_pickling:true 2)设置do_xcom_push = false。这些中的任何一个都应帮助您将日期作为输入传递,并避免下面显示的错误。
参考: - https://github.com/apache/apache/apache/apache/apache/issues/16386686686686686686686 #issuecomment-1060017364
You can try 1) AIRFLOW__CORE__ENABLE_XCOM_PICKLING: true 2) Set do_xcom_push=False . Any of these should help you to pass date as input and also avoid error shown below.
Reference:- https://github.com/apache/airflow/issues/16386#issuecomment-1060017364