简体   繁体   English

将嵌套值提取为列 Google BigQuery?

[英]Extract nested values as columns Google BigQuery?

I have a table with nested values, like the following:我有一个包含嵌套值的表,如下所示: 在此处输入图像描述

I'd like to grab the values, with keys as columns without multiple cross joins.我想获取值,将键作为没有多个交叉连接的列。 ie IE

SELECT 
owner_id, 
owner_type, 
domain, 
metafields.value AS name, 
metafields.value AS image, 
metafields.value AS location, 
metafields.value AS draw
FROM 
example_table

Obviously, the above won't work for this, but the following output would be desired:显然,以上内容不适用于此,但需要以下 output:

在此处输入图像描述

In the actual table there are hundreds of metafields per owner_id, and hundreds of owner_ids, and owner_types.在实际表中,每个 owner_id 有数百个元字段,还有数百个 owner_id 和 owner_types。 Multiple joins to other tables for owner_types is fine, but for the same owner type, I don't want to have to join multiple times. owner_types 与其他表的多次连接很好,但对于相同的所有者类型,我不想多次连接。

Basically, I need to be able to select the key to which the column corresponds, and display the relevant value for that column.基本上,我需要能够 select 列对应的键,并显示该列的相关值。 Without, having to display every metafield available.没有,必须显示每个可用的元字段。

Any way of doing this?有什么办法吗?

You can use the subqueries and SAFE_offset statement and get a value from an array at a specific location.您可以使用子查询和SAFE_offset语句并从特定位置的数组中获取值。 Also, you need to use STRING_AGG , which returns a value (either STRING or BYTES) obtained by concatenating non-null values.此外,您需要使用STRING_AGG ,它返回通过连接非空值获得的值(STRING 或 BYTES)。

With the information you shared, you can use the query below.根据您分享的信息,您可以使用下面的查询。

With this code, you will get all the columns separated by a comma:使用此代码,您将获得用逗号分隔的所有列:

WITH sequences AS
 (
     SELECT 1 as ID,"product" AS owner_type,"beta.com" AS domain,["name","image","lcation","draw"] AS metalfields_key, ["big","pic.png","utha","1"] AS metalfields_value
     ),
 Val as(
 SELECT distinct id, owner_type,domain, value FROM sequences, sequences.metalfields_value as value, sequences.metalfields_key
 ), text as(
 SELECT
 id, owner_type, domain,
 STRING_AGG(value ORDER BY value) AS Text
FROM Val 
GROUP BY owner_type, domain, id
 )
 

In this code, you will get each element that is separated by a comma and return them by columns.在此代码中,您将获取以逗号分隔的每个元素并按列返回它们。

SELECT DISTINCT t1.id, t1.owner_type,domain,
split(t1.text, ',')[SAFE_offset(1)] as name,
split(t1.text, ',')[SAFE_offset(2)] as image,
split(t1.text, ',')[SAFE_offset(3)] as location,
split(t1.text, ',')[SAFE_offset(0)] as draw
from text as t1

You can see the result.你可以看到结果。

在此处输入图像描述

Consider below approach考虑以下方法

select * except(id) from (
  select t.* except(metafields), 
    to_json_string(t) id, key, value
  from your_table t, unnest(metafields) kv
)
pivot (min(value) for key in ('name', 'image', 'location', 'draw'))          

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM