Airflow的bigqueryoperator没有使用udf

时间:2018-05-25 11:01:53

标签: google-bigquery airflow google-cloud-composer

我正在尝试使用用户定义函数(UDF)在Airflow中运行基本的bigquery运算符(使用Google的Composer)。

该示例来自https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions,并在BigQuery中完美运行。

然而,当我上传到作曲家时,我得到“找不到函数:multiplyInputs ...”请参阅下面的python脚本。

BigQueryOperator的udf_config字段需要一个列表,因此我将UDF定义为包含一个字符串的列表 - 不确定这是否正确,因为它显然没有注册为UDF

任何帮助都将受到高度赞赏。

import datetime
from airflow import models
from airflow.contrib.operators import bigquery_operator

yesterday = datetime.datetime.combine(datetime.datetime.today() -             
datetime.timedelta(1),
                                  datetime.datetime.min.time())
default_dag_args = {
                # Setting start date as yesterday starts the DAG 
immediately when it is
                # detected in the Cloud Storage bucket.
                'start_date': yesterday,
                # To email on failure or retry set 'email' arg to your 
email and enable
                # emailing here.
                'email_on_failure': False,
                'email_on_retry': False,
                # If a task fails, retry it once after waiting at least 
5 minutes
                'retries': 1,
                'retry_delay': datetime.timedelta(minutes=5),
                'project_id': 'vital-platform-791'
}

with models.DAG('udf_example',
                schedule_interval=datetime.timedelta(days=1),
                default_args=default_dag_args) as dag:

    table = 'udf_table'

    # flatten fe table
    task_id = table + '_fe'

    udf_config = ["""CREATE TEMPORARY FUNCTION multiplyInputs(x 
                  FLOAT64, y FLOAT64)
                  RETURNS FLOAT64
                  LANGUAGE js AS \"""
                  return x*y;
                  \""";
                  """]

    print udf_config

    query = """WITH numbers AS
              (SELECT 1 AS x, 5 as y
              UNION ALL
              SELECT 2 AS x, 10 as y
              UNION ALL
              SELECT 3 as x, 15 as y)
            SELECT x, y, multiplyInputs(x, y) as product
            FROM numbers"""

    print query

    query = query
    destination_table = 'vital-platform-791.alpha_factors. 
                          {table}_fe'.format(table=table)

    t_fe = bigquery_operator.BigQueryOperator(task_id=task_id,
                                          bql=query,                                         
                     destination_dataset_table=destination_table,
                                          use_legacy_sql=False,                                              
                     write_disposition='WRITE_TRUNCATE',
                                          udf_config=udf_config)

2 个答案:

答案 0 :(得分:1)

我对这个例子有点困惑。看起来您只需要合并udf_configquery

query = ""CREATE TEMPORARY FUNCTION multiplyInputs(x 
              FLOAT64, y FLOAT64)
              RETURNS FLOAT64
              LANGUAGE js AS \"""
              return x*y;
              \""";
              WITH numbers AS
          (SELECT 1 AS x, 5 as y
          UNION ALL
          SELECT 2 AS x, 10 as y
          UNION ALL
          SELECT 3 as x, 15 as y)
        SELECT x, y, multiplyInputs(x, y) as product
        FROM numbers;"""

答案 1 :(得分:0)

在Google云端存储中上传您的UDF功能,并将其传递给udf_config param。

例如:

您的UDF功能位于gs://test-bucket/testfolder/udf.js

然后在你的气流dag使用:

udf_gcs_path = "gs://test-bucket/testfolder/udf.js"

bigquery_operator.BigQueryOperator(task_id=task_id,
    bql=query,                                         
    destination_dataset_table=destination_table,
    use_legacy_sql=False,
    write_disposition='WRITE_TRUNCATE',
    udf_config=[{"resourceUri": udf_gcs_path}])

<强>参考