如何将BigQuery JSON表中的JSON数据插入到嵌套表中?

wf82jlnq  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(94)

我有一个名为json_data的BigQuery表,其中有一个STRING类型的列json_column。此列中存储的JSON数据具有以下结构:

{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "addresses": [
    {
      "street": "123 Main St",
      "city": "New York",
      "country": "USA"
    },
    {
      "street": "456 Elm St",
      "city": "Los Angeles",
      "country": "USA"
    }
  ]
}

我想将这个JSON数据插入BigQuery中名为users的嵌套表中,该表具有以下模式:

CREATE TABLE users (
  id INT64,
  name STRING,
  email STRING,
  addresses ARRAY<STRUCT<
    street STRING,
    city STRING,
    country STRING
  >>
)

有人能指导我如何写一个SQL查询来实现这一点吗?我想从json_data表中提取JSON数据,并将其插入到具有适当结构的users表中。

**尝试:**我尝试了下面的查询,但它没有给予预期的响应:

INSERT INTO users (id, name, email, addresses)
SELECT 
  JSON_EXTRACT_SCALAR(json_column, '$.id') AS id,
  JSON_EXTRACT_SCALAR(json_column, '$.name') AS name,
  JSON_EXTRACT_SCALAR(json_column, '$.email') AS email,
  JSON_EXTRACT_ARRAY(json_column, '$.addresses') AS addresses
FROM json_data

但我得到一个错误:

Query column 4 has type ARRAY<STRING> which cannot be inserted into column addresses, which has type ARRAY<STRUCT<street STRING, city STRING, country STRING>> at [2:1]

任何帮助将不胜感激。谢谢你!

8nuwlpux

8nuwlpux1#

我认为你应该尝试下面的东西:

-- Insert the data from the source_table into the destination_table 
INSERT INTO destination_table (id, name, email, addresses) 
SELECT 
 JSON_EXTRACT_SCALAR(json_data, '$.id') AS id, 
 JSON_EXTRACT_SCALAR(json_data, '$.name') AS name, 
 JSON_EXTRACT_SCALAR(json_data, '$.email') AS email, 
 ARRAY( 
   SELECT AS STRUCT 
     JSON_EXTRACT_SCALAR(address, '$.street') AS street, 
     JSON_EXTRACT_SCALAR(address, '$.city') AS city, 
     JSON_EXTRACT_SCALAR(address, '$.country') AS country 
   FROM UNNEST(JSON_EXTRACT_ARRAY(json_data, '$.addresses')) AS address 
 ) AS addresses 
FROM source_table;

大多数事情都是显而易见的,但下面解释了结构体提取的数组:

  • 对于addresses列,使用ARRAY和SELECT AS STRUCT语法构造结构体数组。
  • 在数组构造中,JSON_EXTRACT_SCALAR再次用于从JSON数组中的每个地址对象提取值。
  • FROM UNNEST子句用于取消嵌套JSON数组,将每个地址对象视为单独的行。

相关问题