Skip to content
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 ReadContext #3662

Merged
merged 2 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ enum QueryAnalyzeMode {
* to the first {@link ResultSet#next()} call. Regardless of blocking behavior, any {@link
* SpannerException} is deferred to the first or subsequent {@link ResultSet#next()} call.
*
* <p>TODO(user): Code examples.
* <!--SNIPPET read_context_read-->
* <pre>{@code
* ReadContext readContext = dbClient.singleUse();
* ResultSet resultSet =
* readContext.read(
* "Albums",
* // KeySet.all() can be used to read all rows in a table. KeySet exposes other
* // methods to read only a subset of the table.
* KeySet.all(),
* Arrays.asList("SingerId", "AlbumId", "AlbumTitle"));
* }</pre>
* <!--SNIPPET read_context_read-->
*
* @param table the name of the table to read
* @param keys the keys and ranges of rows to read. Regardless of ordering in {@code keys}, rows
Expand All @@ -61,6 +72,15 @@ enum QueryAnalyzeMode {
* to the first {@link ResultSet#next()} call. Regardless of blocking behavior, any {@link
* SpannerException} is deferred to the first or subsequent {@link ResultSet#next()} call.
*
* <!--SNIPPET read_context_read_index-->
* <pre>{@code
* ReadContext readContext = dbClient.singleUse();
* Struct row =
* readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
* Arrays.asList("AlbumId", "AlbumTitle"));
* }</pre>
* <!--SNIPPET read_context_read_index-->
*
* @param table the name of the table to read
* @param index the name of the index on {@code table} to use
* @param keys the keys and ranges of index rows to read. Regardless of ordering in {@code keys},
Expand All @@ -74,6 +94,14 @@ ResultSet readUsingIndex(
/**
* Reads a single row from a database, returning {@code null} if the row does not exist.
*
* <!--SNIPPET read_context_read_row-->
* <pre>{@code
* ReadContext readContext = dbClient.singleUse();
* Struct row =
* readContext.readRow("Albums", Key.of(2, 1), Arrays.asList("MarketingBudget"));
* }</pre>
* <!--SNIPPET read_context_read_row-->
*
* @param table the name of the table to read
* @param key the row to read
* @param columns the columns to return
Expand All @@ -85,6 +113,15 @@ ResultSet readUsingIndex(
* Reads a single row from a database using an index, returning {@code null} if the row does not
* exist.
*
* <!--SNIPPET read_context_read_index-->
* <pre>{@code
* ReadContext readContext = dbClient.singleUse();
* Struct row =
* readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
* Arrays.asList("AlbumId", "AlbumTitle"));
* }</pre>
* <!--SNIPPET read_context_read_index-->
*
* @param table the name of the table to read
* @param index the name of the index on {@code table} to use
* @param key the index row to read
Expand All @@ -101,6 +138,18 @@ ResultSet readUsingIndex(
* is deferred to the first {@link ResultSet#next()} call. Regardless of blocking behavior, any
* {@link SpannerException} is deferred to the first or subsequent {@link ResultSet#next()} call.
*
* <!--SNIPPET read_context_execute_query-->
* <pre>{@code
* // Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to
* // null.
* ReadContext readContext = dbClient.singleUse();
* ResultSet resultSet =
* readContext.executeQuery(
* Statement.of(
* "SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums"));
* }</pre>
* <!--SNIPPET read_context_execute_query-->
*
* @param statement the query statement to execute
* @param options the options to configure the query
*/
Expand All @@ -113,6 +162,21 @@ ResultSet readUsingIndex(
* com.google.spanner.v1.ResultSetStats} that can be accessed by calling {@link
* ResultSet#getStats()} on the returned {@code ResultSet}.
*
* <!--SNIPPET read_context_analyze_query-->
* <pre>{@code
* ReadContext rc = dbClient.singleUse();
* ResultSet resultSet =
* rc.analyzeQuery(
* Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"),
* ReadContext.QueryAnalyzeMode.PROFILE);
* while (resultSet.next()) {
* // Discard the results. We're only processing because getStats() below requires it.
* resultSet.getCurrentRowAsStruct();
* }
* ResultSetStats stats = resultSet.getStats();
* }</pre>
* <!--SNIPPET read_context_analyze_query-->
*
* @param statement the query statement to execute
* @param queryMode the mode in which to execute the query
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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/ReadContext's javadoc. Any change
* to this file should be reflected in spanner/ReadContext's javadoc.
*/

package com.google.cloud.examples.spanner.snippets;

import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.Key;
import com.google.cloud.spanner.KeySet;
import com.google.cloud.spanner.ReadContext;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.Struct;
import com.google.spanner.v1.ResultSetStats;
import java.util.Arrays;

/**
* This class contains snippets for {@link com.google.cloud.spanner.ReadContext} interface.
*/
public class ReadContextSnippets {
private final DatabaseClient dbClient;

public ReadContextSnippets(DatabaseClient dbClient) {
this.dbClient = dbClient;
}

ResultSet read() {
// [START read_context_read]
ReadContext readContext = dbClient.singleUse();
ResultSet resultSet =
readContext.read(
"Albums",
// KeySet.all() can be used to read all rows in a table. KeySet exposes other
// methods to read only a subset of the table.
KeySet.all(),
Arrays.asList("SingerId", "AlbumId", "AlbumTitle"));
// [END read_context_read]

return resultSet;
}

ResultSet readUsingIndex() {
// [START read_context_read_index]
ReadContext readContext = dbClient.singleUse();
ResultSet resultSet =
readContext.readUsingIndex(
"Albums",
"AlbumsByAlbumTitle",
KeySet.all(),
Arrays.asList("AlbumId", "AlbumTitle"));
// [END read_context_read_index]

return resultSet;
}

Struct readRow() {
// [START read_context_read_row]
ReadContext readContext = dbClient.singleUse();
Struct row =
readContext.readRow("Albums", Key.of(2, 1), Arrays.asList("MarketingBudget"));
// [END read_context_read_row]

return row;
}

Struct readRowUsingIndex() {
// [START read_context_read_index]
ReadContext readContext = dbClient.singleUse();
Struct row =
readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
Arrays.asList("AlbumId", "AlbumTitle"));
// [END read_context_read_index]

return row;
}

ResultSet executeQuery() {
// [START read_context_execute_query]
// Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to
// null.
ReadContext readContext = dbClient.singleUse();
ResultSet resultSet =
readContext.executeQuery(
Statement.of(
"SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums"));
// [END read_context_execute_query]
return resultSet;
}

ResultSetStats analyzeQuery() {
// [START read_context_analyze_query]
ReadContext rc = dbClient.singleUse();
ResultSet resultSet =
rc.analyzeQuery(
Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"),
ReadContext.QueryAnalyzeMode.PROFILE);
while (resultSet.next()) {
// Discard the results. We're only processing because getStats() below requires it.
resultSet.getCurrentRowAsStruct();
}
ResultSetStats stats = resultSet.getStats();
// [END read_context_analyze_query]
return stats;
}
}