2018年7月27日金曜日

BigQueryを使う(2)

BigQueryをJavaから利用してみます。
(1) 認証用jsonファイルの作成
https://cloud.google.com/bigquery/docs/reference/libraries
を参照してEclipseでJavaのプログラムを作ります。今回はGradleのプロジェクトを作ります。
build.gradleに設定をします。マニュアルとは内容が違うので注意。
compile 'com.google.cloud:google-cloud-bigquery:+'

Refreshをするとライブラリが取り込まれます。




(2) 認証の設定
https://cloud.google.com/bigquery/docs/reference/libraries
の通り認証の設定をしてjsonファイルをダウンロードします。
ダウンロードしたjsonファイルを後で指定します。
(今回はマニュアルで載っているexportではうまくいかなかったです)

(3) Java作成
main関数付きのクラスを作成します。
ソースファイル
https://github.com/smzn/BigQuery
package bigquery;
import java.io.FileInputStream;
import java.io.IOException;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.JobException;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
public class BigQuery_main {
public static void main(String args[]) throws IOException {
BigQuery bigQuery = getClientWithJsonKey("/Users/mizuno/Downloads/closedqueue-929a267e03b8.json");
String query = "select sum(int64_field_0) as sum0, sum(int64_field_1) as sum1,sum(int64_field_2) as sum2 from `mznfe.Fe_OFF_00000*` where int64_field_2048 = 1";
QueryJobConfiguration queryConfig = QueryJobConfiguration.of(query);
TableResult response = null;
try {
response = bigQuery.query(queryConfig);
} catch (JobException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (FieldValueList row : response.iterateAll()) {
System.out.println(row.get("sum0").getValue() + " " + row.get("sum1").getValue() + " " + row.get("sum2").getValue());
}
}
private static BigQuery getClientWithJsonKey(String key) throws IOException {
return BigQueryOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(key)))
.build()
.getService();
}
}

実行すると 、結果が得られます。




0 件のコメント:

コメントを投稿