Skip to content

Commit 6dca6ff

Browse files
docs: add short mode query sample (#3397)
* docs: add short mode query sample * formatting * update comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * update sample comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent a61ce7d commit 6dca6ff

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigquery/tree
214214
| Query Pagination | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/QueryPagination.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/QueryPagination.java) |
215215
| Query Partitioned Table | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/QueryPartitionedTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/QueryPartitionedTable.java) |
216216
| Query Script | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/QueryScript.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/QueryScript.java) |
217+
| Query Short Mode | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/QueryShortMode.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/QueryShortMode.java) |
217218
| Query Total Rows | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/QueryTotalRows.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/QueryTotalRows.java) |
218219
| Query With Array Of Structs Named Parameters | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayOfStructsNamedParameters.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayOfStructsNamedParameters.java) |
219220
| Query With Array Parameters | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayParameters.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayParameters.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_query_shortquery]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.JobId;
24+
import com.google.cloud.bigquery.QueryJobConfiguration;
25+
import com.google.cloud.bigquery.TableResult;
26+
27+
// Sample demonstrating short mode query execution.
28+
//
29+
// While this feature is still in preview, it is controlled by
30+
// setting the environment variable QUERY_PREVIEW_ENABLED=TRUE
31+
// to request short mode execution.
32+
public class QueryShortMode {
33+
34+
public static void main(String[] args) {
35+
String query =
36+
"SELECT name, gender, SUM(number) AS total FROM "
37+
+ "bigquery-public-data.usa_names.usa_1910_2013 GROUP BY "
38+
+ "name, gender ORDER BY total DESC LIMIT 10";
39+
queryShortMode(query);
40+
}
41+
42+
public static void queryShortMode(String query) {
43+
try {
44+
// Initialize client that will be used to send requests. This client only needs
45+
// to be created once, and can be reused for multiple requests.
46+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
47+
48+
// Execute the query. The returned TableResult provides access information
49+
// about the query execution as well as query results.
50+
TableResult results = bigquery.query(QueryJobConfiguration.of(query));
51+
52+
JobId jobId = results.getJobId();
53+
if (jobId != null) {
54+
System.out.println("Query was run with job state. Job ID: " + jobId.toString());
55+
} else {
56+
System.out.println("Query was run in short mode. Query ID: " + results.getQueryId());
57+
}
58+
59+
// Print the results.
60+
results
61+
.iterateAll()
62+
.forEach(
63+
row -> {
64+
System.out.print("name:" + row.get("name").getStringValue());
65+
System.out.print(", gender: " + row.get("gender").getStringValue());
66+
System.out.print(", total: " + row.get("total").getLongValue());
67+
System.out.println();
68+
});
69+
70+
} catch (BigQueryException | InterruptedException e) {
71+
System.out.println("Query not performed \n" + e.toString());
72+
}
73+
}
74+
}
75+
// [END bigquery_query_shortquery]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
public class QueryShortModeIT {
30+
31+
private final Logger log = Logger.getLogger(this.getClass().getName());
32+
private ByteArrayOutputStream bout;
33+
private PrintStream out;
34+
private PrintStream originalPrintStream;
35+
36+
@Before
37+
public void setUp() {
38+
bout = new ByteArrayOutputStream();
39+
out = new PrintStream(bout);
40+
originalPrintStream = System.out;
41+
System.setOut(out);
42+
}
43+
44+
@After
45+
public void tearDown() {
46+
// restores print statements in the original method
47+
System.out.flush();
48+
System.setOut(originalPrintStream);
49+
log.log(Level.INFO, "\n" + bout.toString());
50+
}
51+
52+
@Test
53+
public void testQueryBatch() {
54+
String query =
55+
"SELECT name, gender, SUM(number) AS total FROM "
56+
+ "bigquery-public-data.usa_names.usa_1910_2013 GROUP BY "
57+
+ "name, gender ORDER BY total DESC LIMIT 10";
58+
59+
QueryShortMode.queryShortMode(query);
60+
assertThat(bout.toString()).contains("Query was run");
61+
}
62+
}

0 commit comments

Comments
 (0)