Google bigquery 创建具有列类型记录的表

Google bigquery 创建具有列类型记录的表,google-bigquery,Google Bigquery,我正在使用大查询,我想创建一个用记录类型列填充表的作业。 数据将由一个查询填充——那么我如何编写一个返回记录类型列的查询呢 谢谢 您需要使用点表示法将输出反映为记录示例查询: select 'florida' as country.state, 'SFO' as country.city; 在本例中,country是记录,state | city是记录中的字段。不知何故,奔腾10提出的选项从未在GBQ UI或API Explorer中对我有效。 我可能错过了什么 同时,我发现的解决

我正在使用大查询,我想创建一个用记录类型列填充表的作业。 数据将由一个查询填充——那么我如何编写一个返回记录类型列的查询呢

谢谢

您需要使用点表示法将输出反映为记录示例查询:

select 
  'florida' as country.state, 
  'SFO' as country.city;

在本例中,country是记录,state | city是记录中的字段。

不知何故,奔腾10提出的选项从未在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});  
    };
  }"
)  
为了解决@AdiCohen的真实例子中的一些问题 他在最近的评论中表明:

问:除了记录列之外,我的查询还有其他列,但是当我运行 查询时,它们返回为null。我如何创建一个同时具有这两个属性的表 类型


你的利率很低。重要信息:因此,您必须使用投票下方张贴答案左侧的勾号来标记已接受的答案。这将提高你的利率。通过访问此链接了解其工作原理:请澄清-这是否适用于BQ UI?还是API浏览器-?以上建议从未在BQ UI中为我工作过!例如,您可以使用API资源管理器提供工作设置的示例吗?会很有帮助的!这个答案直接来自@MoshaPasumansky的付费谷歌支持,他为我们几个月前发布的一张罚单提供了答案。这个答案有效,但不要被标题名所愚弄,试试这个:选择country.state从中选择'florida'作为country.state,'SFO'作为country.city进一步验证,尝试选择无效的内容,例如:country.xyq,它将响应未找到的字段“country.xyq”,因此记录已正确创建。问题是创建具有列类型记录的表。所以我在问——你真的试过用你的答案吗?因为,正如我所说,这对我来说从来都不起作用。只是在不创建表或输出到表的情况下运行它—什么也没说!同时,我可能会错过一些东西,这就是我为什么要问的原因。我刚刚试过,它成功了,cell在新的表格中创下了记录:非常感谢!这真的很有帮助。我在哪里可以找到更多关于udf功能的内容?重要的是,您可以使用投递答案左侧投票下方的勾号标记接受答案。看看为什么它很重要,你能帮我吗?除了记录列之外,我的查询还有其他列,但是当我运行查询时,它们返回为null,在这个注释中没有足够的空间。如何创建一个同时具有以下两种类型的表:从JS中选择amount、location.state、location.city//input table选择NESTCONCATstate、“,”城市作为位置,从选择州中选择sumintegeramount作为金额,从选择“florida”作为州中选择城市,“miami”作为城市,“coins”作为货币,40作为金额,选择“加利福尼亚”作为州,“洛杉矶”作为城市,“硬币”作为货币,40作为金额,选择“罗马尼亚”作为州,“特兰西瓦尼亚”作为城市,“硬币”作为货币,40作为金额,位置,//输入列[//输出模式{'name':'location','type':'RECORD','mode':'REPEATED','fields':[{'name':'state','type':'STRING'},{'name':'city','type':'STRING'}},{'name':'amount','type':'STRING'}],functionrow,emit{//function for var i=0;i[ { "location": [ { "state": "california", "city": "la" } ] }, { "location": [ { "state": "florida", "city": "miami" } ] }, { "location": [ { "state": "romania", "city": "transylvania" } ] } ]
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"
  }
]