diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java index 1eef52b3a40..81a05195109 100644 --- a/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java @@ -16,34 +16,36 @@ package com.example.bigquery; -// [START all] -// [START imports] +// [START bigquery_simple_app_all] +// [START bigquery_simple_app_deps] import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.FieldValue; +import com.google.cloud.bigquery.FieldValueList; import com.google.cloud.bigquery.Job; import com.google.cloud.bigquery.JobId; import com.google.cloud.bigquery.JobInfo; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryResponse; import com.google.cloud.bigquery.QueryResult; - import java.util.List; import java.util.UUID; -// [END imports] +// [END bigquery_simple_app_deps] public class SimpleApp { public static void main(String... args) throws Exception { - // [START create_client] + // [START bigquery_simple_app_client] BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); - // [END create_client] - // [START run_query] + // [END bigquery_simple_app_client] + // [START bigquery_simple_app_query] QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder( - "SELECT " - + "APPROX_TOP_COUNT(corpus, 10) as title, " - + "COUNT(*) as unique_words " - + "FROM `bigquery-public-data.samples.shakespeare`;") + "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") // Use standard SQL syntax for queries. // See: https://cloud.google.com/bigquery/sql-reference/ .setUseLegacySql(false) @@ -64,34 +66,21 @@ public static void main(String... args) throws Exception { // 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); - // [END run_query] - // [START print_results] QueryResult result = response.getResult(); // Print all pages of the results. - while (result != null) { - for (List row : result.iterateAll()) { - List titles = row.get(0).getRepeatedValue(); - System.out.println("titles:"); - - for (FieldValue titleValue : titles) { - List titleRecord = titleValue.getRecordValue(); - String title = titleRecord.get(0).getStringValue(); - long uniqueWords = titleRecord.get(1).getLongValue(); - System.out.printf("\t%s: %d\n", title, uniqueWords); - } - - long uniqueWords = row.get(1).getLongValue(); - System.out.printf("total unique words: %d\n", uniqueWords); - } - - result = result.getNextPage(); + 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 print_results] + // [END bigquery_simple_app_print] } } -// [END all] +// [END bigquery_simple_app_all] diff --git a/bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java b/bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java index f4be05c2a95..71a01e90c15 100644 --- a/bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java +++ b/bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java @@ -18,15 +18,14 @@ import static com.google.common.truth.Truth.assertThat; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - /** Tests for simple app sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") @@ -50,6 +49,6 @@ public void tearDown() { public void testQuickstart() throws Exception { SimpleApp.main(); String got = bout.toString(); - assertThat(got).contains("hamlet"); + assertThat(got).contains("views:"); } }