public String query(String query) {
try {
File file = new File("src/main/resources/credentials/gcp_key.json");
String absolutePath = file.getAbsolutePath();
Path keyPath = Paths.get(absolutePath);
BigQuery bigquery = BigQueryOptions.newBuilder()
.setCredentials(
ServiceAccountCredentials.fromStream(new FileInputStream(keyPath.toFile()))
).build().getService();
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
TableResult results = bigquery.query(queryConfig);
Iterable<FieldValueList> iterables = results.iterateAll();
String count = "";
for (FieldValueList row : iterables) {
for (FieldValue v : row) {
System.out.println("Query performed successfully.");
count = v.getValue().toString();
}
}
return count;
} catch (BigQueryException | InterruptedException | IllegalArgumentException | IOException e) {
System.out.println("Query not performed \n" + e.toString());
return "";
}
}