forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spanner: Add snippets for Spanner, BatchClient and BatchReadOnlyTrans…
…action (googleapis#3611)
- Loading branch information
1 parent
997d2a3
commit 1f12a83
Showing
5 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...xamples/src/main/java/com/google/cloud/examples/spanner/snippets/BatchClientSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2018 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. | ||
*/ | ||
|
||
/* | ||
* EDITING INSTRUCTIONS | ||
* This file is referenced in spanner/BatchClient's javadoc. Any change | ||
* to this file should be reflected in spanner/BatchClient's javadoc. | ||
*/ | ||
|
||
package com.google.cloud.examples.spanner.snippets; | ||
|
||
import com.google.cloud.spanner.BatchClient; | ||
import com.google.cloud.spanner.BatchReadOnlyTransaction; | ||
import com.google.cloud.spanner.BatchTransactionId; | ||
import com.google.cloud.spanner.KeySet; | ||
import com.google.cloud.spanner.Partition; | ||
import com.google.cloud.spanner.PartitionOptions; | ||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.Statement; | ||
import com.google.cloud.spanner.TimestampBound; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* This class contains snippets for {@link com.google.cloud.spanner.BatchClient} interface. | ||
*/ | ||
public class BatchClientSnippets { | ||
|
||
private final BatchClient batchClient; | ||
|
||
public BatchClientSnippets(BatchClient batchClient) { | ||
this.batchClient = batchClient; | ||
} | ||
|
||
/** | ||
* Example to do a batch strong read. | ||
*/ | ||
BatchReadOnlyTransaction readStrong() { | ||
// [START batch_client_strong_read] | ||
BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong()); | ||
// [END batch_client_strong_read] | ||
return txn; | ||
} | ||
|
||
/** | ||
* Example to do a batch read with txn id. | ||
*/ | ||
BatchReadOnlyTransaction readWithId(BatchReadOnlyTransaction my_txn) { | ||
// [START batch_client_read_with_id] | ||
BatchTransactionId txnId = my_txn.getBatchTransactionId(); | ||
BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(txnId); | ||
// [END batch_client_read_with_id] | ||
|
||
return txn; | ||
} | ||
|
||
void partitionQuery() { | ||
// [START partition_query] | ||
final BatchReadOnlyTransaction txn = | ||
batchClient.batchReadOnlyTransaction(TimestampBound.strong()); | ||
List<Partition> partitions = txn.partitionQuery(PartitionOptions.getDefaultInstance(), | ||
Statement.of("SELECT SingerId, FirstName, LastName FROM Singers")); | ||
|
||
for (final Partition p : partitions) { | ||
try (ResultSet results = txn.execute(p)) { | ||
while (results.next()) { | ||
long singerId = results.getLong(0); | ||
String firstName = results.getString(1); | ||
String lastName = results.getString(2); | ||
System.out.println("[" + singerId + "] " + firstName + " " + lastName); | ||
} | ||
} | ||
} | ||
// [END partition_query] | ||
|
||
} | ||
|
||
void partitionRead() { | ||
// [START partition_read] | ||
final BatchReadOnlyTransaction txn = | ||
batchClient.batchReadOnlyTransaction(TimestampBound.strong()); | ||
List<Partition> partitions = | ||
txn.partitionRead( | ||
PartitionOptions.getDefaultInstance(), | ||
"Singers", | ||
KeySet.all(), | ||
Arrays.asList("SingerId", "FirstName", "LastName")); | ||
for (final Partition p : partitions) { | ||
try (ResultSet results = txn.execute(p)) { | ||
while (results.next()) { | ||
long singerId = results.getLong(0); | ||
String firstName = results.getString(1); | ||
String lastName = results.getString(2); | ||
System.out.println("P2 [" + singerId + "] " + firstName + " " + lastName); | ||
} | ||
} | ||
} | ||
// [END partition_read] | ||
} | ||
|
||
void partitionReadUsingIndex() { | ||
// [START partition_read_using_index] | ||
final BatchReadOnlyTransaction txn = | ||
batchClient.batchReadOnlyTransaction(TimestampBound.strong()); | ||
List<Partition> partitions = | ||
txn.partitionReadUsingIndex( | ||
PartitionOptions.getDefaultInstance(), | ||
"Singers", | ||
"SingerId", | ||
KeySet.all(), | ||
Arrays.asList("FirstName")); | ||
BatchTransactionId txnID = txn.getBatchTransactionId(); | ||
int numRowsRead = 0; | ||
for (Partition p : partitions) { | ||
BatchReadOnlyTransaction batchTxnOnEachWorker = batchClient.batchReadOnlyTransaction(txnID); | ||
try (ResultSet results = batchTxnOnEachWorker.execute(p)) { | ||
while (results.next()) { | ||
System.out.println(results.getString(0)); | ||
} | ||
} | ||
} | ||
// [END partition_read_using_index] | ||
} | ||
} | ||
|
Oops, something went wrong.