Oracle Date类型的运行时变量在气流中

发布于 2025-02-07 00:12:24 字数 1511 浏览 2 评论 0 原文

我正在尝试定义需要在Apache Airffore中使用运行时参数的任务(我们称其为“ batch_dt')。
我正在使用 oraclestoredprocedureopererator ,并且该过程的参数是数据库类型 date

procedure use_dates (
    i_date  in date,
    i_date2 in date,
    i_date3 in date
  );

但是,我很难将此参数定义为运行时变量。我可以在特定数据库中使用精确的格式字符串,但不想依赖数据库中的当前NLS设置。

  1. 气流宏不起作用 {{dag_run.conf ['batch_dt']}}}} 否则


    {{macros.dateTime.strptime.strtptime(dag_run.conf ['batch_dttt to '],'%y-%m-%d')}} 返回始终导致

    的字符串

    ORA-01861:字面不匹配格式字符串

  2. 使用 to_date('{{dag_run.conf ['batch_dt']}}}','dd-mm yyyy'结果

    ORA-01858:在预期数字

    的情况下发现了一个非数字字符
  3. 当我定义 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.

  1. 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

  2. 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

  3. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2025-02-14 00:12:24

jinja templating ,用于apache气流中,将模板字段呈现为字符串,默认情况下。因此,您的 strptime 方法调用的结果在渲染时将转换为字符串。

要覆盖这种行为,并能够渲染本机Python对象而不是普通字符串,您必须将DAG的 render_template_as_native_obj 选项设置为true (默认选项的值的值为false)。

with DAG(
        'USE_DATES',
        description='TEST DATE INPUT RUNTIME DAG',
        ...
        render_template_as_native_obj=True
) as dag:
...

现在,您的 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).

with DAG(
        'USE_DATES',
        description='TEST DATE INPUT RUNTIME DAG',
        ...
        render_template_as_native_obj=True
) as dag:
...

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

飘逸的'云 2025-02-14 00:12:24
call_procedure = OracleStoredProcedureOperator(
    task_id="call_procedure",
    do_xcom_push=False,        
    procedure="PROC_XYZ",
    parameters={"P_IN_WORK_DATE":date(2020, 5, 17)},
)

您可以尝试1)airflow__core__enable_xcom_pickling:true 2)设置do_xcom_push = false。这些中的任何一个都应帮助您将日期作为输入传递,并避免下面显示的错误。
参考: - https://github.com/apache/apache/apache/apache/apache/issues/16386686686686686686686 #issuecomment-1060017364

错误 - 无法将XCOM值序列化为JSON。如果您正在使用
泡菜代替XCOM的JSON,然后您需要启用泡菜
支持XCOM在您的*** config。

call_procedure = OracleStoredProcedureOperator(
    task_id="call_procedure",
    do_xcom_push=False,        
    procedure="PROC_XYZ",
    parameters={"P_IN_WORK_DATE":date(2020, 5, 17)},
)

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

ERROR - Could not serialize the XCom value into JSON. If you are using
pickle instead of JSON for XCom, then you need to enable pickle
support for XCom in your *** config.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文