BigQuery temporay函数返回结构

时间:2019-03-15 11:01:54

标签: javascript sql arrays struct google-bigquery

我在Bigquery上执行我的JS临时功能时遇到了问题。此函数返回JS代码中的数组数组,如下例所示。我应该使用哪种STRUCT将数组的每个元素保存在表的列中?

该函数返回什么:

   var items = [[1, 2],[3, 4],[5, 6]];

使用的STRUCT:

RETURNS ARRAY<STRUCT<lat FLOAT64, lon FLOAT64>>

代码是这样的:

CREATE TEMPORARY FUNCTION

kMeans(x ARRAY<FLOAT64>,  
       y ARRAY<FLOAT64>, 
       iterations FLOAT64,  -- the number of iterations
       k FLOAT64)           -- the number of clusters

 RETURNS ARRAY<STRUCT<lat FLOAT64 , lon FLOAT64>>

LANGUAGE js AS """
'use strict'
function sumOfSquareDiffs(oneVector, anotherVector) {
  // the sum of squares error //
...
...
...
    return [[1.0, 2.0],[3.0, 4.0],[5.0, 6.0]];
}
""";

SELECT kMeans(lat, lon, 50.0, 3.0) FROM `dataset.table`

Result of the query

1 个答案:

答案 0 :(得分:0)

问题在于[[1.0, 2.0],[3.0, 4.0],[5.0, 6.0]]是一个数组数组,而不是一个结构数组。这是一个简单的示例,实际上返回了一些输出;希望这会帮助您入门:

CREATE TEMPORARY FUNCTION

kMeans(x ARRAY<FLOAT64>,  
       y ARRAY<FLOAT64>, 
       iterations FLOAT64,  -- the number of iterations
       k FLOAT64)           -- the number of clusters

 RETURNS ARRAY<STRUCT<lat FLOAT64 , lon FLOAT64>>

LANGUAGE js AS """
'use strict'
function sumOfSquareDiffs(oneVector, anotherVector) {
  // the sum of squares error //
  return [{lat: 1.0, lon: 2.0},
          {lat: 3.0, lon: 4.0},
          {lat: 5.0, lon: 6.0}];
}

return sumOfSquareDiffs([1, 2], [3, 4]);
""";

SELECT kMeans([1.2], [3.4], 50.0, 3.0);