use of com.google.cloud.bigquery.FieldValueList in project java-docs-samples by GoogleCloudPlatform.
the class SimpleApp method main.
public static void main(String... args) throws Exception {
// [START bigquery_simple_app_client]
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
// [END bigquery_simple_app_client]
// [START bigquery_simple_app_query]
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder("SELECT " + "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, " + "view_count " + "FROM `bigquery-public-data.stackoverflow.posts_questions` " + "WHERE tags like '%google-bigquery%' " + "ORDER BY favorite_count DESC LIMIT 10").setUseLegacySql(false).build();
// Create a job ID so that we can safely retry.
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
// Wait for the query to complete.
queryJob = queryJob.waitFor();
// Check for errors
if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
// errors, not just the latest one.
throw new RuntimeException(queryJob.getStatus().getError().toString());
}
// [END bigquery_simple_app_query]
// [START bigquery_simple_app_print]
// Get the results.
QueryResponse response = bigquery.getQueryResults(jobId);
TableResult result = queryJob.getQueryResults();
// Print all pages of the results.
for (FieldValueList row : result.iterateAll()) {
String url = row.get("url").getStringValue();
long viewCount = row.get("view_count").getLongValue();
System.out.printf("url: %s views: %d%n", url, viewCount);
}
// [END bigquery_simple_app_print]
}
use of com.google.cloud.bigquery.FieldValueList in project java-docs-samples by GoogleCloudPlatform.
the class BigQueryHome method convertRunToHtmlTable.
private static String convertRunToHtmlTable(TableResult result) {
if (result == null) {
return "";
}
StringBuilder sb = new StringBuilder();
for (FieldValueList row : result.iterateAll()) {
sb.append("<tr>");
String url = row.get("url").getStringValue();
addColumn(sb, String.format("<a href=\"%s\">%s</a>", url, url));
addColumn(sb, row.get("view_count").getLongValue());
sb.append("</tr>");
}
return sb.toString();
}
Aggregations