【问题标题】:Loading JSON data to multiple tables in BigQuery将 JSON 数据加载到 BigQuery 中的多个表
【发布时间】:2021-03-24 19:38:20
【问题描述】:

我的 JSON 看起来像:

{
    "Key1":"Value1","Key2":"Value2","Key3":"Value3","List1":
    [
        {
            "SubKey1":"SubValue1_1","SubKey2":"SubValue1_2","SubKey3":"SubValue1_3"
        },
        {
            "SubKey1":"SubValue2_1","SubKey2":"SubValue2_2","SubKey3":"SubValue2_3"
        },
        {
            "SubKey1":"SubValue3_1","SubKey2":"SubValue3_2","SubKey3":"SubValue3_3"
        }
    ]
}

它会像这样加载到单个 BigQuery 表中:



但我希望我的数据加载到 2 个单独的表中,例如:





请指导我应该怎么做。

【问题讨论】:

    标签: json google-cloud-platform google-bigquery


    【解决方案1】:

    我认为在加载数据时直接做是不可能的。

    我建议按原样加载数据,然后执行以下查询以创建包含所需格式数据的表。 “cs_test”是表的数据集名称,“so”是加载数据的表的名称:

    CREATE TABLE cs_test.so_2 AS SELECT key1, list FROM cs_test.so AS s, unnest(s.list1) AS list;
    

    “so_2”中的数据如下所示:

    并使用如下格式的数据创建一个表:

    您需要执行以下查询:

    CREATE TABLE cs_test.so_1 AS SELECT key1, key2, key3 FROM cs_test.so;
    

    【讨论】:

      【解决方案2】:

      如果可以使用 bq 命令行就可以了。

      假设您的 JSON 文件 (my_json_file.json) 位于 GCS 存储桶(例如 my_gcs_bucket)和目标表 my_dataset.my_destination_table 中,您可以运行以下命令

      bq load --ignore_unknown_values --source_format=NEWLINE_DELIMITED_JSON my_dataset.my_destination_table "gs://my_gcs_bucket/my_json_file.json" ./schema.json
      

      在 schema.json 中,您已经选择了目标表的架构。例如,以下两个模式将按预期加载数据:

      schema_1.json

      [
        {
          "mode": "NULLABLE",
          "name": "Key1",
          "type": "STRING"
        },
        {
          "mode": "NULLABLE",
          "name": "Key2",
          "type": "STRING"
        },
        {
          "mode": "NULLABLE",
          "name": "Key3",
          "type": "STRING"
        }
      ]
      

      schema_2.json

      [
        {
          "mode": "NULLABLE",
          "name": "Key1",
          "type": "STRING"
        },
        {
          "fields": [
            {
              "mode": "NULLABLE",
              "name": "SubKey1",
              "type": "STRING"
            },
            {
              "mode": "NULLABLE",
              "name": "SubKey2",
              "type": "STRING"
            },
            {
              "mode": "NULLABLE",
              "name": "SubKey3",
              "type": "STRING"
            }
          ],
          "mode": "REPEATED",
          "name": "List1",
          "type": "RECORD"
        }
      ]
      

      然后

      bq load --ignore_unknown_values --source_format=NEWLINE_DELIMITED_JSON my_dataset.my_destination_table_1 "gs://my_gcs_bucket/my_json_file.json" ./schema_1.json
      
      bq load --ignore_unknown_values --source_format=NEWLINE_DELIMITED_JSON my_dataset.my_destination_table_2 "gs://my_gcs_bucket/my_json_file.json" ./schema_2.json
      

      将基于同一个 JSON 文件加载两个不同的表

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-29
        • 1970-01-01
        • 2019-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        相关资源
        最近更新 更多