我的表有一个 JSON 字段,如下所示。每个条目中可以有任意数量的评论。
{
"entry": 1234,
"comment_6789": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello world"
},
"comment_4564564": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello mars"
}
}
看完之后这个问题 https://stackoverflow.com/questions/52120182/bigquery-json-extract-all-elements-from-an-array我知道我可以使用 JSON_EXTRACT_ARRAY,但我有点不确定如何定位每个comment_
条目,因为它们有不同的 id?
考虑下面
#standardSQL
with `project.dataset.table` as (
select 1 id, '''
{
"entry": 1234,
"comment_6789": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello world"
},
"comment_4564564": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello mars"
}
}
''' col
)
select id, comment,
json_value(kv, '$._seconds') seconds,
json_value(kv, '$._nanoseconds') nanoseconds,
json_value(kv, '$.message') message
from `project.dataset.table`,
unnest(regexp_extract_all(col, r'comment_\d+')) comment,
unnest([struct(regexp_extract(col, r'"' || comment || '": ({[^{}]+})') as kv)])
带输出
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)