-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
54ae77d
commit d407baa
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
// [START bigquery_simple_query_connection_read_api] | ||
|
||
import com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.cloud.bigquery.BigQueryResult; | ||
import com.google.cloud.bigquery.Connection; | ||
import com.google.cloud.bigquery.ConnectionSettings; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class SimpleQueryConnectionReadApi { | ||
|
||
public static void main(String[] args) { | ||
String query = | ||
"SELECT corpus, count(*) as corpus_count " | ||
+ "FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; | ||
simpleQueryConnectionReadApi(query); | ||
} | ||
|
||
public static void simpleQueryConnectionReadApi(String query) { | ||
|
||
try { | ||
// Initialize client and create a Connection session. | ||
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); | ||
ConnectionSettings connectionSettings = | ||
ConnectionSettings.newBuilder() | ||
.setRequestTimeout(10L) | ||
.setMaxResults(100L) | ||
.setUseQueryCache(true) | ||
.build(); | ||
Connection connection = bigquery.createConnection(connectionSettings); | ||
|
||
// Execute the query using the Connection session. | ||
BigQueryResult bigQueryResult = connection.executeSelect(query); | ||
ResultSet resultSet = bigQueryResult.getResultSet(); | ||
|
||
while (resultSet.next()) { | ||
System.out.print("corpus:" + resultSet.getString("corpus")); | ||
System.out.print(", count:" + resultSet.getLong("corpus_count")); | ||
System.out.println(); | ||
} | ||
System.out.println("Query ran successfully"); | ||
} catch (SQLException e) { | ||
System.out.println("Query did not run \n" + e.toString()); | ||
} | ||
} | ||
} | ||
// [END bigquery_simple_query_connection_read_api] |
61 changes: 61 additions & 0 deletions
61
samples/snippets/src/test/java/com/example/bigquery/SimpleQueryConnectionReadApiIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class SimpleQueryConnectionReadApiIT { | ||
|
||
private final Logger log = Logger.getLogger(this.getClass().getName()); | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private PrintStream originalPrintStream; | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
originalPrintStream = System.out; | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
// restores print statements in the original method | ||
System.out.flush(); | ||
System.setOut(originalPrintStream); | ||
log.log(Level.INFO, "\n" + bout.toString()); | ||
} | ||
|
||
@Test | ||
public void testSimpleQueryConnectionReadApi() { | ||
String query = | ||
"SELECT corpus, count(*) as corpus_count " | ||
+ "FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; | ||
|
||
SimpleQueryConnectionReadApi.simpleQueryConnectionReadApi(query); | ||
assertThat(bout.toString()).contains("Query ran successfully"); | ||
} | ||
} |