Struct 中的所有值到大查询中的列

All the values inside Struct to columns in big query

我是 bigquery 的新手,我有一个列 'device',其中包含嵌套的结构属性,如下所示

设备

brand_name brand_id country
huawei 34j0 china
brand_name brand_id country
sony ds5g japan

我们可以通过这样做将这些设备属性转换为列 `

SELECT
device.brandname 
device.brand_id 
device.country 
from table

(数据的结构是 STRUCT

但问题是我们无法访问设备中的每个单独元素(如 device.brandname device.brand_id.....等)如果属性有n个键,有没有办法一次循环设备列中的所有键,谢谢

我假设您有一个架构,其中“设备”是一个嵌套结构,类似于...(父字段只是举例)

Schema: parent_schema
|- parent_schema_id: string
|- device_owner: string
|- ... other properties ...
+- device: record
|  |- brand_id: string
|  |- brand_name: string
|  |- country: string
:

基于该假设,您应该能够编写如下查询...

SELECT
    parent_schema_ia,
    device_owner,
    device.brand_id,
    device.brand_name,
    device.country
  FROM parent_schema;

您还可以在 WHERE 子句中引用 device 列,例如...

SELECT
    parent_schema_ia,
    device_owner,
    device.brand_id,
    device.brand_name,
    device.country
  FROM parent_schema
 WHERE device.brand_name = 'huawei';

Google BigQuery 帮助中很好地呈现了所有内容...

https://cloud.google.com/bigquery/docs/legacy-nested-repeated

抱歉,如果我的假设不正确...我不清楚你的问题更宽的结构是什么样的。

is there a way to loop all the keys in device column in one go

使用以下方法

select * except(device), device.*
from your_table    

如果 device 是您的 table 中的唯一列 - 使用下面的

select device.*
from your_table          

有输出