2017-07-31 11 views
0

マップからBigQueryテーブルにデータを挿入するthis pageのチュートリアルのようなJavaドライバを使用してBigQueryにデータをストリーミングしようとしています。 The v2 of the streaming rest APIは、挿入時にJSONとして行を指定することをサポートしていますので、以下の例のようなマップを使用するのではなく、Javaドライバを使用してJSONをbigqueryにストリームできますか?Javaを使用してBigQueryにJSONをストリーミングする

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文字列ではなく、地図に渡すためにいくつかの方法がありますか?

+1

間違いなくC#でがあるようです:https://cloud.google.com/bigquery/streaming-data-into-bigquery#bigquery-stream-data-csharp は、私はJavaではないと思います、クイック検索で見つからなかった –

答えて

0

ObjectMapperクラスを使用してJSON文字列をMapに変換してから、JavaのBigQueryにストリーミングするためにGoogleのサイトの例と同じ方法でアップロードします。

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); 
} 
関連する問題