Google bigquery 如何在云数据实验室上创建和使用UDF?

Google bigquery 如何在云数据实验室上创建和使用UDF?,google-bigquery,google-cloud-datalab,Google Bigquery,Google Cloud Datalab,我使用命令创建了一个名为“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': 'st

我使用命令创建了一个名为“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格式注释的参数。输入行参数 声明的类型必须为{field:type,field2:type}},并且 输出发射器参数声明的类型必须为 函数({field:type,field2:type})

因此,我在passthrough函数上方添加了jsdoc注释

/** 
 * @param {{field:string, field2:string}} row
 * @param function({{field:string, field2:string}}) emit 
 */
并运行sql命令。但它仍然返回一个错误“未知TVF:passthrough”


如何声明参数,并在以后的datalab上使用UDF?

我们目前的UDF支持是针对BigQuery中首次引入的早期UDF。我们正在积极更新现有的支持


您可以在我们的github repo上跟踪一些进度--…您可以在此处看到现有支持的示例(将发生变化):

您的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 $udf_call
%%sql
SELECT outputA, outputB FROM (passthrough(SELECT "abc" AS inputA, "def" AS inputB))