-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
spanner: Add snippets for Spanner, BatchClient and BatchReadOnlyTransaction #3611
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.