Search in sources :

Example 1 with FieldValueList

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]
}
Also used : BigQuery(com.google.cloud.bigquery.BigQuery) TableResult(com.google.cloud.bigquery.TableResult) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValueList(com.google.cloud.bigquery.FieldValueList) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) JobId(com.google.cloud.bigquery.JobId)

Example 2 with FieldValueList

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();
}
Also used : FieldValueList(com.google.cloud.bigquery.FieldValueList)

Aggregations

FieldValueList (com.google.cloud.bigquery.FieldValueList)2 BigQuery (com.google.cloud.bigquery.BigQuery)1 Job (com.google.cloud.bigquery.Job)1 JobId (com.google.cloud.bigquery.JobId)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 QueryResponse (com.google.cloud.bigquery.QueryResponse)1 TableResult (com.google.cloud.bigquery.TableResult)1