Skip to content

Commit 5ccfc3a

Browse files
authored
spanner: Add snippets for ReadContext (#3662)
spanner: Add snippets for ReadContext
1 parent ccf68f3 commit 5ccfc3a

File tree

2 files changed

+189
-1
lines changed

2 files changed

+189
-1
lines changed

google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ReadContext.java

+65-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,18 @@ enum QueryAnalyzeMode {
4343
* to the first {@link ResultSet#next()} call. Regardless of blocking behavior, any {@link
4444
* SpannerException} is deferred to the first or subsequent {@link ResultSet#next()} call.
4545
*
46-
* <p>TODO(user): Code examples.
46+
* <!--SNIPPET read_context_read-->
47+
* <pre>{@code
48+
* ReadContext readContext = dbClient.singleUse();
49+
* ResultSet resultSet =
50+
* readContext.read(
51+
* "Albums",
52+
* // KeySet.all() can be used to read all rows in a table. KeySet exposes other
53+
* // methods to read only a subset of the table.
54+
* KeySet.all(),
55+
* Arrays.asList("SingerId", "AlbumId", "AlbumTitle"));
56+
* }</pre>
57+
* <!--SNIPPET read_context_read-->
4758
*
4859
* @param table the name of the table to read
4960
* @param keys the keys and ranges of rows to read. Regardless of ordering in {@code keys}, rows
@@ -61,6 +72,15 @@ enum QueryAnalyzeMode {
6172
* to the first {@link ResultSet#next()} call. Regardless of blocking behavior, any {@link
6273
* SpannerException} is deferred to the first or subsequent {@link ResultSet#next()} call.
6374
*
75+
* <!--SNIPPET read_context_read_index-->
76+
* <pre>{@code
77+
* ReadContext readContext = dbClient.singleUse();
78+
* Struct row =
79+
* readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
80+
* Arrays.asList("AlbumId", "AlbumTitle"));
81+
* }</pre>
82+
* <!--SNIPPET read_context_read_index-->
83+
*
6484
* @param table the name of the table to read
6585
* @param index the name of the index on {@code table} to use
6686
* @param keys the keys and ranges of index rows to read. Regardless of ordering in {@code keys},
@@ -74,6 +94,14 @@ ResultSet readUsingIndex(
7494
/**
7595
* Reads a single row from a database, returning {@code null} if the row does not exist.
7696
*
97+
* <!--SNIPPET read_context_read_row-->
98+
* <pre>{@code
99+
* ReadContext readContext = dbClient.singleUse();
100+
* Struct row =
101+
* readContext.readRow("Albums", Key.of(2, 1), Arrays.asList("MarketingBudget"));
102+
* }</pre>
103+
* <!--SNIPPET read_context_read_row-->
104+
*
77105
* @param table the name of the table to read
78106
* @param key the row to read
79107
* @param columns the columns to return
@@ -85,6 +113,15 @@ ResultSet readUsingIndex(
85113
* Reads a single row from a database using an index, returning {@code null} if the row does not
86114
* exist.
87115
*
116+
* <!--SNIPPET read_context_read_index-->
117+
* <pre>{@code
118+
* ReadContext readContext = dbClient.singleUse();
119+
* Struct row =
120+
* readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
121+
* Arrays.asList("AlbumId", "AlbumTitle"));
122+
* }</pre>
123+
* <!--SNIPPET read_context_read_index-->
124+
*
88125
* @param table the name of the table to read
89126
* @param index the name of the index on {@code table} to use
90127
* @param key the index row to read
@@ -101,6 +138,18 @@ ResultSet readUsingIndex(
101138
* is deferred to the first {@link ResultSet#next()} call. Regardless of blocking behavior, any
102139
* {@link SpannerException} is deferred to the first or subsequent {@link ResultSet#next()} call.
103140
*
141+
* <!--SNIPPET read_context_execute_query-->
142+
* <pre>{@code
143+
* // Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to
144+
* // null.
145+
* ReadContext readContext = dbClient.singleUse();
146+
* ResultSet resultSet =
147+
* readContext.executeQuery(
148+
* Statement.of(
149+
* "SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums"));
150+
* }</pre>
151+
* <!--SNIPPET read_context_execute_query-->
152+
*
104153
* @param statement the query statement to execute
105154
* @param options the options to configure the query
106155
*/
@@ -113,6 +162,21 @@ ResultSet readUsingIndex(
113162
* com.google.spanner.v1.ResultSetStats} that can be accessed by calling {@link
114163
* ResultSet#getStats()} on the returned {@code ResultSet}.
115164
*
165+
* <!--SNIPPET read_context_analyze_query-->
166+
* <pre>{@code
167+
* ReadContext rc = dbClient.singleUse();
168+
* ResultSet resultSet =
169+
* rc.analyzeQuery(
170+
* Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"),
171+
* ReadContext.QueryAnalyzeMode.PROFILE);
172+
* while (resultSet.next()) {
173+
* // Discard the results. We're only processing because getStats() below requires it.
174+
* resultSet.getCurrentRowAsStruct();
175+
* }
176+
* ResultSetStats stats = resultSet.getStats();
177+
* }</pre>
178+
* <!--SNIPPET read_context_analyze_query-->
179+
*
116180
* @param statement the query statement to execute
117181
* @param queryMode the mode in which to execute the query
118182
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2018 Google LLC
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 spanner/ReadContext's javadoc. Any change
20+
* to this file should be reflected in spanner/ReadContext's javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.spanner.snippets;
24+
25+
import com.google.cloud.spanner.DatabaseClient;
26+
import com.google.cloud.spanner.Key;
27+
import com.google.cloud.spanner.KeySet;
28+
import com.google.cloud.spanner.ReadContext;
29+
import com.google.cloud.spanner.ResultSet;
30+
import com.google.cloud.spanner.Statement;
31+
import com.google.cloud.spanner.Struct;
32+
import com.google.spanner.v1.ResultSetStats;
33+
import java.util.Arrays;
34+
35+
/**
36+
* This class contains snippets for {@link com.google.cloud.spanner.ReadContext} interface.
37+
*/
38+
public class ReadContextSnippets {
39+
private final DatabaseClient dbClient;
40+
41+
public ReadContextSnippets(DatabaseClient dbClient) {
42+
this.dbClient = dbClient;
43+
}
44+
45+
ResultSet read() {
46+
// [START read_context_read]
47+
ReadContext readContext = dbClient.singleUse();
48+
ResultSet resultSet =
49+
readContext.read(
50+
"Albums",
51+
// KeySet.all() can be used to read all rows in a table. KeySet exposes other
52+
// methods to read only a subset of the table.
53+
KeySet.all(),
54+
Arrays.asList("SingerId", "AlbumId", "AlbumTitle"));
55+
// [END read_context_read]
56+
57+
return resultSet;
58+
}
59+
60+
ResultSet readUsingIndex() {
61+
// [START read_context_read_index]
62+
ReadContext readContext = dbClient.singleUse();
63+
ResultSet resultSet =
64+
readContext.readUsingIndex(
65+
"Albums",
66+
"AlbumsByAlbumTitle",
67+
KeySet.all(),
68+
Arrays.asList("AlbumId", "AlbumTitle"));
69+
// [END read_context_read_index]
70+
71+
return resultSet;
72+
}
73+
74+
Struct readRow() {
75+
// [START read_context_read_row]
76+
ReadContext readContext = dbClient.singleUse();
77+
Struct row =
78+
readContext.readRow("Albums", Key.of(2, 1), Arrays.asList("MarketingBudget"));
79+
// [END read_context_read_row]
80+
81+
return row;
82+
}
83+
84+
Struct readRowUsingIndex() {
85+
// [START read_context_read_index]
86+
ReadContext readContext = dbClient.singleUse();
87+
Struct row =
88+
readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
89+
Arrays.asList("AlbumId", "AlbumTitle"));
90+
// [END read_context_read_index]
91+
92+
return row;
93+
}
94+
95+
ResultSet executeQuery() {
96+
// [START read_context_execute_query]
97+
// Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to
98+
// null.
99+
ReadContext readContext = dbClient.singleUse();
100+
ResultSet resultSet =
101+
readContext.executeQuery(
102+
Statement.of(
103+
"SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums"));
104+
// [END read_context_execute_query]
105+
return resultSet;
106+
}
107+
108+
ResultSetStats analyzeQuery() {
109+
// [START read_context_analyze_query]
110+
ReadContext rc = dbClient.singleUse();
111+
ResultSet resultSet =
112+
rc.analyzeQuery(
113+
Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"),
114+
ReadContext.QueryAnalyzeMode.PROFILE);
115+
while (resultSet.next()) {
116+
// Discard the results. We're only processing because getStats() below requires it.
117+
resultSet.getCurrentRowAsStruct();
118+
}
119+
ResultSetStats stats = resultSet.getStats();
120+
// [END read_context_analyze_query]
121+
return stats;
122+
}
123+
}
124+

0 commit comments

Comments
 (0)