我有一个BigQuery表' metrics ',该表具有嵌套的记录字段,其中一个字段称为 resource.labels ,其键对值如下所示{{3 }}
我需要更新resource.labels这是一个重复的记录字段,并且在这种情况下具有键值对resource.labels.key和resource.labels.value
where key="machinetype" and value="N/A" set value="g1.small"
我已经尝试过了
update `metrics` set resource.labels.value=ARRAY(SELECT AS STRUCT * REPLACE("g1.small" as value) from UNNEST(resource.labels) as h where h.key="machinetype" and h.value="N/A") where resource.labels.key="machinetype" and resource.labels.value="N/A"
但出现此错误:
Cannot access field key on a value with type ARRAY<STRUCT<key STRING, value STRING>> at [1:241]
有人可以提出任何建议吗?谢谢。
答案 0 :(得分:2)
尝试以下方法-应该可以
UPDATE `project.dataset.metrics`
SET resource = (
SELECT AS STRUCT * REPLACE(
ARRAY(
SELECT AS STRUCT key,
IF(key='machinetype' and value='N/A', 'g1.small', value) as value
FROM t.labels
) AS labels)
FROM UNNEST([resource]) t
)
WHERE true