【问题标题】:How to create new table with nested schema entirely in BigQuery如何在 BigQuery 中完全创建具有嵌套架构的新表
【发布时间】:2015-02-17 08:28:27
【问题描述】:

我在 BigQuery 中有一个嵌套表 A,其架构如下:

    {
    "name": "page_event",
    "mode": "repeated",
    "type": "RECORD",
    "fields": [
        {
            "name": "id",
            "type": "STRING"
        }
    ]
    }

我想用其他表中的数据丰富表 A 并将结果保存为新的嵌套表。假设我想将“描述”字段添加到表 A(创建表 B),所以我的架构如下:

    {
    "name": "page_event",
    "mode": "repeated",
    "type": "RECORD",
    "fields": [
        {
            "name": "id",
            "type": "STRING"
        },
        {
            "name": "description",
            "type": "STRING"
        }
    ]
    }

如何在 BigQuery 中执行此操作?似乎在 BigQuery SQL 中没有用于创建嵌套结构的函数(生成列表的 NEST 函数除外 - 但此函数似乎不起作用,因意外错误而失败)

我能想到的唯一方法是:

  • 使用字符串连接函数生成表 B,其中包含名为“json”的单个字段,内容是来自 A 的丰富数据,转换为 json 字符串
  • 将 B 作为一组文件 F 导出到 GCS
  • 将 F 加载为表 C

有没有更简单的方法?

【问题讨论】:

    标签: sql json nested google-bigquery


    【解决方案1】:

    为了丰富现有表的架构,可以使用表补丁 API
    https://cloud.google.com/bigquery/docs/reference/v2/tables/patch

    请求如下所示

    PATCH https://www.googleapis.com/bigquery/v2/projects/{project_id}/datasets/{dataset_id}/tables/{table_id}?key={YOUR_API_KEY}
    
    {
     "schema": {
      "fields": [
       {
        "name": "page_event",
        "mode": "repeated",
        "type": "RECORD",
        "fields": [
         {
          "name": "id",
          "type": "STRING"
         },
         {
          "name": "description",
          "type": "STRING"
         }
        ]
       }
      ]
     }
    }
    

    补丁前

    补丁后

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      相关资源
      最近更新 更多