1919import com .google .cloud .bigquery .BigQuery ;
2020import com .google .cloud .bigquery .FieldValue ;
2121import com .google .cloud .bigquery .FieldValueList ;
22- import com .google .cloud .bigquery .TableId ;
2322import com .google .cloud .bigquery .QueryJobConfiguration ;
2423import com .google .cloud .bigquery .QueryParameterValue ;
24+ import com .google .cloud .bigquery .TableId ;
25+ import java .util .concurrent .TimeoutException ;
2526import org .joda .time .DateTime ;
2627import org .joda .time .DateTimeZone ;
2728import org .joda .time .format .DateTimeFormatter ;
2829import org .joda .time .format .ISODateTimeFormat ;
29- import java .util .concurrent .TimeoutException ;
3030
3131/**
3232 * This class contains snippets for cloud.google.com documentation.
@@ -45,8 +45,7 @@ public CloudSnippets(BigQuery bigquery) {
4545 public void runLegacySqlQuery () throws InterruptedException {
4646 // [START bigquery_query_legacy]
4747 // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
48- String query =
49- "SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;" ;
48+ String query = "SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;" ;
5049 QueryJobConfiguration queryConfig =
5150 // To use legacy SQL syntax, set useLegacySql to true.
5251 QueryJobConfiguration .newBuilder (query ).setUseLegacySql (true ).build ();
@@ -67,8 +66,7 @@ public void runLegacySqlQuery() throws InterruptedException {
6766 public void runStandardSqlQuery () throws InterruptedException {
6867 // [START bigquery_query_standard]
6968 // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
70- String query =
71- "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
69+ String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
7270 QueryJobConfiguration queryConfig =
7371 // Note that setUseLegacySql is set to false by default
7472 QueryJobConfiguration .newBuilder (query ).setUseLegacySql (false ).build ();
@@ -86,19 +84,19 @@ public void runStandardSqlQuery() throws InterruptedException {
8684 /**
8785 * Example of running a query and saving the results to a table.
8886 */
89- public void runQueryPermanentTable (String destinationDataset , String destinationTable ) throws InterruptedException {
87+ public void runQueryPermanentTable (String destinationDataset , String destinationTable )
88+ throws InterruptedException {
9089 // [START bigquery_query_destination_table]
9190 // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
9291 // String destinationDataset = 'my_destination_dataset';
9392 // String destinationTable = 'my_destination_table';
94- String query =
95- "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
93+ String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
9694 QueryJobConfiguration queryConfig =
9795 // Note that setUseLegacySql is set to false by default
9896 QueryJobConfiguration .newBuilder (query )
99- // Save the results of the query to a permanent table.
100- .setDestinationTable (TableId .of (destinationDataset , destinationTable ))
101- .build ();
97+ // Save the results of the query to a permanent table.
98+ .setDestinationTable (TableId .of (destinationDataset , destinationTable ))
99+ .build ();
102100
103101 // Print the results.
104102 for (FieldValueList row : bigquery .query (queryConfig ).iterateAll ()) {
@@ -113,22 +111,23 @@ public void runQueryPermanentTable(String destinationDataset, String destination
113111 /**
114112 * Example of running a query and saving the results to a table.
115113 */
116- public void runQueryLargeResults (String destinationDataset , String destinationTable ) throws InterruptedException {
114+ public void runQueryLargeResults (String destinationDataset , String destinationTable )
115+ throws InterruptedException {
117116 // [START bigquery_query_legacy_large_results]
118117 // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
119118 // String destinationDataset = 'my_destination_dataset';
120119 // String destinationTable = 'my_destination_table';
121- String query =
122- "SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;" ;
120+ String query = "SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;" ;
123121 QueryJobConfiguration queryConfig =
124122 // To use legacy SQL syntax, set useLegacySql to true.
125- QueryJobConfiguration .newBuilder (query ).setUseLegacySql (true )
126- // Save the results of the query to a permanent table.
127- .setDestinationTable (TableId .of (destinationDataset , destinationTable ))
128- // Allow results larger than the maximum response size.
129- // If true, a destination table must be set.
130- .setAllowLargeResults (true )
131- .build ();
123+ QueryJobConfiguration .newBuilder (query )
124+ .setUseLegacySql (true )
125+ // Save the results of the query to a permanent table.
126+ .setDestinationTable (TableId .of (destinationDataset , destinationTable ))
127+ // Allow results larger than the maximum response size.
128+ // If true, a destination table must be set.
129+ .setAllowLargeResults (true )
130+ .build ();
132131
133132 // Print the results.
134133 for (FieldValueList row : bigquery .query (queryConfig ).iterateAll ()) {
@@ -146,13 +145,12 @@ public void runQueryLargeResults(String destinationDataset, String destinationTa
146145 public void runUncachedQuery () throws TimeoutException , InterruptedException {
147146 // [START bigquery_query_no_cache]
148147 // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
149- String query =
150- "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
148+ String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
151149 QueryJobConfiguration queryConfig =
152150 QueryJobConfiguration .newBuilder (query )
153- // Disable the query cache to force live query evaluation.
154- .setUseQueryCache (false )
155- .build ();
151+ // Disable the query cache to force live query evaluation.
152+ .setUseQueryCache (false )
153+ .build ();
156154
157155 // Print the results.
158156 for (FieldValueList row : bigquery .query (queryConfig ).iterateAll ()) {
@@ -170,15 +168,14 @@ public void runUncachedQuery() throws TimeoutException, InterruptedException {
170168 public void runBatchQuery () throws TimeoutException , InterruptedException {
171169 // [START bigquery_query_batch]
172170 // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
173- String query =
174- "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
171+ String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" ;
175172 QueryJobConfiguration queryConfig =
176173 QueryJobConfiguration .newBuilder (query )
177- // Run at batch priority, which won't count toward concurrent rate
178- // limit. See:
179- // https://cloud.google.com/bigquery/docs/running-queries#batch
180- .setPriority (QueryJobConfiguration .Priority .BATCH )
181- .build ();
174+ // Run at batch priority, which won't count toward concurrent rate
175+ // limit. See:
176+ // https://cloud.google.com/bigquery/docs/running-queries#batch
177+ .setPriority (QueryJobConfiguration .Priority .BATCH )
178+ .build ();
182179
183180 // Print the results.
184181 for (FieldValueList row : bigquery .query (queryConfig ).iterateAll ()) {
@@ -207,9 +204,9 @@ public void runQueryWithNamedParameters() throws InterruptedException {
207204 // Note: Standard SQL is required to use query parameters.
208205 QueryJobConfiguration queryConfig =
209206 QueryJobConfiguration .newBuilder (query )
210- .addNamedParameter ("corpus" , QueryParameterValue .string (corpus ))
211- .addNamedParameter ("min_word_count" , QueryParameterValue .int64 (minWordCount ))
212- .build ();
207+ .addNamedParameter ("corpus" , QueryParameterValue .string (corpus ))
208+ .addNamedParameter ("min_word_count" , QueryParameterValue .int64 (minWordCount ))
209+ .build ();
213210
214211 // Print the results.
215212 for (FieldValueList row : bigquery .query (queryConfig ).iterateAll ()) {
@@ -240,9 +237,9 @@ public void runQueryWithArrayParameters() throws InterruptedException {
240237 // Note: Standard SQL is required to use query parameters.
241238 QueryJobConfiguration queryConfig =
242239 QueryJobConfiguration .newBuilder (query )
243- .addNamedParameter ("gender" , QueryParameterValue .string (gender ))
244- .addNamedParameter ("states" , QueryParameterValue .array (states , String .class ))
245- .build ();
240+ .addNamedParameter ("gender" , QueryParameterValue .string (gender ))
241+ .addNamedParameter ("states" , QueryParameterValue .array (states , String .class ))
242+ .build ();
246243
247244 // Print the results.
248245 for (FieldValueList row : bigquery .query (queryConfig ).iterateAll ()) {
@@ -265,12 +262,12 @@ public void runQueryWithTimestampParameters() throws InterruptedException {
265262 // Note: Standard SQL is required to use query parameters.
266263 QueryJobConfiguration queryConfig =
267264 QueryJobConfiguration .newBuilder (query )
268- .addNamedParameter (
269- "ts_value" ,
270- QueryParameterValue .timestamp (
271- // Timestamp takes microseconds since 1970-01-01T00:00:00 UTC
272- timestamp .getMillis () * 1000 ))
273- .build ();
265+ .addNamedParameter (
266+ "ts_value" ,
267+ QueryParameterValue .timestamp (
268+ // Timestamp takes microseconds since 1970-01-01T00:00:00 UTC
269+ timestamp .getMillis () * 1000 ))
270+ .build ();
274271
275272 // Print the results.
276273 DateTimeFormatter formatter = ISODateTimeFormat .dateTimeNoMillis ().withZoneUTC ();
0 commit comments