2018年8月1日水曜日

BigQueryを使う(3) BigQueryへ格納

前回JavaからBigQueryにアクセスし、テーブル情報を集計しました。当初その結果はMySQLに格納しましたが、集計情報もデータ数が多くMySQLから取り出すことが大変だったので、BigQueryにStreaming Insertを使って格納しました。

(1) テーブル作成関数
データ格納用のテーブルを作成しました。


(2) データ格納関数
public void insertTotals(TableResult response, int index){
System.out.println("Totals : Insert開始");
for (FieldValueList row : response.iterateAll()) {
Map<String, Object> rowvalue = new HashMap<>();
for(int i = 0; i < column; i++) {
rowvalue.put("sum"+i, row.get("sum"+i).getValue());
}
rowvalue.put("index", index);
InsertAllRequest insertRequest = InsertAllRequest.newBuilder(tableId).addRow(rowvalue).build();
InsertAllResponse insertResponse = bigQuery.insertAll(insertRequest);
}
}
view raw insertTotals hosted with ❤ by GitHub


全体
https://github.com/smzn/BigQuery
シンプル
https://github.com/smzn/BigQuery_Insert

Streamingはデータが入るまである程度時間がかかるので、データが格納されていなくても間違いではないので注意。

0 件のコメント:

コメントを投稿