From 2c7c17c273ef69878e7ddfadfe3ac78c9f7f5924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Fri, 18 Feb 2022 12:42:08 +0100 Subject: [PATCH 1/6] docs: add samples for PostgreSQL --- samples/snippets/pom.xml | 1 + .../com/example/spanner/PgBatchDmlSample.java | 85 ++++++++++ .../spanner/PgCaseSensitivitySample.java | 151 ++++++++++++++++++ .../spanner/PgInterleavedTableSample.java | 81 ++++++++++ .../spanner/PgPartitionedDmlSample.java | 58 +++++++ .../example/spanner/PgBatchDmlSampleIT.java | 59 +++++++ .../spanner/PgCaseSensitivitySampleIT.java | 46 ++++++ .../spanner/PgInterleavedTableSampleIT.java | 45 ++++++ .../spanner/PgPartitionedDmlSampleIT.java | 91 +++++++++++ 9 files changed, 617 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/spanner/PgBatchDmlSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/PgInterleavedTableSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/PgPartitionedDmlSample.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/PgBatchDmlSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/PgCaseSensitivitySampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/PgInterleavedTableSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/PgPartitionedDmlSampleIT.java diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 2104201819..5d6015b113 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -44,6 +44,7 @@ com.google.cloud google-cloud-spanner + 6.19.0 diff --git a/samples/snippets/src/main/java/com/example/spanner/PgBatchDmlSample.java b/samples/snippets/src/main/java/com/example/spanner/PgBatchDmlSample.java new file mode 100644 index 0000000000..6851dfd366 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/PgBatchDmlSample.java @@ -0,0 +1,85 @@ +/* + * Copyright 2022 Google Inc. + * + * 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.spanner; + +// [START spanner_postgresql_batch_dml] + +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; +import com.google.cloud.spanner.Statement; +import java.util.Arrays; + +class PgBatchDmlSample { + + static void batchDml() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + + batchDml(projectId, instanceId, databaseId); + } + + static void batchDml(String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService()) { + DatabaseClient client = + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); + + // Spanner PostgreSQL supports BatchDML statements. This will batch multiple DML statements + // into one request, which reduces the number of round trips that is needed for multiple DML + // statements. + long[] updateCounts = + client + .readWriteTransaction() + .run( + transaction -> + transaction.batchUpdate( + Arrays.asList( + Statement.newBuilder( + "INSERT INTO Singers (SingerId, FirstName, LastName) " + + "VALUES ($1, $2, $3)") + // Use 'p1' to bind to the parameter with index 1 etc. + .bind("p1") + .to(1L) + .bind("p2") + .to("Alice") + .bind("p3") + .to("Henderson") + .build(), + Statement.newBuilder( + "INSERT INTO Singers (SingerId, FirstName, LastName) " + + "VALUES ($1, $2, $3)") + // Use 'p1' to bind to the parameter with index 1 etc. + .bind("p1") + .to(2L) + .bind("p2") + .to("Bruce") + .bind("p3") + .to("Allison") + .build()))); + long totalUpdateCount = Arrays.stream(updateCounts).sum(); + System.out.printf("Inserted %d singers\n", totalUpdateCount); + } + } +} +// [END spanner_postgresql_batch_dml] \ No newline at end of file diff --git a/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java b/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java new file mode 100644 index 0000000000..5218c8c8b4 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java @@ -0,0 +1,151 @@ +/* + * Copyright 2022 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.spanner; + +// [START spanner_postgresql_identifier_case_sensitivity] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.spanner.DatabaseAdminClient; +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Mutation; +import com.google.cloud.spanner.ResultSet; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; +import com.google.cloud.spanner.Statement; +import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; +import java.util.Collections; +import java.util.concurrent.ExecutionException; + +public class PgCaseSensitivitySample { + + static void pgCaseSensitivity() { + // TODO(developer): Replace these variables before running the sample. + final String projectId = "my-project"; + final String instanceId = "my-instance"; + final String databaseId = "my-database"; + pgCaseSensitivity(projectId, instanceId, databaseId); + } + + static void pgCaseSensitivity(String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService()) { + final DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient(); + + // Spanner PostgreSQL follows the case sensitivity rules of PostgreSQL. This means that: + // 1. Identifiers that are not double-quoted are folded to lower case. + // 2. Identifiers that are double-quoted retain their case and are case-sensitive. + // See https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS + // for more information. + final OperationFuture updateOperation = + databaseAdminClient.updateDatabaseDdl( + instanceId, + databaseId, + Collections.singleton( + "CREATE TABLE Singers (" + // SingerId will be folded to `singerid`. + + " SingerId bigint NOT NULL PRIMARY KEY," + // FirstName and LastName are double-quoted and will therefore retain their + // mixed case and are case-sensitive. This means that any statement that + // references any of these columns must use double quotes. + + " \"FirstName\" varchar(1024) NOT NULL," + + " \"LastName\" varchar(1024) NOT NULL" + + ")"), + null); + updateOperation.get(); + + DatabaseClient client = + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); + + client.write( + Collections.singleton( + Mutation.newInsertBuilder("Singers") + .set("singerid") + .to(1L) + // Column names in mutations are always case-insensitive, regardless whether the + // columns were double-quoted or not during creation. + .set("firstname") + .to("Bruce") + .set("lastname") + .to("Allison") + .build())); + + try (ResultSet singers = + client.singleUse().executeQuery(Statement.of("SELECT * FROM Singers"))) { + while (singers.next()) { + System.out.printf( + "SingerId: %d, FirstName: %s, LastName: %s\n", + // SingerId is automatically folded to lower case. Accessing the column by its name in + // a result set must therefore use all lower-case letters. + singers.getLong("singerid"), + // FirstName and LastName were double-quoted during creation, and retain their mixed + // case when returned in a result set. + singers.getString("FirstName"), + singers.getString("LastName")); + } + } + + // Aliases are also identifiers, and specifying an alias in double quotes will make the alias + // retain its case. + try (ResultSet singers = + client + .singleUse() + .executeQuery( + Statement.of( + "SELECT " + + "singerid AS \"SingerId\", " + + "concat(\"FirstName\", ' '::varchar, \"LastName\") AS \"FullName\" " + + "FROM Singers"))) { + while (singers.next()) { + System.out.printf( + "SingerId: %d, FullName: %s\n", + // The aliases are double-quoted and therefore retains their mixed case. + singers.getLong("SingerId"), singers.getString("FullName")); + } + } + + // DML statements must also follow the PostgreSQL case rules. + client + .readWriteTransaction() + .run( + transaction -> + transaction.executeUpdate( + Statement.newBuilder( + "INSERT INTO Singers (SingerId, \"FirstName\", \"LastName\") " + + "VALUES ($1, $2, $3)") + .bind("p1") + .to(2L) + .bind("p2") + .to("Alice") + .bind("p3") + .to("Bruxelles") + .build())); + } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. + throw SpannerExceptionFactory.asSpannerException(e.getCause()); + } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. + throw SpannerExceptionFactory.propagateInterrupt(e); + } + } +} +// [END spanner_postgresql_identifier_case_sensitivity] \ No newline at end of file diff --git a/samples/snippets/src/main/java/com/example/spanner/PgInterleavedTableSample.java b/samples/snippets/src/main/java/com/example/spanner/PgInterleavedTableSample.java new file mode 100644 index 0000000000..d79ba7b58f --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/PgInterleavedTableSample.java @@ -0,0 +1,81 @@ +/* + * Copyright 2022 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.spanner; + +// [START spanner_postgresql_interleaved_table] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.spanner.DatabaseAdminClient; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; +import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; + +public class PgInterleavedTableSample { + + static void pgInterleavedTable() { + // TODO(developer): Replace these variables before running the sample. + final String projectId = "my-project"; + final String instanceId = "my-instance"; + final String databaseId = "my-database"; + pgInterleavedTable(projectId, instanceId, databaseId); + } + + static void pgInterleavedTable(String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService()) { + final DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient(); + + // The Spanner PostgreSQL dialect extends the PostgreSQL dialect with certain Spanner + // specific features, such as interleaved tables. + // See https://cloud.google.com/spanner/docs/postgresql/data-definition-language#create_table + // for the full CREATE TABLE syntax. + final OperationFuture updateOperation = + databaseAdminClient.updateDatabaseDdl( + instanceId, + databaseId, + Arrays.asList( + "CREATE TABLE Singers (" + + " SingerId bigint NOT NULL PRIMARY KEY," + + " FirstName varchar(1024) NOT NULL," + + " LastName varchar(1024) NOT NULL" + + ")", + "CREATE TABLE Albums (" + + " SingerId bigint NOT NULL," + + " AlbumId bigint NOT NULL," + + " Title varchar(1024) NOT NULL," + + " PRIMARY KEY (SingerId, AlbumId)" + + ") INTERLEAVE IN PARENT Singers ON DELETE CASCADE"), + null); + updateOperation.get(); + System.out.println("Created interleaved table hierarchy using PostgreSQL dialect"); + } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. + throw SpannerExceptionFactory.asSpannerException(e.getCause()); + } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. + throw SpannerExceptionFactory.propagateInterrupt(e); + } + } +} +// [END spanner_postgresql_interleaved_table] \ No newline at end of file diff --git a/samples/snippets/src/main/java/com/example/spanner/PgPartitionedDmlSample.java b/samples/snippets/src/main/java/com/example/spanner/PgPartitionedDmlSample.java new file mode 100644 index 0000000000..ff442e7b2e --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/PgPartitionedDmlSample.java @@ -0,0 +1,58 @@ +/* + * Copyright 2022 Google Inc. + * + * 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.spanner; + +// [START spanner_postgresql_partitioned_dml] + +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; +import com.google.cloud.spanner.Statement; + +class PgPartitionedDmlSample { + + static void partitionedDml() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + + partitionedDml(projectId, instanceId, databaseId); + } + + static void partitionedDml(String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService()) { + DatabaseClient client = + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); + + // Spanner PostgreSQL has the same transaction limits as normal Spanner. This includes a + // maximum of 20,000 mutations in a single read/write transaction. Large update operations can + // be executed using Partitioned DML. This is also supported on Spanner PostgreSQL. + // See https://cloud.google.com/spanner/docs/dml-partitioned for more information. + long deletedCount = + client.executePartitionedUpdate(Statement.of("DELETE FROM users WHERE active=false")); + // The returned update count is the lower bound of the number of records that was deleted. + System.out.printf("Deleted at least %d inactive users\n", deletedCount); + } + } +} +// [END spanner_postgresql_partitioned_dml] \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/spanner/PgBatchDmlSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/PgBatchDmlSampleIT.java new file mode 100644 index 0000000000..0b105d41b7 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/PgBatchDmlSampleIT.java @@ -0,0 +1,59 @@ +/* + * Copyright 2022 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.spanner; + +import static org.junit.Assert.assertTrue; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Dialect; +import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; +import java.util.Collections; +import org.junit.Test; + +public class PgBatchDmlSampleIT extends SampleTestBase { + + @Test + public void testPgBatchDml() throws Exception { + final String databaseId = idGenerator.generateDatabaseId(); + databaseAdminClient + .createDatabase( + databaseAdminClient + .newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)) + .setDialect(Dialect.POSTGRESQL) + .build(), + Collections.emptyList()) + .get(); + final OperationFuture updateOperation = + databaseAdminClient.updateDatabaseDdl( + instanceId, + databaseId, + Collections.singleton( + "CREATE TABLE Singers (" + + " SingerId bigint NOT NULL PRIMARY KEY," + + " FirstName varchar(1024)," + + " LastName varchar(1024)" + + ")"), + null); + updateOperation.get(); + + final String out = + SampleRunner.runSample(() -> PgBatchDmlSample.batchDml(projectId, instanceId, databaseId)); + + assertTrue(out.contains("Inserted 2 singers")); + } +} \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/spanner/PgCaseSensitivitySampleIT.java b/samples/snippets/src/test/java/com/example/spanner/PgCaseSensitivitySampleIT.java new file mode 100644 index 0000000000..1c2bca587c --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/PgCaseSensitivitySampleIT.java @@ -0,0 +1,46 @@ +/* + * Copyright 2022 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.spanner; + +import static org.junit.Assert.assertTrue; + +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Dialect; +import java.util.Collections; +import org.junit.Test; + +public class PgCaseSensitivitySampleIT extends SampleTestBase { + + @Test + public void testPgCaseSensitivitySample() throws Exception { + final String databaseId = idGenerator.generateDatabaseId(); + databaseAdminClient + .createDatabase( + databaseAdminClient + .newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)) + .setDialect(Dialect.POSTGRESQL) + .build(), + Collections.emptyList()) + .get(); + + final String out = + SampleRunner.runSample( + () -> PgCaseSensitivitySample.pgCaseSensitivity(projectId, instanceId, databaseId)); + assertTrue(out, out.contains("SingerId: 1, FirstName: Bruce, LastName: Allison")); + assertTrue(out, out.contains("SingerId: 1, FullName: Bruce Allison")); + } +} \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/spanner/PgInterleavedTableSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/PgInterleavedTableSampleIT.java new file mode 100644 index 0000000000..6678f2a471 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/PgInterleavedTableSampleIT.java @@ -0,0 +1,45 @@ +/* + * Copyright 2022 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.spanner; + +import static org.junit.Assert.assertTrue; + +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Dialect; +import java.util.Collections; +import org.junit.Test; + +public class PgInterleavedTableSampleIT extends SampleTestBase { + + @Test + public void testPgInterleavedTableSample() throws Exception { + final String databaseId = idGenerator.generateDatabaseId(); + databaseAdminClient + .createDatabase( + databaseAdminClient + .newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)) + .setDialect(Dialect.POSTGRESQL) + .build(), + Collections.emptyList()) + .get(); + + final String out = + SampleRunner.runSample( + () -> PgInterleavedTableSample.pgInterleavedTable(projectId, instanceId, databaseId)); + assertTrue(out.contains("Created interleaved table hierarchy using PostgreSQL dialect")); + } +} \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/spanner/PgPartitionedDmlSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/PgPartitionedDmlSampleIT.java new file mode 100644 index 0000000000..26d7a37b64 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/PgPartitionedDmlSampleIT.java @@ -0,0 +1,91 @@ +/* + * Copyright 2022 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.spanner; + +import static org.junit.Assert.assertTrue; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Dialect; +import com.google.cloud.spanner.Mutation; +import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; +import java.util.Arrays; +import java.util.Collections; +import org.junit.Test; + +public class PgPartitionedDmlSampleIT extends SampleTestBase { + + @Test + public void testPgPartitionedDml() throws Exception { + final String databaseId = idGenerator.generateDatabaseId(); + databaseAdminClient + .createDatabase( + databaseAdminClient + .newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)) + .setDialect(Dialect.POSTGRESQL) + .build(), + Collections.emptyList()) + .get(); + final OperationFuture updateOperation = + databaseAdminClient.updateDatabaseDdl( + instanceId, + databaseId, + Collections.singleton( + "CREATE TABLE users (" + + " user_id bigint NOT NULL PRIMARY KEY," + + " user_name varchar(1024)," + + " active boolean" + + ")"), + null); + updateOperation.get(); + DatabaseClient client = + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); + client.write( + Arrays.asList( + Mutation.newInsertBuilder("users") + .set("user_id") + .to(1L) + .set("user_name") + .to("user1") + .set("active") + .to(false) + .build(), + Mutation.newInsertBuilder("users") + .set("user_id") + .to(2L) + .set("user_name") + .to("user2") + .set("active") + .to(false) + .build(), + Mutation.newInsertBuilder("users") + .set("user_id") + .to(3L) + .set("user_name") + .to("user3") + .set("active") + .to(true) + .build())); + + final String out = + SampleRunner.runSample( + () -> PgPartitionedDmlSample.partitionedDml(projectId, instanceId, databaseId)); + + assertTrue(out.contains("Deleted at least 2 inactive users")); + } +} \ No newline at end of file From 1a63c09c87a50e7143cf7d358534faccfff12475 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Fri, 18 Feb 2022 11:48:50 +0000 Subject: [PATCH 2/6] =?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 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index afe439ac3e..c147bb7fcc 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file com.google.cloud google-cloud-spanner + 6.19.0 ``` @@ -264,6 +265,10 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/ | Get Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/GetInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/GetInstanceConfigSample.java) | | List Databases Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/ListDatabasesSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/ListDatabasesSample.java) | | List Instance Configs Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/ListInstanceConfigsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/ListInstanceConfigsSample.java) | +| Pg Batch Dml Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/PgBatchDmlSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/PgBatchDmlSample.java) | +| Pg Case Sensitivity Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java) | +| Pg Interleaved Table Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/PgInterleavedTableSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/PgInterleavedTableSample.java) | +| Pg Partitioned Dml Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/PgPartitionedDmlSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/PgPartitionedDmlSample.java) | | Query Information Schema Database Options Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/QueryInformationSchemaDatabaseOptionsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/QueryInformationSchemaDatabaseOptionsSample.java) | | Query With Json Parameter Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/QueryWithJsonParameterSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/QueryWithJsonParameterSample.java) | | Query With Numeric Parameter Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/QueryWithNumericParameterSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/QueryWithNumericParameterSample.java) | From 22c2452264ed6ef21526aff881507750bbbb98a4 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 10 Mar 2022 14:08:37 +0000 Subject: [PATCH 3/6] =?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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4ef0b4c52f..325d83c7ee 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,13 @@ implementation 'com.google.cloud:google-cloud-spanner' If you are using Gradle without BOM, add this to your dependencies ```Groovy -implementation 'com.google.cloud:google-cloud-spanner:6.21.0' +implementation 'com.google.cloud:google-cloud-spanner:6.21.1' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.21.0" +libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.21.1" ``` ## Authentication From 5a825cc4a2d140b9f6021ad2c4a42905c6fec058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Thu, 10 Mar 2022 15:09:06 +0100 Subject: [PATCH 4/6] deps: remove specific Spanner version --- samples/snippets/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 03cd2ca2d8..bfe4d24f75 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -44,7 +44,6 @@ com.google.cloud google-cloud-spanner - 6.19.0 From 14ffcf0b614000316ddf0495f9fd7b3218b9b4ae Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 10 Mar 2022 14:11:22 +0000 Subject: [PATCH 5/6] =?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 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 325d83c7ee..c38d9f8172 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,6 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file com.google.cloud google-cloud-spanner - 6.19.0 ``` From 62e92c2d0c8459b02895f87b324b4bd886622669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Thu, 10 Mar 2022 17:13:42 +0100 Subject: [PATCH 6/6] fix: specify column names in query --- .../java/com/example/spanner/PgCaseSensitivitySample.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java b/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java index 5218c8c8b4..d76b26b178 100644 --- a/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java +++ b/samples/snippets/src/main/java/com/example/spanner/PgCaseSensitivitySample.java @@ -89,7 +89,10 @@ static void pgCaseSensitivity(String projectId, String instanceId, String databa .build())); try (ResultSet singers = - client.singleUse().executeQuery(Statement.of("SELECT * FROM Singers"))) { + client + .singleUse() + .executeQuery( + Statement.of("SELECT SingerId, \"FirstName\", \"LastName\" FROM Singers"))) { while (singers.next()) { System.out.printf( "SingerId: %d, FirstName: %s, LastName: %s\n",