【问题标题】:How to create and use UDF on cloud datalab?如何在cloud datalab上创建和使用UDF?
【发布时间】:2015-11-11 08:46:32
【问题描述】:

我使用命令创建了一个名为“passthrough”的udf,

%%bigquery udf -m passthrough

function passthrough(row, emit) {
  emit({outputA: row.inputA, outputB: row.inputB});
}

bigquery.defineFunction(
  'passthrough',
  ['inputA', 'inputB'],
  [{'name': 'outputA', 'type': 'string'},
   {'name': 'outputB', 'type': 'string'}],
  passthrough
);

然后,它返回了错误。

JavaScript 必须声明输入行和输出发射器 参数使用有效的 jsdoc 格式 cmets。输入行参数 声明必须输入为 {{field:type, field2:type}} 并且 输出发射器参数声明必须键入为 函数({{field:type, field2:type}}.

所以,我在透传功能上面加了jsdoc cmets,

/** 
 * @param {{field:string, field2:string}} row
 * @param function({{field:string, field2:string}}) emit 
 */

然后运行 ​​sql 命令。但它仍然返回错误“Unknown TVF: passthrough”。

%%sql
SELECT outputA, outputB FROM (passthrough(SELECT "abc" AS inputA, "def" AS inputB))

如何声明参数,稍后在 datalab 上使用 UDF?

【问题讨论】:

    标签: google-bigquery google-cloud-datalab


    【解决方案1】:

    您的 UDF 定义应该是:

    /** 
     * @param {{field:string, field2:string}} row
     * @param function({{field:string, field2:string}}) emit 
     */
    function passthrough(row, emit) {
      emit({outputA: row.inputA, outputB: row.inputB});
    }
    

    如果您现在想使用 UDF,则需要在 Python 代码中使用中间步骤,当我们更新时,这将不再起作用(当您当前的操作方式应该基本正确时)。

    您需要将 UDF 应用于表格,然后执行以下操作:

    import gcp.bigquery as bq
    tbl = bq.Query('SELECT "abc" AS inputA, "def" AS inputB').results()
    udf_call = passthrough(tbl)
    

    然后在你的 SQL 中:

    %%sql
    SELECT outputA, outputB FROM $udf_call
    

    当更新到来时,你可以做你现在正在做的事情:

    %%sql
    SELECT outputA, outputB FROM (passthrough(SELECT "abc" AS inputA, "def" AS inputB))
    

    【讨论】:

      【解决方案2】:

      我们目前拥有的 UDF 支持在 BigQuery 中首次引入时针对早期的 UDF。我们正在积极努力更新我们所拥有的支持。

      您可以在我们的 github 存储库中跟踪一些进度 -- https://github.com/GoogleCloudPlatform/datalab ...您可以在此处查看现有支持的示例(将会更改):https://github.com/GoogleCloudPlatform/datalab/blob/master/dev/notebooks/BigQuery%20-%20JavaScript%20UDFs.ipynb

      【讨论】:

        猜你喜欢
        • 2017-10-11
        • 2018-09-19
        • 2017-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-12
        • 1970-01-01
        相关资源
        最近更新 更多