Skip to content

Commit d407baa

Browse files
docs: add simple query connection read api sample (#3394)
* Basic setup for connection api sample using simple query * Update sample to use Connection * Fix import/lint * Fix import order * Remove closing result set as it is not implemented * Remove necessary TODO comment * 🦉 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 54ae77d commit d407baa

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigquery/tree
233233
| Set User Agent | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/SetUserAgent.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/SetUserAgent.java) |
234234
| Simple App | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/SimpleApp.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/SimpleApp.java) |
235235
| Simple Query | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/SimpleQuery.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/SimpleQuery.java) |
236+
| Simple Query Connection Read Api | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.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/SimpleQueryConnectionReadApi.java) |
236237
| Table Exists | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/TableExists.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/TableExists.java) |
237238
| Table Insert Rows | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/TableInsertRows.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/TableInsertRows.java) |
238239
| Table Insert Rows Without Row Ids | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/TableInsertRowsWithoutRowIds.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/TableInsertRowsWithoutRowIds.java) |
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_simple_query_connection_read_api]
20+
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.BigQueryResult;
24+
import com.google.cloud.bigquery.Connection;
25+
import com.google.cloud.bigquery.ConnectionSettings;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
29+
public class SimpleQueryConnectionReadApi {
30+
31+
public static void main(String[] args) {
32+
String query =
33+
"SELECT corpus, count(*) as corpus_count "
34+
+ "FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
35+
simpleQueryConnectionReadApi(query);
36+
}
37+
38+
public static void simpleQueryConnectionReadApi(String query) {
39+
40+
try {
41+
// Initialize client and create a Connection session.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
ConnectionSettings connectionSettings =
44+
ConnectionSettings.newBuilder()
45+
.setRequestTimeout(10L)
46+
.setMaxResults(100L)
47+
.setUseQueryCache(true)
48+
.build();
49+
Connection connection = bigquery.createConnection(connectionSettings);
50+
51+
// Execute the query using the Connection session.
52+
BigQueryResult bigQueryResult = connection.executeSelect(query);
53+
ResultSet resultSet = bigQueryResult.getResultSet();
54+
55+
while (resultSet.next()) {
56+
System.out.print("corpus:" + resultSet.getString("corpus"));
57+
System.out.print(", count:" + resultSet.getLong("corpus_count"));
58+
System.out.println();
59+
}
60+
System.out.println("Query ran successfully");
61+
} catch (SQLException e) {
62+
System.out.println("Query did not run \n" + e.toString());
63+
}
64+
}
65+
}
66+
// [END bigquery_simple_query_connection_read_api]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
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 SimpleQueryConnectionReadApiIT {
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 testSimpleQueryConnectionReadApi() {
54+
String query =
55+
"SELECT corpus, count(*) as corpus_count "
56+
+ "FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
57+
58+
SimpleQueryConnectionReadApi.simpleQueryConnectionReadApi(query);
59+
assertThat(bout.toString()).contains("Query ran successfully");
60+
}
61+
}

0 commit comments

Comments
 (0)