从Airflow Operator向SQL模板传递参数 [英] Passing arguments to sql template from airflow operator

查看:282
本文介绍了从Airflow Operator向SQL模板传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将BigQueryOperator与SQL模板一起使用,如何将参数传递给SQL?

If I am using a BigQueryOperator with a SQL Template, how could I pass an argument to the SQL?

文件:.sql /query.sql

File: .sql/query.sql

SELECT * FROM `dataset.{{ task_instance.variable_for_execution }}

文件:dag.py

BigQueryOperator(
    task_id='compare_tables',
    sql='./sql/query.sql',
    use_legacy_sql=False,
    dag=dag,
)


推荐答案

您可以在 params 参数,可以在模板字段中使用,如下所示:

You can pass an argument in params parameter which can be used in the templated field as follows:

BigQueryOperator(
    task_id='',
    sql='SELECT * FROM `dataset.{{ params.param1 }}',
    params={
        'param1': 'value1',
        'param2': 'value2'
    },
    use_legacy_sql=False,
    dag=dag
)

或者您可以在文件中单独使用SQL:

OR you can have the SQL separate in file:

文件:./sql/query.sql

File: ./sql/query.sql

SELECT * FROM `dataset.{{ params.param1 }}

params 参数的输入应为字典。通常,Airflow中的任何运算符都可以通过此 params 参数传递。

params parameter's input should be a dictionary. In general, any operator in Airflow can be passed this params parameter.

这篇关于从Airflow Operator向SQL模板传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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