如何将嵌套记录更新为静态值

时间:2020-02-25 07:00:29

标签: google-bigquery

我有嵌套数据,并且想更新实际上为数组格式user_properties['first_open_time']['int_value']的数组键的值。我想将此值更改为静态值-1582243200000,即-。数据的样本结构如下:

    [
      {
        "user_properties": [
          {
            "key": "ga_session_id",
            "value": {
              "string_value": null,
              "int_value": "1582306435",
              "float_value": null,
              "double_value": null,
              "set_timestamp_micros": "1582306435527000"
            }
          },
          {
            "key": "ga_session_number",
            "value": {
              "string_value": null,
              "int_value": "1",
              "float_value": null,
              "double_value": null,
              "set_timestamp_micros": "1582306435527000"
            }
          },
          {
            "key": "first_open_time",
            "value": {
              "string_value": null,
              "int_value": "1582308000000",
              "float_value": null,
              "double_value": null,
              "set_timestamp_micros": "1582306432489000"
            }
          }
        ]
      }
    ]

1 个答案:

答案 0 :(得分:1)

您基本上需要重建结构,更改过程中要更改的内容:

-- CREATE TABLE `temp.firebase_sample`
-- AS (
-- SELECT * FROM `bingo-blast-174dd.analytics_151321511.events_20200225`
-- );

UPDATE `temp.firebase_sample` a
SET user_properties = (
  SELECT
    ARRAY_AGG(STRUCT(key,
        STRUCT(value.string_value,
          IF(key='first_open_time', 1582243200000, value.int_value),
          value.float_value,
          value.double_value,
          value.set_timestamp_micros)))
  FROM UNNEST(a.user_properties) x)
WHERE TRUE