|
20 | 20 | // [START bigquery_simple_app_deps] |
21 | 21 |
|
22 | 22 | import com.google.cloud.bigquery.BigQuery; |
| 23 | +import com.google.cloud.bigquery.BigQueryException; |
23 | 24 | import com.google.cloud.bigquery.BigQueryOptions; |
24 | 25 | import com.google.cloud.bigquery.FieldValueList; |
25 | 26 | import com.google.cloud.bigquery.Job; |
26 | 27 | import com.google.cloud.bigquery.JobId; |
27 | 28 | import com.google.cloud.bigquery.JobInfo; |
28 | 29 | import com.google.cloud.bigquery.QueryJobConfiguration; |
29 | 30 | import com.google.cloud.bigquery.TableResult; |
30 | | -import java.util.UUID; |
31 | 31 |
|
32 | 32 | // [END bigquery_simple_app_deps] |
33 | 33 |
|
34 | 34 | public class SimpleApp { |
| 35 | + |
35 | 36 | public static void main(String... args) throws Exception { |
36 | | - // [START bigquery_simple_app_client] |
37 | | - BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
38 | | - // [END bigquery_simple_app_client] |
39 | | - // [START bigquery_simple_app_query] |
40 | | - QueryJobConfiguration queryConfig = |
41 | | - QueryJobConfiguration.newBuilder( |
42 | | - "SELECT CONCAT('https://stackoverflow.com/questions/', " |
43 | | - + "CAST(id as STRING)) as url, view_count " |
44 | | - + "FROM `bigquery-public-data.stackoverflow.posts_questions` " |
45 | | - + "WHERE tags like '%google-bigquery%' " |
46 | | - + "ORDER BY view_count DESC " |
47 | | - + "LIMIT 10") |
48 | | - // Use standard SQL syntax for queries. |
49 | | - // See: https://cloud.google.com/bigquery/sql-reference/ |
50 | | - .setUseLegacySql(false) |
51 | | - .build(); |
| 37 | + // TODO(developer): Replace these variables before running the app. |
| 38 | + String projectId = "MY_PROJECT_ID"; |
| 39 | + simpleApp(projectId); |
| 40 | + } |
52 | 41 |
|
53 | | - // Create a job ID so that we can safely retry. |
54 | | - JobId jobId = JobId.of(UUID.randomUUID().toString()); |
55 | | - Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build()); |
| 42 | + public static void simpleApp(String projectId) { |
| 43 | + try { |
| 44 | + // [START bigquery_simple_app_client] |
| 45 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 46 | + // [END bigquery_simple_app_client] |
| 47 | + // [START bigquery_simple_app_query] |
| 48 | + QueryJobConfiguration queryConfig = |
| 49 | + QueryJobConfiguration.newBuilder( |
| 50 | + "SELECT CONCAT('https://stackoverflow.com/questions/', " |
| 51 | + + "CAST(id as STRING)) as url, view_count " |
| 52 | + + "FROM `bigquery-public-data.stackoverflow.posts_questions` " |
| 53 | + + "WHERE tags like '%google-bigquery%' " |
| 54 | + + "ORDER BY view_count DESC " |
| 55 | + + "LIMIT 10") |
| 56 | + // Use standard SQL syntax for queries. |
| 57 | + // See: https://cloud.google.com/bigquery/sql-reference/ |
| 58 | + .setUseLegacySql(false) |
| 59 | + .build(); |
56 | 60 |
|
57 | | - // Wait for the query to complete. |
58 | | - queryJob = queryJob.waitFor(); |
| 61 | + JobId jobId = JobId.newBuilder().setProject(projectId).build(); |
| 62 | + Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build()); |
59 | 63 |
|
60 | | - // Check for errors |
61 | | - if (queryJob == null) { |
62 | | - throw new RuntimeException("Job no longer exists"); |
63 | | - } else if (queryJob.getStatus().getError() != null) { |
64 | | - // You can also look at queryJob.getStatus().getExecutionErrors() for all |
65 | | - // errors, not just the latest one. |
66 | | - throw new RuntimeException(queryJob.getStatus().getError().toString()); |
67 | | - } |
68 | | - // [END bigquery_simple_app_query] |
| 64 | + // Wait for the query to complete. |
| 65 | + queryJob = queryJob.waitFor(); |
| 66 | + |
| 67 | + // Check for errors |
| 68 | + if (queryJob == null) { |
| 69 | + throw new RuntimeException("Job no longer exists"); |
| 70 | + } else if (queryJob.getStatus().getError() != null) { |
| 71 | + // You can also look at queryJob.getStatus().getExecutionErrors() for all |
| 72 | + // errors, not just the latest one. |
| 73 | + throw new RuntimeException(queryJob.getStatus().getError().toString()); |
| 74 | + } |
| 75 | + // [END bigquery_simple_app_query] |
69 | 76 |
|
70 | | - // [START bigquery_simple_app_print] |
71 | | - // Get the results. |
72 | | - TableResult result = queryJob.getQueryResults(); |
| 77 | + // [START bigquery_simple_app_print] |
| 78 | + // Get the results. |
| 79 | + TableResult result = queryJob.getQueryResults(); |
73 | 80 |
|
74 | | - // Print all pages of the results. |
75 | | - for (FieldValueList row : result.iterateAll()) { |
76 | | - // String type |
77 | | - String url = row.get("url").getStringValue(); |
78 | | - String viewCount = row.get("view_count").getStringValue(); |
79 | | - System.out.printf("%s : %s views\n", url, viewCount); |
| 81 | + // Print all pages of the results. |
| 82 | + for (FieldValueList row : result.iterateAll()) { |
| 83 | + // String type |
| 84 | + String url = row.get("url").getStringValue(); |
| 85 | + String viewCount = row.get("view_count").getStringValue(); |
| 86 | + System.out.printf("%s : %s views\n", url, viewCount); |
| 87 | + } |
| 88 | + } catch (BigQueryException | InterruptedException e) { |
| 89 | + System.out.println("Simple App failed due to error: \n" + e.toString()); |
80 | 90 | } |
81 | 91 | // [END bigquery_simple_app_print] |
82 | 92 | } |
|
0 commit comments