16
16
17
17
package com .example .bigquery ;
18
18
19
- // [START all ]
20
- // [START imports ]
19
+ // [START bigquery_simple_app_all ]
20
+ // [START bigquery_simple_app_deps ]
21
21
import com .google .cloud .bigquery .BigQuery ;
22
22
import com .google .cloud .bigquery .BigQueryOptions ;
23
23
import com .google .cloud .bigquery .FieldValue ;
24
+ import com .google .cloud .bigquery .FieldValueList ;
24
25
import com .google .cloud .bigquery .Job ;
25
26
import com .google .cloud .bigquery .JobId ;
26
27
import com .google .cloud .bigquery .JobInfo ;
27
28
import com .google .cloud .bigquery .QueryJobConfiguration ;
28
29
import com .google .cloud .bigquery .QueryResponse ;
29
30
import com .google .cloud .bigquery .QueryResult ;
30
-
31
31
import java .util .List ;
32
32
import java .util .UUID ;
33
- // [END imports ]
33
+ // [END bigquery_simple_app_deps ]
34
34
35
35
public class SimpleApp {
36
36
public static void main (String ... args ) throws Exception {
37
- // [START create_client ]
37
+ // [START bigquery_simple_app_client ]
38
38
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 ]
41
41
QueryJobConfiguration queryConfig =
42
42
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" )
47
49
// Use standard SQL syntax for queries.
48
50
// See: https://cloud.google.com/bigquery/sql-reference/
49
51
.setUseLegacySql (false )
@@ -64,34 +66,21 @@ public static void main(String... args) throws Exception {
64
66
// errors, not just the latest one.
65
67
throw new RuntimeException (queryJob .getStatus ().getError ().toString ());
66
68
}
69
+ // [END bigquery_simple_app_query]
67
70
71
+ // [START bigquery_simple_app_print]
68
72
// Get the results.
69
73
QueryResponse response = bigquery .getQueryResults (jobId );
70
- // [END run_query]
71
74
72
- // [START print_results]
73
75
QueryResult result = response .getResult ();
74
76
75
77
// 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 );
93
82
}
94
- // [END print_results ]
83
+ // [END bigquery_simple_app_print ]
95
84
}
96
85
}
97
- // [END all ]
86
+ // [END bigquery_simple_app_all ]
0 commit comments