1616
1717package com .example .bigquery ;
1818
19- // [START all ]
20- // [START imports ]
19+ // [START bigquery_simple_app_all ]
20+ // [START bigquery_simple_app_deps ]
2121import com .google .cloud .bigquery .BigQuery ;
2222import com .google .cloud .bigquery .BigQueryOptions ;
2323import com .google .cloud .bigquery .FieldValue ;
24+ import com .google .cloud .bigquery .FieldValueList ;
2425import com .google .cloud .bigquery .Job ;
2526import com .google .cloud .bigquery .JobId ;
2627import com .google .cloud .bigquery .JobInfo ;
2728import com .google .cloud .bigquery .QueryJobConfiguration ;
2829import com .google .cloud .bigquery .QueryResponse ;
2930import com .google .cloud .bigquery .QueryResult ;
30-
3131import java .util .List ;
3232import java .util .UUID ;
33- // [END imports ]
33+ // [END bigquery_simple_app_deps ]
3434
3535public class SimpleApp {
3636 public static void main (String ... args ) throws Exception {
37- // [START create_client ]
37+ // [START bigquery_simple_app_client ]
3838 BigQuery bigquery = BigQueryOptions .getDefaultInstance ().getService ();
39- // [END create_client ]
40- // [START run_query ]
39+ // [END bigquery_simple_app_client ]
40+ // [START bigquery_simple_app_query ]
4141 QueryJobConfiguration queryConfig =
4242 QueryJobConfiguration .newBuilder (
43- "SELECT "
44- + "APPROX_TOP_COUNT(corpus, 10) as title, "
45- + "COUNT(*) as unique_words "
46- + "FROM `bigquery-public-data.samples.shakespeare`;" )
43+ "SELECT "
44+ + "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, "
45+ + "view_count "
46+ + "FROM `bigquery-public-data.stackoverflow.posts_questions` "
47+ + "WHERE tags like '%google-bigquery%' "
48+ + "ORDER BY favorite_count DESC LIMIT 10" )
4749 // Use standard SQL syntax for queries.
4850 // See: https://cloud.google.com/bigquery/sql-reference/
4951 .setUseLegacySql (false )
@@ -64,34 +66,21 @@ public static void main(String... args) throws Exception {
6466 // errors, not just the latest one.
6567 throw new RuntimeException (queryJob .getStatus ().getError ().toString ());
6668 }
69+ // [END bigquery_simple_app_query]
6770
71+ // [START bigquery_simple_app_print]
6872 // Get the results.
6973 QueryResponse response = bigquery .getQueryResults (jobId );
70- // [END run_query]
7174
72- // [START print_results]
7375 QueryResult result = response .getResult ();
7476
7577 // Print all pages of the results.
76- while (result != null ) {
77- for (List <FieldValue > row : result .iterateAll ()) {
78- List <FieldValue > titles = row .get (0 ).getRepeatedValue ();
79- System .out .println ("titles:" );
80-
81- for (FieldValue titleValue : titles ) {
82- List <FieldValue > titleRecord = titleValue .getRecordValue ();
83- String title = titleRecord .get (0 ).getStringValue ();
84- long uniqueWords = titleRecord .get (1 ).getLongValue ();
85- System .out .printf ("\t %s: %d\n " , title , uniqueWords );
86- }
87-
88- long uniqueWords = row .get (1 ).getLongValue ();
89- System .out .printf ("total unique words: %d\n " , uniqueWords );
90- }
91-
92- result = result .getNextPage ();
78+ for (FieldValueList row : result .iterateAll ()) {
79+ String url = row .get ("url" ).getStringValue ();
80+ long viewCount = row .get ("view_count" ).getLongValue ();
81+ System .out .printf ("url: %s views: %d%n" , url , viewCount );
9382 }
94- // [END print_results ]
83+ // [END bigquery_simple_app_print ]
9584 }
9685}
97- // [END all ]
86+ // [END bigquery_simple_app_all ]
0 commit comments