From 7a9ac9342c7cf3ee683ea8076b7a75fdd9c8fa65 Mon Sep 17 00:00:00 2001 From: PhongChuong Date: Wed, 17 Jul 2024 16:05:02 -0400 Subject: [PATCH 1/7] Basic setup for connection api sample using simple query --- .../SimpleQueryConnectionReadApi.java | 65 +++++++++++++++++++ .../SimpleQueryConnectionReadApiIT.java | 61 +++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java create mode 100644 samples/snippets/src/test/java/com/example/bigquery/SimpleQueryConnectionReadApiIT.java diff --git a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java new file mode 100644 index 000000000..daf3bb3ff --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java @@ -0,0 +1,65 @@ +/* + * 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.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.QueryJobConfiguration; +import com.google.cloud.bigquery.TableResult; + +public class SimpleQueryConnectionReadApi { + + public static void main(String[] args) { + // TODO(developer): Replace this query before running the sample. + 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 that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create the query job. + QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); + + // Execute the query. + TableResult result = bigquery.query(queryConfig); + + // Print the results. + result + .iterateAll() + .forEach( + row -> { + System.out.print("corpus:" + row.get("corpus").getStringValue()); + System.out.print(", count:" + row.get("corpus_count").getLongValue()); + System.out.println(); + }); + + System.out.println("Query ran successfully"); + } catch (BigQueryException | InterruptedException e) { + System.out.println("Query did not run \n" + e.toString()); + } + } +} +// [END bigquery_simple_query_connection_read_api] diff --git a/samples/snippets/src/test/java/com/example/bigquery/SimpleQueryConnectionReadApiIT.java b/samples/snippets/src/test/java/com/example/bigquery/SimpleQueryConnectionReadApiIT.java new file mode 100644 index 000000000..b7cb109c7 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/SimpleQueryConnectionReadApiIT.java @@ -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"); + } +} From 9aff46f80aae01220222658855cc7e2e3319fcb2 Mon Sep 17 00:00:00 2001 From: PhongChuong Date: Thu, 25 Jul 2024 16:21:20 -0400 Subject: [PATCH 2/7] Update sample to use Connection --- .../SimpleQueryConnectionReadApi.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java index daf3bb3ff..451c1dfbb 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java +++ b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java @@ -35,26 +35,28 @@ public static void main(String[] args) { } public static void simpleQueryConnectionReadApi(String query) { + + try { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. + // 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); - // Create the query job. - QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); - - // Execute the query. - TableResult result = bigquery.query(queryConfig); + // Execute the query using the Connection session. + BigQueryResult bigQueryResult = connection.executeSelect(query); + ResultSet resultSet = bigQueryResult.getResultSet(); - // Print the results. - result - .iterateAll() - .forEach( - row -> { - System.out.print("corpus:" + row.get("corpus").getStringValue()); - System.out.print(", count:" + row.get("corpus_count").getLongValue()); - System.out.println(); - }); + while (resultSet.next()) { + System.out.print("corpus:" + resultSet.getString("corpus")); + System.out.print(", count:" + resultSet.getLong("corpus_count"); + System.out.println(); + } + resultSet.close(); System.out.println("Query ran successfully"); } catch (BigQueryException | InterruptedException e) { From 2025e1a79e32c5a9bb74f1b2be0dcf98924addfb Mon Sep 17 00:00:00 2001 From: PhongChuong Date: Thu, 25 Jul 2024 16:34:06 -0400 Subject: [PATCH 3/7] Fix import/lint --- .../bigquery/SimpleQueryConnectionReadApi.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java index 451c1dfbb..d0767f486 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java +++ b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java @@ -21,8 +21,13 @@ import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.BigQueryOptions; -import com.google.cloud.bigquery.QueryJobConfiguration; -import com.google.cloud.bigquery.TableResult; +import com.google.cloud.bigquery.ConnectionSettings; +import com.google.cloud.bigquery.Connection; +import com.google.cloud.bigquery.BigQueryResult; + +import java.sql.SQLException; +import java.sql.ResultSet; + public class SimpleQueryConnectionReadApi { @@ -53,13 +58,13 @@ public static void simpleQueryConnectionReadApi(String query) { while (resultSet.next()) { System.out.print("corpus:" + resultSet.getString("corpus")); - System.out.print(", count:" + resultSet.getLong("corpus_count"); + System.out.print(", count:" + resultSet.getLong("corpus_count")); System.out.println(); } resultSet.close(); System.out.println("Query ran successfully"); - } catch (BigQueryException | InterruptedException e) { + } catch (SQLException e) { System.out.println("Query did not run \n" + e.toString()); } } From a89da1b34c143aa23e5bfe589a9c659858a918e8 Mon Sep 17 00:00:00 2001 From: PhongChuong Date: Thu, 25 Jul 2024 16:43:15 -0400 Subject: [PATCH 4/7] Fix import order --- .../example/bigquery/SimpleQueryConnectionReadApi.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java index d0767f486..d575673a5 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java +++ b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java @@ -21,13 +21,11 @@ import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.BigQueryOptions; -import com.google.cloud.bigquery.ConnectionSettings; -import com.google.cloud.bigquery.Connection; import com.google.cloud.bigquery.BigQueryResult; - -import java.sql.SQLException; +import com.google.cloud.bigquery.Connection; +import com.google.cloud.bigquery.ConnectionSettings; import java.sql.ResultSet; - +import java.sql.SQLException; public class SimpleQueryConnectionReadApi { From 884df7446675ea0a8e2bf022afcc84d8916fa2ce Mon Sep 17 00:00:00 2001 From: PhongChuong Date: Thu, 25 Jul 2024 17:03:25 -0400 Subject: [PATCH 5/7] Remove closing result set as it is not implemented --- .../java/com/example/bigquery/SimpleQueryConnectionReadApi.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java index d575673a5..371ba2732 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java +++ b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java @@ -59,8 +59,6 @@ public static void simpleQueryConnectionReadApi(String query) { System.out.print(", count:" + resultSet.getLong("corpus_count")); System.out.println(); } - resultSet.close(); - System.out.println("Query ran successfully"); } catch (SQLException e) { System.out.println("Query did not run \n" + e.toString()); From b0d02ba17f6ff00584a91b84708645777e6027ab Mon Sep 17 00:00:00 2001 From: PhongChuong Date: Thu, 25 Jul 2024 19:16:24 -0400 Subject: [PATCH 6/7] Remove necessary TODO comment --- .../java/com/example/bigquery/SimpleQueryConnectionReadApi.java | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java index 371ba2732..c5e39a8a5 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java +++ b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java @@ -30,7 +30,6 @@ public class SimpleQueryConnectionReadApi { public static void main(String[] args) { - // TODO(developer): Replace this query before running the sample. String query = "SELECT corpus, count(*) as corpus_count " + "FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; From 7c05e1ead97cb3be9a803c7705fc55ff45a88d54 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 25 Jul 2024 23:18:40 +0000 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 1 + .../bigquery/SimpleQueryConnectionReadApi.java | 13 ++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b170f4afa..31dd098f4 100644 --- a/README.md +++ b/README.md @@ -233,6 +233,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigquery/tree | 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) | | 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) | | 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) | +| 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) | | 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) | | 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) | | 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) | diff --git a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java index c5e39a8a5..970c29a2e 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java +++ b/samples/snippets/src/main/java/com/example/bigquery/SimpleQueryConnectionReadApi.java @@ -19,7 +19,6 @@ // [START bigquery_simple_query_connection_read_api] import com.google.cloud.bigquery.BigQuery; -import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.BigQueryResult; import com.google.cloud.bigquery.Connection; @@ -38,15 +37,15 @@ public static void main(String[] args) { 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(); + ConnectionSettings connectionSettings = + ConnectionSettings.newBuilder() + .setRequestTimeout(10L) + .setMaxResults(100L) + .setUseQueryCache(true) + .build(); Connection connection = bigquery.createConnection(connectionSettings); // Execute the query using the Connection session.