从 BigQuery 列中提取 Json 值
Posted
技术标签:
【中文标题】从 BigQuery 列中提取 Json 值【英文标题】:Extract Json value from BigQuery column 【发布时间】:2021-06-14 09:05:11 【问题描述】:我有一列包含 JSON,例如:
"overview":
"resourceName": "VM-test",
"recommendedAction": "Change machine type",
"resource": "//compute.googleapis.com/projects/test/zones/europe-west2-c/instances/VM-test",
"currentMachineType":
"cpuMilliVcores": 8000.0,
"memoryBytes": 3.221225472E10,
"name": "n1-standard-8",
"memoryMb": 30720.0,
"guestCpus": 8.0
,
"location": "europe-west2-c",
"recommendedMachineType":
"cpuMilliVcores": 4000.0,
"name": "custom-4-23552",
"memoryBytes": 2.4696061952E10,
"memoryMb": 23552.0,
"guestCpus": 4.0
我想将"resourceName":
及其值提取到另一个名为resourceName
的列中,并将"resource": "//compute.googleapis.com/projects/
test/europe-west2-c/instances/VM-test"
提取到另一个名为ProjectName
的列中,该列将保存位于 Uri 内的项目名称。我查看了 JSON_EXTRACT 但无法弄清楚。
任何帮助将不胜感激。
谢谢
【问题讨论】:
【参考方案1】:考虑下面
select
json_value(text, '$.overview.resourceName') as resourceName,
regexp_extract(json_value(text, '$.overview.resource'), r'/projects/([^/]+)') as projectName
from `project.dataset.table`
如果应用于您问题中的样本数据 - 输出是
【讨论】:
【参考方案2】:试试json_value:
with mytable as (
select '''
"overview":
"resourceName": "VM-test",
"recommendedAction": "Change machine type",
"resource": "//compute.googleapis.com/projects/test/zones/europe-west2-c/instances/VM-test",
"currentMachineType":
"cpuMilliVcores": 8000.0,
"memoryBytes": 3.221225472E10,
"name": "n1-standard-8",
"memoryMb": 30720.0,
"guestCpus": 8.0
,
"location": "europe-west2-c",
"recommendedMachineType":
"cpuMilliVcores": 4000.0,
"name": "custom-4-23552",
"memoryBytes": 2.4696061952E10,
"memoryMb": 23552.0,
"guestCpus": 4.0
''' as text
)
select
json_value(text, '$.overview.resourceName') as resourceName,
json_value(text, '$.overview.resource') as projectName,
from mytable
【讨论】:
嗨,谢尔盖,非常感谢您的回复,您知道我可以从 uri 中提取“test”值并将其放在 projectName 列下吗? 是的,试试regexp_extract以上是关于从 BigQuery 列中提取 Json 值的主要内容,如果未能解决你的问题,请参考以下文章
Bigquery:是不是有一种 json 路径方法可以仅从具有动态键的 json 数组中提取值?