Skip to content

Commit d3b5097

Browse files
nginsberg-googlegcf-owl-bot[bot]arpan14olavloite
authored
docs: add a sample for max commit delays (#2941)
* 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 <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Arpan Mishra <akmish3@gmail.com> Co-authored-by: Knut Olav Løite <koloite@gmail.com>
1 parent c461db1 commit d3b5097

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
497497
| 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) |
498498
| 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) |
499499
| 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) |
500+
| 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) |
500501
| 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) |
501502
| 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) |
502503
| 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) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.spanner;
18+
19+
// [START spanner_set_max_commit_delay]
20+
21+
import com.google.cloud.spanner.CommitResponse;
22+
import com.google.cloud.spanner.DatabaseClient;
23+
import com.google.cloud.spanner.DatabaseId;
24+
import com.google.cloud.spanner.Mutation;
25+
import com.google.cloud.spanner.Options;
26+
import com.google.cloud.spanner.Spanner;
27+
import com.google.cloud.spanner.SpannerOptions;
28+
import java.time.Duration;
29+
import java.util.Arrays;
30+
31+
public class SetMaxCommitDelaySample {
32+
33+
static void setMaxCommitDelay() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
final String projectId = "my-project";
36+
final String instanceId = "my-instance";
37+
final String databaseId = "my-database";
38+
39+
try (Spanner spanner =
40+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
41+
final DatabaseClient databaseClient = spanner
42+
.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
43+
setMaxCommitDelay(databaseClient);
44+
}
45+
}
46+
47+
static void setMaxCommitDelay(DatabaseClient databaseClient) {
48+
final CommitResponse commitResponse = databaseClient.writeWithOptions(Arrays.asList(
49+
Mutation.newInsertOrUpdateBuilder("Albums")
50+
.set("SingerId")
51+
.to("1")
52+
.set("AlbumId")
53+
.to("1")
54+
.set("MarketingBudget")
55+
.to("200000")
56+
.build(),
57+
Mutation.newInsertOrUpdateBuilder("Albums")
58+
.set("SingerId")
59+
.to("2")
60+
.set("AlbumId")
61+
.to("2")
62+
.set("MarketingBudget")
63+
.to("400000")
64+
.build()
65+
), Options.maxCommitDelay(Duration.ofMillis(100)));
66+
67+
System.out.println(
68+
"Updated data with timestamp + " + commitResponse.getCommitTimestamp() + ".");
69+
}
70+
}
71+
// [END spanner_set_max_commit_delay]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.spanner;
18+
19+
import static com.example.spanner.SampleRunner.runSample;
20+
import static com.google.common.truth.Truth.assertThat;
21+
22+
import com.google.cloud.spanner.DatabaseClient;
23+
import com.google.cloud.spanner.DatabaseId;
24+
import com.google.cloud.spanner.KeySet;
25+
import com.google.cloud.spanner.Mutation;
26+
import com.google.common.collect.ImmutableList;
27+
import java.util.Arrays;
28+
import java.util.Collections;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
/**
37+
* Integration tests for {@link SetMaxCommitDelaySample}
38+
*/
39+
@RunWith(JUnit4.class)
40+
public class SetMaxCommitDelaySampleIT extends SampleTestBase {
41+
42+
private static DatabaseId databaseId;
43+
44+
@BeforeClass
45+
public static void createTestDatabase() throws Exception {
46+
final String database = idGenerator.generateDatabaseId();
47+
databaseAdminClient
48+
.createDatabase(
49+
instanceId,
50+
database,
51+
ImmutableList.of(
52+
"CREATE TABLE Singers ("
53+
+ " SingerId INT64 NOT NULL,"
54+
+ " FirstName STRING(1024),"
55+
+ " LastName STRING(1024),"
56+
+ " SingerInfo BYTES(MAX)"
57+
+ ") PRIMARY KEY (SingerId)",
58+
"CREATE TABLE Albums ("
59+
+ " SingerId INT64 NOT NULL,"
60+
+ " AlbumId INT64 NOT NULL,"
61+
+ " AlbumTitle STRING(MAX),"
62+
+ " MarketingBudget INT64"
63+
+ ") PRIMARY KEY (SingerId, AlbumId),"
64+
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"))
65+
.get();
66+
databaseId = DatabaseId.of(projectId, instanceId, database);
67+
}
68+
69+
@Before
70+
public void insertTestData() {
71+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
72+
client.write(Arrays.asList(
73+
Mutation.newInsertBuilder("Singers")
74+
.set("SingerId")
75+
.to(1L)
76+
.set("FirstName")
77+
.to("first name 1")
78+
.set("LastName")
79+
.to("last name 1")
80+
.build(),
81+
Mutation.newInsertBuilder("Singers")
82+
.set("SingerId")
83+
.to(2L)
84+
.set("FirstName")
85+
.to("first name 2")
86+
.set("LastName")
87+
.to("last name 2")
88+
.build()
89+
));
90+
}
91+
92+
@After
93+
public void removeTestData() {
94+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
95+
client.write(Collections.singletonList(Mutation.delete("Singers", KeySet.all())));
96+
}
97+
98+
@Test
99+
public void testSetMaxCommitDelaySample() throws Exception {
100+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
101+
final String out = runSample(() -> SetMaxCommitDelaySample.setMaxCommitDelay(client));
102+
103+
assertThat(out).contains("Updated data");
104+
}
105+
}

0 commit comments

Comments
 (0)