2017-07-31 44 views
0

我試圖使用類似於this page教程的Java驅動程序將數據流式傳輸到BigQuery,該教程將數據從地圖插入到BigQuery表中。 The v2 of the streaming rest API支持在插入時將行指定爲JSON,所以我想知道是否可以使用Java驅動程序將JSON流式傳輸到bigquery,而不必像下面的示例那樣使用映射。使用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?

+1

有肯定似乎是在C#:https://cloud.google.com/bigquery/streaming-data-into-bigquery#bigquery-stream-data-csharp 我不認爲在Java中,通過快速搜索找不到 –

回答

0

最終使用Jackson將JSON字符串轉換爲使用ObjectMapper類的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); 
} 
相關問題