在BigQueryOperator中将参数添加为template_fields

时间:2019-04-14 15:27:20

标签: airflow

我正在尝试在Bigquery运算符中对params字段进行模板化,如下所示。

t3 = MyBigQueryOperator(
    task_id='autoplay_calc',
    bql='autoplay_calc.sql',
    params={
            "env" : deployment
            ,"region" : region
            ,"partition_start_date" : '{{ macros.ds_add(ds, -1) }}'
            },
    bigquery_conn_id='gcp_conn',
    use_legacy_sql=False,
    write_disposition='WRITE_APPEND',
    allow_large_results=True,
    provide_context=True,
    destination_dataset_table=reporting_project + '.pa_reporting_public_batch.autoplay_calc',
    dag=dag
    )

我意识到params并不是模板字段,因此我按如下所示扩展了Bigqueryoperator使其成为模板字段。

class MyBigQueryOperator(BigQueryOperator):
    template_fields = ('params', 'bql', 'destination_dataset_table')

但是,当我运行代码时,当我收到下面的错误消息时,它似乎并没有转换params字段

Could not cast literal "{{ macros.ds_add(ds, -1) }}

1 个答案:

答案 0 :(得分:1)

简短的回答:params不支持模板,因为它是字典,因此需要将jinja2应用于键值对。您不能仅通过扩展template_fields属性来添加支持。