google-bigquery - 如何处理bigquery中的动态模式

标签 google-bigquery

我的数据如下所示: 第 1 行 - {"id": "1", "object": "user","metadata": {"name": "1234"}} 第 2 行 - {"id": "1", "object": "user","metadata": {"name": "1234","email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1a19183b1a191855181416" rel="noreferrer noopener nofollow">[email protected]</a>"}}

我使用第 1 行创建了表格 metadata RECORD NULLABLE metadata.tenant STRING NULLABLE object STRING NULLABLE id STRING NULLABLE

但是我的插入将在第 2 行失败。我的架构应该是什么样子才能处理元数据字段中的更改?

最佳答案

对于您的问题中显示的示例 - 我会使用以下架构

[
  {
    "name": "id",
    "type": "INTEGER",
    "mode": "NULLABLE"
  },
  {
    "name": "object",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "metadata",
    "type": "STRING",
    "mode": "NULLABLE"
  }
]   

下面是我如何处理它的示例

#standardSQL
WITH `yourProject.yourDataset.yourTable` AS (
  SELECT 1 AS id, 'user' AS object, '{"name": "BI Architect", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbdad9d8fbdad9d895d8d4d6" rel="noreferrer noopener nofollow">[email protected]</a>"}' AS metadata UNION ALL
  SELECT 2, 'expert', '{"name": "Elliott Brossard"}'
)
SELECT 
  id, 
  object, 
  JSON_EXTRACT_SCALAR(metadata, '$.name') AS name,
  JSON_EXTRACT_SCALAR(metadata, '$.email') AS email 
FROM `yourProject.yourDataset.yourTable` 
ORDER BY id

产生以下输出

id  object  name                email    
1   user    BI Architect        <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="08696a6b48696a6b266b6765" rel="noreferrer noopener nofollow">[email protected]</a>  
2   expert  Elliott Brossard    null     

关于google-bigquery - 如何处理bigquery中的动态模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45178304/

相关文章:

google-analytics - `fullVisitorId` => clientId,一对多映射?

google-bigquery - 成功创建表后不会清除目标表

google-cloud-platform - 有没有办法在 Google Cloud Platform 中安排 Cloud Shell 命令?

sql - 如何使用 BigQuery 旧版 SQL 查询同一重复字符串字段的多个值?

javascript - 在 BigQuery 中将 API 调用作为 UDF 的一部分 - 可能吗?

python - BigQuery : result set in UI and from API have different number of rows. 为什么?

google-bigquery - 大查询-水平连接字符串

sql - BigQuery - 查找 N 个最近的向量

google-bigquery - 从 BigQuery 导出到 Google Cloud Datastore

google-apps-script - 在电子表格中存档数据