From d3b50976f8a6687a6dac2f483ae133c026b81cac Mon Sep 17 00:00:00 2001 From: nginsberg-google <131713109+nginsberg-google@users.noreply.github.com> Date: Sat, 20 Apr 2024 10:43:08 -0700 Subject: [PATCH] docs: add a sample for max commit delays (#2941) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add sample and test. * format * format * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update samples/snippets/src/main/java/com/example/spanner/SetMaxCommitDelaySample.java * chore: removed unused import --------- Co-authored-by: Owl Bot Co-authored-by: Arpan Mishra Co-authored-by: Knut Olav Løite --- README.md | 1 + .../spanner/SetMaxCommitDelaySample.java | 71 ++++++++++++ .../spanner/SetMaxCommitDelaySampleIT.java | 105 ++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/spanner/SetMaxCommitDelaySample.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/SetMaxCommitDelaySampleIT.java diff --git a/README.md b/README.md index 0417e7f9a4f..cdbab7b5cd0 100644 --- a/README.md +++ b/README.md @@ -497,6 +497,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/ | Quickstart Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/QuickstartSample.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/QuickstartSample.java) | | Read Data With Database Role | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/ReadDataWithDatabaseRole.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/ReadDataWithDatabaseRole.java) | | Restore Backup With Encryption Key | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/RestoreBackupWithEncryptionKey.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/RestoreBackupWithEncryptionKey.java) | +| Set Max Commit Delay Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/SetMaxCommitDelaySample.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/SetMaxCommitDelaySample.java) | | Spanner Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/SpannerSample.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/SpannerSample.java) | | Statement Timeout Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/StatementTimeoutExample.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/StatementTimeoutExample.java) | | Tag Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/TagSample.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/TagSample.java) | diff --git a/samples/snippets/src/main/java/com/example/spanner/SetMaxCommitDelaySample.java b/samples/snippets/src/main/java/com/example/spanner/SetMaxCommitDelaySample.java new file mode 100644 index 00000000000..90166482936 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/SetMaxCommitDelaySample.java @@ -0,0 +1,71 @@ +/* + * 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.spanner; + +// [START spanner_set_max_commit_delay] + +import com.google.cloud.spanner.CommitResponse; +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Mutation; +import com.google.cloud.spanner.Options; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; +import java.time.Duration; +import java.util.Arrays; + +public class SetMaxCommitDelaySample { + + static void setMaxCommitDelay() { + // TODO(developer): Replace these variables before running the sample. + final String projectId = "my-project"; + final String instanceId = "my-instance"; + final String databaseId = "my-database"; + + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { + final DatabaseClient databaseClient = spanner + .getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); + setMaxCommitDelay(databaseClient); + } + } + + static void setMaxCommitDelay(DatabaseClient databaseClient) { + final CommitResponse commitResponse = databaseClient.writeWithOptions(Arrays.asList( + Mutation.newInsertOrUpdateBuilder("Albums") + .set("SingerId") + .to("1") + .set("AlbumId") + .to("1") + .set("MarketingBudget") + .to("200000") + .build(), + Mutation.newInsertOrUpdateBuilder("Albums") + .set("SingerId") + .to("2") + .set("AlbumId") + .to("2") + .set("MarketingBudget") + .to("400000") + .build() + ), Options.maxCommitDelay(Duration.ofMillis(100))); + + System.out.println( + "Updated data with timestamp + " + commitResponse.getCommitTimestamp() + "."); + } +} +// [END spanner_set_max_commit_delay] diff --git a/samples/snippets/src/test/java/com/example/spanner/SetMaxCommitDelaySampleIT.java b/samples/snippets/src/test/java/com/example/spanner/SetMaxCommitDelaySampleIT.java new file mode 100644 index 00000000000..de9e47951f8 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/SetMaxCommitDelaySampleIT.java @@ -0,0 +1,105 @@ +/* + * 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.spanner; + +import static com.example.spanner.SampleRunner.runSample; +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.KeySet; +import com.google.cloud.spanner.Mutation; +import com.google.common.collect.ImmutableList; +import java.util.Arrays; +import java.util.Collections; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration tests for {@link SetMaxCommitDelaySample} + */ +@RunWith(JUnit4.class) +public class SetMaxCommitDelaySampleIT extends SampleTestBase { + + private static DatabaseId databaseId; + + @BeforeClass + public static void createTestDatabase() throws Exception { + final String database = idGenerator.generateDatabaseId(); + databaseAdminClient + .createDatabase( + instanceId, + database, + ImmutableList.of( + "CREATE TABLE Singers (" + + " SingerId INT64 NOT NULL," + + " FirstName STRING(1024)," + + " LastName STRING(1024)," + + " SingerInfo BYTES(MAX)" + + ") PRIMARY KEY (SingerId)", + "CREATE TABLE Albums (" + + " SingerId INT64 NOT NULL," + + " AlbumId INT64 NOT NULL," + + " AlbumTitle STRING(MAX)," + + " MarketingBudget INT64" + + ") PRIMARY KEY (SingerId, AlbumId)," + + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE")) + .get(); + databaseId = DatabaseId.of(projectId, instanceId, database); + } + + @Before + public void insertTestData() { + final DatabaseClient client = spanner.getDatabaseClient(databaseId); + client.write(Arrays.asList( + Mutation.newInsertBuilder("Singers") + .set("SingerId") + .to(1L) + .set("FirstName") + .to("first name 1") + .set("LastName") + .to("last name 1") + .build(), + Mutation.newInsertBuilder("Singers") + .set("SingerId") + .to(2L) + .set("FirstName") + .to("first name 2") + .set("LastName") + .to("last name 2") + .build() + )); + } + + @After + public void removeTestData() { + final DatabaseClient client = spanner.getDatabaseClient(databaseId); + client.write(Collections.singletonList(Mutation.delete("Singers", KeySet.all()))); + } + + @Test + public void testSetMaxCommitDelaySample() throws Exception { + final DatabaseClient client = spanner.getDatabaseClient(databaseId); + final String out = runSample(() -> SetMaxCommitDelaySample.setMaxCommitDelay(client)); + + assertThat(out).contains("Updated data"); + } +}