From 5ccfc3a36964a2533cfa6ac4745ea80e8c790751 Mon Sep 17 00:00:00 2001 From: Nithin Sujir <118742+nithinsujir@users.noreply.github.com> Date: Thu, 13 Sep 2018 14:45:27 -0700 Subject: [PATCH] spanner: Add snippets for ReadContext (#3662) spanner: Add snippets for ReadContext --- .../com/google/cloud/spanner/ReadContext.java | 66 +++++++++- .../spanner/snippets/ReadContextSnippets.java | 124 ++++++++++++++++++ 2 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/spanner/snippets/ReadContextSnippets.java diff --git a/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ReadContext.java b/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ReadContext.java index 4e221b1ea999..a484e8297834 100644 --- a/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ReadContext.java +++ b/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ReadContext.java @@ -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. * - *

TODO(user): Code examples. + * + *

{@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"));
+   * }
+ * * * @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 @@ -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. * + * + *
{@code
+   * ReadContext readContext = dbClient.singleUse();
+   * Struct row =
+   *     readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
+   *         Arrays.asList("AlbumId", "AlbumTitle"));
+   * }
+ * + * * @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}, @@ -74,6 +94,14 @@ ResultSet readUsingIndex( /** * Reads a single row from a database, returning {@code null} if the row does not exist. * + * + *
{@code
+   * ReadContext readContext = dbClient.singleUse();
+   * Struct row =
+   *     readContext.readRow("Albums", Key.of(2, 1), Arrays.asList("MarketingBudget"));
+   * }
+ * + * * @param table the name of the table to read * @param key the row to read * @param columns the columns to return @@ -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. * + * + *
{@code
+   * ReadContext readContext = dbClient.singleUse();
+   * Struct row =
+   *     readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
+   *         Arrays.asList("AlbumId", "AlbumTitle"));
+   * }
+ * + * * @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 @@ -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. * + * + *
{@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"));
+   * }
+ * + * * @param statement the query statement to execute * @param options the options to configure the query */ @@ -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}. * + * + *
{@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();
+   * }
+ * + * * @param statement the query statement to execute * @param queryMode the mode in which to execute the query */ diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/spanner/snippets/ReadContextSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/spanner/snippets/ReadContextSnippets.java new file mode 100644 index 000000000000..3aecca889d0d --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/spanner/snippets/ReadContextSnippets.java @@ -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; + } +} +