【问题标题】:Streaming JSON into BigQuery using Java使用 Java 将 JSON 流式传输到 BigQuery
【发布时间】:2017-07-31 20:30:19
【问题描述】:

我正在尝试使用类似于this page 上的教程的 Java 驱动程序将数据流式传输到 BigQuery,该驱动程序将数据从地图插入到 BigQuery 表中。 The v2 of the streaming rest API 支持在插入时将行指定为 JSON,所以我想知道是否可以使用 Java 驱动程序将 JSON 流式传输到 bigquery,而不必像下面的示例那样使用映射。

Map<String, Object> rowContent = new HashMap<>();
rowContent.put("booleanField", true);
// Bytes are passed in base64
rowContent.put("bytesField", "Cg0NDg0="); // 0xA, 0xD, 0xD, 0xE, 0xD in base64
// Records are passed as a map
Map<String, Object> recordsContent = new HashMap<>();
recordsContent.put("stringField", "Hello, World!");
rowContent.put("recordField", recordsContent);
InsertAllResponse response = bigquery.insertAll(InsertAllRequest.newBuilder(tableId)
    .addRow("rowId", rowContent)
    // More rows can be added in the same RPC by invoking .addRow() on the builder
    .build());

即有什么方法可以运行bigquery.insertAll,但传入一个 json 字符串而不是 Map?

【问题讨论】:

标签: java json google-bigquery


【解决方案1】:

最终使用 Jackson 使用 ObjectMapper 类将 JSON 字符串转换为 Map,然后使用与 Google 网站上的示例相同的方式上传到 Java 中的 BigQuery。

BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
TableId tableId = TableId.of("dataset_name", "table_name");
try {
  HashMap<String,Object> mapResult = new ObjectMapper().readValue(json_string, HashMap.class);
  InsertAllResponse response = bigquery.insertAll(InsertAllRequest.newBuilder(tableId)
    .addRow(UUID.randomUUID().toString(), mapResult)
    .build());
  if (response.hasErrors()) {
    // If any of the insertions failed, this lets you inspect the errors
    for (Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) {
      System.out.println(entry);
    }
  }
} catch (IOException e) {
  // Failed to Map JSON String
  System.out.println(e);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多