Pentium10 提出的某种选项在 GBQ UI 或 API Explorer 中从未对我有用。
我可能遗漏了什么
同时,我发现的解决方法如下例所示
SELECT location.state, location.city FROM JS(
( // input table
SELECT NEST(CONCAT(state, ',', city)) AS locations
FROM (
SELECT state, city FROM
(SELECT 'florida' AS state, 'miami' AS city),
(SELECT 'california' AS state, 'la' AS city),
(SELECT 'romania' AS state, 'transylvania' AS city)
)
),
locations, // input columns
"[ // output schema
{'name': 'location', 'type': 'RECORD',
'mode': 'REPEATED',
'fields': [
{'name': 'state', 'type': 'STRING'},
{'name': 'city', 'type': 'STRING'}
]
}
]",
"function(row, emit){ // function
for (var i = 0; i < row.locations.length; i++) {
var c = [];
x = row.locations[i].split(',');
t = {state:x[0], city:x[1]}
c.push(t);
emit({location: c});
};
}"
)
请注意:
您应该使用Allow Large Results 设置目标表并取消选中Flatten Results
输出表的结果是(在 JSON 模式下)
[
{
"location": [
{
"state": "california",
"city": "la"
}
]
},
{
"location": [
{
"state": "florida",
"city": "miami"
}
]
},
{
"location": [
{
"state": "romania",
"city": "transylvania"
}
]
}
]
添加以解决@AdiCohen 在他的真实示例中遇到的一些问题
他在最近的 cmets 中展示了:
问:我的查询除了记录列之外还有其他列,但是当我运行时
查询,它们返回为空。我怎样才能创建一个表
类型?
SELECT amount, currency, location.state, location.city FROM JS(
( // input table
SELECT NEST(CONCAT(state, ',', city)) AS locations,
SUM(amount) AS amount, MAX(currency) as currency
FROM (
SELECT state, city, amount, currency, ROW_NUMBER() OVER() as grp FROM
(SELECT 'florida' AS state, 'miami' AS city, 'coins' AS currency, 40 AS amount),
(SELECT 'california' AS state, 'la' AS city, 'coins' AS currency, 40 AS amount),
(SELECT 'romania' AS state, 'transylvania' AS city,'coins' AS currency, 40 AS amount)
) GROUP BY grp
),
amount, currency, locations, // input columns
"[ // output schema
{'name': 'location', 'type': 'RECORD', 'mode': 'REPEATED',
'fields': [
{'name': 'state', 'type': 'STRING'},
{'name': 'city', 'type': 'STRING'}
] },
{ 'name': 'amount', 'type': 'INTEGER'},
{ 'name': 'currency', 'type': 'STRING'}
]",
"function(row, emit) { // function
for (var i = 0; i < row.locations.length; i++) {
var c = [];
x = row.locations[i].split(',');
t = {state:x[0], city:x[1]}
c.push(t);
emit({amount: row.amount, currency: row.currency, location: c});
};
}"
)
这里的输出是:
[
{
"amount": "40",
"currency": "coins",
"location_state": "romania",
"location_city": "transylvania"
},
{
"amount": "40",
"currency": "coins",
"location_state": "florida",
"location_city": "miami"
},
{
"amount": "40",
"currency": "coins",
"location_state": "california",
"location_city": "la"
}
]