Skip to content

Commit 8af258d

Browse files
authored
---
yaml --- r: 8189 b: refs/heads/pubsub-streaming-pull c: 003921a h: refs/heads/master i: 8187: 570fb89
1 parent 1bd87e6 commit 8af258d

3 files changed

Lines changed: 356 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
6060
refs/heads/tswast-patch-1: 7d159fddcbc35d80aa6e78378d12a0992c8708fa
61-
refs/heads/pubsub-streaming-pull: 7f4f0429d9f3d29d2d9315c3c358acdd0216e2ac
61+
refs/heads/pubsub-streaming-pull: 003921a73de54daf36d7422373a50299c6d065c8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in DatabaseClient's javadoc. Any change to this file should be reflected
20+
* in DatabaseClient's javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.spanner.snippets;
24+
25+
import com.google.cloud.Timestamp;
26+
import com.google.cloud.spanner.DatabaseClient;
27+
import com.google.cloud.spanner.Key;
28+
import com.google.cloud.spanner.Mutation;
29+
import com.google.cloud.spanner.ReadOnlyTransaction;
30+
import com.google.cloud.spanner.Struct;
31+
import com.google.cloud.spanner.TimestampBound;
32+
import com.google.cloud.spanner.TransactionContext;
33+
import com.google.cloud.spanner.TransactionRunner;
34+
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
35+
import java.util.Collections;
36+
import java.util.concurrent.TimeUnit;
37+
38+
/**
39+
* This class contains snippets for {@link DatabaseClient} interface.
40+
*/
41+
public class DatabaseClientSnippets {
42+
43+
private final DatabaseClient dbClient;
44+
45+
public DatabaseClientSnippets(DatabaseClient dbClient) {
46+
this.dbClient = dbClient;
47+
}
48+
49+
/**
50+
* Example of blind write.
51+
*/
52+
// [TARGET write(Iterable)]
53+
// [VARIABLE my_singer_id]
54+
public void write(long singerId) {
55+
// [START write]
56+
Mutation mutation = Mutation.newInsertBuilder("Singer")
57+
.set("SingerId")
58+
.to(singerId)
59+
.set("FirstName")
60+
.to("Billy")
61+
.set("LastName")
62+
.to("Joel")
63+
.build();
64+
dbClient.write(Collections.singletonList(mutation));
65+
// [END write]
66+
}
67+
68+
/**
69+
* Example of unprotected blind write.
70+
*/
71+
// [TARGET writeAtLeastOnce(Iterable)]
72+
// [VARIABLE my_singer_id]
73+
public void writeAtLeastOnce(long singerId) {
74+
// [START writeAtLeastOnce]
75+
Mutation mutation = Mutation.newInsertBuilder("Singers")
76+
.set("SingerId")
77+
.to(singerId)
78+
.set("FirstName")
79+
.to("Billy")
80+
.set("LastName")
81+
.to("Joel")
82+
.build();
83+
dbClient.writeAtLeastOnce(Collections.singletonList(mutation));
84+
// [END writeAtLeastOnce]
85+
}
86+
87+
/**
88+
* Example of single use.
89+
*/
90+
// [TARGET singleUse()]
91+
// [VARIABLE my_singer_id]
92+
public String singleUse(long singerId) {
93+
// [START singleUse]
94+
String column = "FirstName";
95+
Struct row =
96+
dbClient.singleUse().readRow("Singers", Key.of(singerId), Collections.singleton(column));
97+
String firstName = row.getString(column);
98+
// [END singleUse]
99+
return firstName;
100+
}
101+
102+
/**
103+
* Example of single use with timestamp bound.
104+
*/
105+
// [TARGET singleUse(TimestampBound)]
106+
// [VARIABLE my_singer_id]
107+
public String singleUseStale(long singerId) {
108+
// [START singleUseStale]
109+
String column = "FirstName";
110+
Struct row =
111+
dbClient
112+
.singleUse(TimestampBound.ofMaxStaleness(10, TimeUnit.SECONDS))
113+
.readRow("Singers", Key.of(singerId), Collections.singleton(column));
114+
String firstName = row.getString(column);
115+
// [END singleUseStale]
116+
return firstName;
117+
}
118+
119+
/**
120+
* Example of single use read only transaction.
121+
*/
122+
// [TARGET singleUseReadOnlyTransaction()]
123+
// [VARIABLE my_singer_id]
124+
public Timestamp singleUseReadOnlyTransaction(long singerId) {
125+
// [START singleUseReadOnlyTransaction]
126+
String column = "FirstName";
127+
ReadOnlyTransaction txn = dbClient.singleUseReadOnlyTransaction();
128+
Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
129+
row.getString(column);
130+
Timestamp timestamp = txn.getReadTimestamp();
131+
// [END singleUseReadOnlyTransaction]
132+
return timestamp;
133+
}
134+
135+
/**
136+
* Example of single use read only transaction with timestamp bound.
137+
*/
138+
// [TARGET singleUseReadOnlyTransaction(TimestampBound)]
139+
// [VARIABLE my_singer_id]
140+
public Timestamp singleUseReadOnlyTransactionTimestamp(long singerId) {
141+
// [START singleUseReadOnlyTransactionTimestamp]
142+
String column = "FirstName";
143+
ReadOnlyTransaction txn =
144+
dbClient.singleUseReadOnlyTransaction(TimestampBound.ofMaxStaleness(10, TimeUnit.SECONDS));
145+
Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
146+
row.getString(column);
147+
Timestamp timestamp = txn.getReadTimestamp();
148+
// [END singleUseReadOnlyTransactionTimestamp]
149+
return timestamp;
150+
}
151+
152+
/**
153+
* Example of read only transaction.
154+
*/
155+
// [TARGET readOnlyTransaction()]
156+
// [VARIABLE my_singer_id]
157+
// [VARIABLE my_album_id]
158+
public String readOnlyTransaction(long singerId, long albumId) {
159+
// [START readOnlyTransaction]
160+
String singerColumn = "FirstName";
161+
String albumColumn = "AlbumTitle";
162+
String albumTitle = null;
163+
// ReadOnlyTransaction should be closed to prevent resource leak.
164+
try (ReadOnlyTransaction txn = dbClient.readOnlyTransaction()) {
165+
Struct singerRow =
166+
txn.readRow("Singers", Key.of(singerId), Collections.singleton(singerColumn));
167+
Struct albumRow =
168+
txn.readRow("Albums", Key.of(singerId, albumId), Collections.singleton(albumColumn));
169+
singerRow.getString(singerColumn);
170+
albumTitle = albumRow.getString(albumColumn);
171+
}
172+
// [END readOnlyTransaction]
173+
return albumTitle;
174+
}
175+
176+
/**
177+
* Example of read only transaction with timestamp bound.
178+
*/
179+
// [TARGET readOnlyTransaction(TimestampBound)]
180+
// [VARIABLE my_singer_id]
181+
// [VARIABLE my_album_id]
182+
public String readOnlyTransactionTimestamp(long singerId, long albumId) {
183+
// [START readOnlyTransactionTimestamp]
184+
String singerColumn = "FirstName";
185+
String albumColumn = "AlbumTitle";
186+
String albumTitle = null;
187+
// ReadOnlyTransaction should be closed to prevent resource leak.
188+
try (ReadOnlyTransaction txn =
189+
dbClient.readOnlyTransaction(TimestampBound.ofExactStaleness(10, TimeUnit.SECONDS))) {
190+
Struct singerRow =
191+
txn.readRow("Singers", Key.of(singerId), Collections.singleton(singerColumn));
192+
Struct albumRow =
193+
txn.readRow("Albums", Key.of(singerId, albumId), Collections.singleton(albumColumn));
194+
singerRow.getString(singerColumn);
195+
albumTitle = albumRow.getString(albumColumn);
196+
}
197+
// [END readOnlyTransactionTimestamp]
198+
return albumTitle;
199+
}
200+
201+
/**
202+
* Example of a read write transaction.
203+
*/
204+
// [TARGET readWriteTransaction()]
205+
// [VARIABLE my_singer_id]
206+
public void readWriteTransaction(final long singerId) {
207+
// [START readWriteTransaction]
208+
TransactionRunner runner = dbClient.readWriteTransaction();
209+
runner.run(
210+
new TransactionCallable<Void>() {
211+
212+
@Override
213+
public Void run(TransactionContext transaction) throws Exception {
214+
String column = "FirstName";
215+
Struct row =
216+
transaction.readRow("Singers", Key.of(singerId), Collections.singleton(column));
217+
String name = row.getString(column);
218+
transaction.buffer(
219+
Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
220+
return null;
221+
}
222+
});
223+
// [END readWriteTransaction]
224+
}
225+
}

0 commit comments

Comments
 (0)