如何将use_legacy_sql = False传递给具有BigQuery连接的Airflow DAG中的SqlSensor?

时间:2019-05-02 12:25:16

标签: google-bigquery airflow

自Apache Airflow 1.9发行以来,我一直在使用自己的自定义SqlSensor,因为我无法使用在Google BigQuery上运行的Standard SQL语句中包含的SqlSensor,因为默认值为use legacy SQL

我检查了最近的1.10.3版本,看来情况仍然如此。除了使用我自己的SQL传感器作为插件之外,还有其他方法可以实现此目的吗?

3 个答案:

答案 0 :(得分:0)

从文档中的enabling standard SQL主题开始,如果您不能直接设置该选项,另一种方法是使用#standardSQL shebang,例如:

#standardSQL
SELECT x
FROM UNNEST([1, 2, 3]) AS x;

应该可以使用该前缀提交查询以覆盖设置。

答案 1 :(得分:0)

更新您的自定义传感器,以将use_legacy_sql=False传递到BigQueryHook。

hook = BigQueryHook(
            bigquery_conn_id=self.bigquery_conn_id,
            delegate_to=self.delegate_to,
            use_legacy_sql=False
       )

答案 2 :(得分:0)

我找到的最快的解决方案是

  • 定义一个新类 BigQuerySqlSensor
  • 覆盖 _get_hook 方法
  • 在覆盖中设置 use_legacy_sql=False
  • 返回更新的钩子
from airflow.sensors.sql_sensor import SqlSensor

class BigQuerySqlSensor(SqlSensor):
    def _get_hook(self):
        hook = super()._get_hook()
        hook.use_legacy_sql = False
        return hook

sense_stuff = BigQuerySqlSensor(
        dag=dag,
        task_id='sense_stuff',
        conn_id='the_connection_id',
        sql="SELECT COUNT(*) FROM some_table",
        mode='reschedule',
        poke_interval=600,
        timeout=(3600)
    )

相关问题