气流:将{{ds}}作为参数传递给PostgresOperator [英] Airflow: pass {{ ds }} as param to PostgresOperator

查看:253
本文介绍了气流:将{{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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆