|
| 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.CommitResponse; |
| 20 | +import com.google.cloud.spanner.DatabaseClient; |
| 21 | +import com.google.cloud.spanner.DatabaseId; |
| 22 | +import com.google.cloud.spanner.Mutation; |
| 23 | +import com.google.cloud.spanner.Options; |
| 24 | +import com.google.cloud.spanner.Spanner; |
| 25 | +import com.google.cloud.spanner.SpannerOptions; |
| 26 | +import com.google.cloud.spanner.Statement; |
| 27 | +import com.google.cloud.spanner.TransactionContext; |
| 28 | +import com.google.cloud.spanner.TransactionManager; |
| 29 | +import java.util.Collections; |
| 30 | +import com.google.spanner.v1.BatchWriteResponse; |
| 31 | +import com.google.api.gax.rpc.ServerStream; |
| 32 | +import com.google.rpc.Code; |
| 33 | +import com.google.cloud.spanner.MutationGroup; |
| 34 | +import com.google.common.collect.ImmutableList; |
| 35 | + |
| 36 | +/** Sample showing how to set exclude transaction from change streams in different write requests. */ |
| 37 | +public class ChangeStreamsTxnExclusionSample { |
| 38 | + |
| 39 | + static void setExcludeTxnFromChangeStreams() { |
| 40 | + // TODO(developer): Replace these variables before running the sample. |
| 41 | + final String projectId = "span-cloud-testing"; |
| 42 | + final String instanceId = "weideng-test"; |
| 43 | + final String databaseId = "my-database"; |
| 44 | + |
| 45 | + try (Spanner spanner = |
| 46 | + SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { |
| 47 | + final DatabaseClient databaseClient = |
| 48 | + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); |
| 49 | + rwTxnExcludedFromChangeStreams(databaseClient); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // [START spanner_set_exclude_txn_from_change_streams] |
| 54 | + static void rwTxnExcludedFromChangeStreams(DatabaseClient client) { |
| 55 | + // Exclude the transaction from allowed tracking change streams with alloww_txn_exclusion=true. |
| 56 | + // This exclusion will be applied to all the individual operations inside this transaction. |
| 57 | + client |
| 58 | + .readWriteTransaction(Options.excludeTxnFromChangeStreams()) |
| 59 | + .run( |
| 60 | + transaction -> { |
| 61 | + transaction.executeUpdate( |
| 62 | + Statement.of( |
| 63 | + "INSERT Singers (SingerId, FirstName, LastName)\n" |
| 64 | + + "VALUES (1341, 'Virginia', 'Watson')")); |
| 65 | + System.out.println("New singer inserted."); |
| 66 | + |
| 67 | + transaction.executeUpdate( |
| 68 | + Statement.of("UPDATE Singers SET FirstName = 'Hi' WHERE SingerId = 111")); |
| 69 | + System.out.println("Singer first name updated."); |
| 70 | + |
| 71 | + return null; |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + static void writeExcludedFromChangeStreams(DatabaseClient client) { |
| 76 | + CommitResponse response = |
| 77 | + client.writeWithOptions( |
| 78 | + Collections.singletonList( |
| 79 | + Mutation.newInsertOrUpdateBuilder("Singers") |
| 80 | + .set("SingerId") |
| 81 | + .to(4520) |
| 82 | + .set("FirstName") |
| 83 | + .to("Lauren") |
| 84 | + .set("LastName") |
| 85 | + .to("Lee") |
| 86 | + .build()), |
| 87 | + Options.excludeTxnFromChangeStreams()); |
| 88 | + System.out.println("New singer inserted."); |
| 89 | + } |
| 90 | + |
| 91 | + static void writeAtLeastOnceExcludedFromChangeStreams(DatabaseClient client) { |
| 92 | + CommitResponse response = |
| 93 | + client.writeAtLeastOnceWithOptions( |
| 94 | + Collections.singletonList( |
| 95 | + Mutation.newInsertOrUpdateBuilder("Singers") |
| 96 | + .set("SingerId") |
| 97 | + .to(45201) |
| 98 | + .set("FirstName") |
| 99 | + .to("Laura") |
| 100 | + .set("LastName") |
| 101 | + .to("Johnson") |
| 102 | + .build()), |
| 103 | + Options.excludeTxnFromChangeStreams()); |
| 104 | + System.out.println("New singer inserted."); |
| 105 | + } |
| 106 | + |
| 107 | + static void batchWriteAtLeastOnceExcludedFromChangeStreams(DatabaseClient client) { |
| 108 | + ServerStream<BatchWriteResponse> responses = |
| 109 | + client.batchWriteAtLeastOnce( |
| 110 | + ImmutableList.of(MutationGroup.of( |
| 111 | + Mutation.newInsertOrUpdateBuilder("Singers") |
| 112 | + .set("SingerId") |
| 113 | + .to(116) |
| 114 | + .set("FirstName") |
| 115 | + .to("Scarlet") |
| 116 | + .set("LastName") |
| 117 | + .to("Terry") |
| 118 | + .build())), |
| 119 | + Options.excludeTxnFromChangeStreams()); |
| 120 | + for (BatchWriteResponse response : responses) { |
| 121 | + if (response.getStatus().getCode() != Code.OK_VALUE) { |
| 122 | + System.out.printf( |
| 123 | + "Mutation group could not be applied with error code %s and " |
| 124 | + + "error message %s", |
| 125 | + Code.forNumber(response.getStatus().getCode()), response.getStatus().getMessage()); |
| 126 | + } |
| 127 | + } |
| 128 | + System.out.println("New singer inserted."); |
| 129 | + } |
| 130 | + |
| 131 | + static void pdmlExcludedFromChangeStreams(DatabaseClient client) { |
| 132 | + client.executePartitionedUpdate( |
| 133 | + Statement.of("DELETE FROM Singers WHERE TRUE"), Options.excludeTxnFromChangeStreams()); |
| 134 | + System.out.println("Singers deleted."); |
| 135 | + } |
| 136 | + |
| 137 | + static void txnManagerExcludedFromChangeStreams(DatabaseClient client) { |
| 138 | + try (TransactionManager manager = client.transactionManager(Options.excludeTxnFromChangeStreams())) { |
| 139 | + TransactionContext transaction = manager.begin(); |
| 140 | + transaction.buffer( |
| 141 | + Mutation.newInsertOrUpdateBuilder("Singers") |
| 142 | + .set("SingerId") |
| 143 | + .to(888) |
| 144 | + .set("FirstName") |
| 145 | + .to("Johnson") |
| 146 | + .set("LastName") |
| 147 | + .to("Doug") |
| 148 | + .build()); |
| 149 | + manager.commit(); |
| 150 | + System.out.println("New singer inserted."); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // [END spanner_set_exclude_txn_from_change_streams] |
| 155 | + |
| 156 | +} |
0 commit comments