8000 BigQuery: rewrite simple app tutorial. (#950) · abhiravredox/java-docs-samples@23a9b03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23a9b03

Browse files
authored
BigQuery: rewrite simple app tutorial. (GoogleCloudPlatform#950)
- Add region tags for needed dependencies. - Use more relevant query from public datasets.
1 parent 453ce56 commit 23a9b03

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,36 @@
1616

1717
package com.example.bigquery;
1818

19-
// [START all]
20-
// [START imports]
19+
// [START bigquery_simple_app_all]
20+
// [START bigquery_simple_app_deps]
2121
import com.google.cloud.bigquery.BigQuery;
2222
import com.google.cloud.bigquery.BigQueryOptions;
2323
import com.google.cloud.bigquery.FieldValue;
24+
import com.google.cloud.bigquery.FieldValueList;
2425
import com.google.cloud.bigquery.Job;
2526
import com.google.cloud.bigquery.JobId;
2627
import com.google.cloud.bigquery.JobInfo;
2728
import com.google.cloud.bigquery.QueryJobConfiguration;
2829
import com.google.cloud.bigquery.QueryResponse;
2930
import com.google.cloud.bigquery.QueryResult;
30-
3131
import java.util.List;
3232
import java.util.UUID;
33-
// [END imports]
33+
// [END bigquery_simple_app_deps]
3434

3535
public 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]

bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
2123
import org.junit.After;
2224
import org.junit.Before;
2325
import org.junit.Test;
2426
import org.junit.runner.RunWith;
2527
import org.junit.runners.JUnit4;
2628

27-
import java.io.ByteArrayOutputStream;
28-
import java.io.PrintStream;
29-
3029
/** Tests for simple app sample. */
3130
@RunWith(JUnit4.class)
3231
@SuppressWarnings("checkstyle:abbreviationaswordinname")
@@ -50,6 +49,6 @@ public void tearDown() {
5049
public void testQuickstart() throws Exception {
5150
SimpleApp.main();
5251
String got = bout.toString();
53-
assertThat(got).contains("hamlet");
52+
assertThat(got).contains("views:");
5453
}
5554
}

0 commit comments

Comments
 (0)
0