Skip to content

Commit 413f445

Browse files
committed
docs(samples): add samples and tests for change streams txn exclusion
1 parent 0777f99 commit 413f445

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2021 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 com.google.cloud.spanner.DatabaseClient;
20+
import com.google.cloud.spanner.DatabaseId;
21+
import com.google.cloud.spanner.Options;
22+
import com.google.cloud.spanner.Spanner;
23+
import com.google.cloud.spanner.SpannerOptions;
24+
import com.google.cloud.spanner.Statement;
25+
26+
/**
27+
* Sample showing how to set exclude transaction from change streams in different write requests.
28+
*/
29+
public class ChangeStreamsTxnExclusionSample {
30+
31+
static void setExcludeTxnFromChangeStreams() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
final String projectId = "my-instance";
34+
final String instanceId = "my-project";
35+
final String databaseId = "my-database";
36+
37+
try (Spanner spanner =
38+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
39+
final DatabaseClient databaseClient =
40+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
41+
rwTxnExcludedFromChangeStreams(databaseClient);
42+
}
43+
}
44+
45+
// [START spanner_set_exclude_txn_from_change_streams]
46+
static void rwTxnExcludedFromChangeStreams(DatabaseClient client) {
47+
// Exclude the transaction from allowed tracking change streams with alloww_txn_exclusion=true.
48+
// This exclusion will be applied to all the individual operations inside this transaction.
49+
client
50+
.readWriteTransaction(Options.excludeTxnFromChangeStreams())
51+
.run(
52+
transaction -> {
53+
transaction.executeUpdate(
54+
Statement.of(
55+
"INSERT Singers (SingerId, FirstName, LastName)\n"
56+
+ "VALUES (1341, 'Virginia', 'Watson')"));
57+
System.out.println("New singer inserted.");
58+
59+
transaction.executeUpdate(
60+
Statement.of("UPDATE Singers SET FirstName = 'Hi' WHERE SingerId = 111"));
61+
System.out.println("Singer first name updated.");
62+
63+
return null;
64+
});
65+
}
66+
// [END spanner_set_exclude_txn_from_change_streams]
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
/** Integration tests for {@link ChangeStreamsTxnExclusionSample} */
37+
@RunWith(JUnit4.class)
38+
public class ChangeStreamsTxnExclusionSampleIT extends SampleTestBase {
39+
40+
private static DatabaseId databaseId;
41+
42+
@BeforeClass
43+
public static void createTestDatabase() throws Exception {
44+
final String database = idGenerator.generateDatabaseId();
45+
databaseAdminClient
46+
.createDatabase(
47+
instanceId,
48+
database,
49+
ImmutableList.of(
50+
"CREATE TABLE Singers ("
51+
+ " SingerId INT64 NOT NULL,"
52+
+ " FirstName STRING(1024),"
53+
+ " LastName STRING(1024),"
54+
+ " SingerInfo BYTES(MAX)"
55+
+ ") PRIMARY KEY (SingerId)"))
56+
.get();
57+
databaseId = DatabaseId.of(projectId, instanceId, database);
58+
}
59+
60+
@Before
61+
public void insertTestData() {
62+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
63+
client.write(
64+
Arrays.asList(
65+
Mutation.newInsertBuilder("Singers")
66+
.set("SingerId")
67+
.to(1L)
68+
.set("FirstName")
69+
.to("first name 1")
70+
.set("LastName")
71+
.to("last name 1")
72+
.build(),
73+
Mutation.newInsertBuilder("Singers")
74+
.set("SingerId")
75+
.to(2L)
76+
.set("FirstName")
77+
.to("first name 2")
78+
.set("LastName")
79+
.to("last name 2")
80+
.build()));
81+
}
82+
83+
@After
84+
public void removeTestData() {
85+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
86+
client.write(Collections.singletonList(Mutation.delete("Singers", KeySet.all())));
87+
}
88+
89+
@Test
90+
public void testSetExcludeTxnFromChangeStreamsSampleSample() throws Exception {
91+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
92+
String out =
93+
runSample(() -> ChangeStreamsTxnExclusionSample.rwTxnExcludedFromChangeStreams(client));
94+
assertThat(out).contains("New singer inserted.");
95+
assertThat(out).contains("Singer first name updated.");
96+
}
97+
}

0 commit comments

Comments
 (0)