气流:将{{ds}}作为参数传递给PostgresOperator

编程入门 行业动态 更新时间:2024-10-26 22:21:12
本文介绍了气流:将{{ds}}作为参数传递给PostgresOperator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用执行日期作为我的sql文件的参数:

i would like to use execution date as parameter to my sql file:

我尝试过

dt = '{{ ds }}' s3_to_redshift = PostgresOperator( task_id='s3_to_redshift', postgres_conn_id='redshift', sql='s3_to_redshift.sql', params={'file': dt}, dag=dag )

但它不起作用。

推荐答案

dt ='{{ds}}'

不起作用,因为Jinja(气流中使用的模板引擎)可以

Doesn't work because Jinja (the templating engine used within airflow) does not process the entire Dag definition file.

对于每个操作员,Jinja将处理某些字段,这些字段是其中的一部分运算符本身的定义。

For each Operator there are fields which Jinja will process, which are part of the definition of the operator itself.

在这种情况下,您可以创建 params 字段(实际上称为 parameters ,请确保更改此模板),如果您扩展 PostgresOperator 这样的模板:

In this case, you can make the params field (which is actually called parameters, make sure to change this) templated if you extend the PostgresOperator like this:

class MyPostgresOperator(PostgresOperator): template_fields = ('sql','parameters')

现在您应该能够做到:

s3_to_redshift = MyPostgresOperator( task_id='s3_to_redshift', postgres_conn_id='redshift', sql='s3_to_redshift.sql', parameters={'file': '{{ ds }}'}, dag=dag )

更多推荐

气流:将{{ds}}作为参数传递给PostgresOperator

本文发布于:2023-11-23 17:35:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:气流   参数   ds   PostgresOperator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!