3

我有多个 JSON 文件。这些文件有两个嵌套字段。这些文件每天生成,因此我需要在 BigQuery 表中执行每日插入和更新操作。我在图像中共享了表模式。

如何对嵌套字段执行更新操作?

BigQuery 数据集

4

2 回答 2

5

有点晚了,但以防其他人正在搜索。如果您可以使用标准 SQL:

INSERT INTO your_table (optout_time, clicks, profile_id, opens, ... ) 
VALUES (
  1552297347, 
  [
   STRUCT(1539245347 as ts, 'url1' as url), 
   STRUCT(1539245341 as ts, 'url2' as url)
  ], 
  'whatever', 
  [ 
   STRUCT(1539245347 as ts), 
   STRUCT(1539245341 as ts)
  ], 
  ...
)
于 2018-11-28T16:08:21.913 回答
1

BigQuery UI 仅提供 JSON 导入以创建新表。因此,要将文件内容流式传输到现有的 BigQuery 表中,您可以使用客户端库以您喜欢的编程语言编写一个小程序。

我将假设您将数据作为行分隔的 JSON 格式,如下所示:

 {"optout_time": 1552297349, "clicks": {"ts": 1539245349, "url": "www.google.com"}, "profile_id": "foo", ...}
 {"optout_time": 1532242949, "clicks": {"ts": 1530247349, "url": "www.duckduckgo.com"}, "profile_id": "bar", ...}

作业的 python 脚本看起来像这样。它将 json 文件名作为命令行参数:

import json
import sys

from google.cloud import bigquery


dataset_id = "<DATASET-ID>"  # the ID of your dataset
table_id = "<TABLE-ID>"  # the ID of your table

client = bigquery.Client()
table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref)

for f in sys.argv[1:]:
    with open(f) as fh:
        data = [json.loads(x) for x in fh]
        client.insert_rows_json(table, data)

嵌套是自动处理的。

有关此类操作在其他语言中的外观的指针,您可以查看此文档

于 2018-09-06T15:35:49.440 回答