-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathReads.java
278 lines (250 loc) · 11.5 KB
/
Reads.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2019 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.
*/
package com.example.bigtable;
// [START bigtable_reads_print]
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
import com.google.api.gax.rpc.ServerStream;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.Filters;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import java.io.IOException;
public class Reads {
// Write your code here.
// [START_EXCLUDE]
// [START bigtable_reads_row]
public static void readRow() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readRow(projectId, instanceId, tableId);
}
public static void readRow(String projectId, String instanceId, String tableId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
String rowkey = "phone#4c410523#20190501";
Row row = dataClient.readRow(tableId, rowkey);
printRow(row);
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reads_row]
// [START bigtable_reads_row_partial]
public static void readRowPartial() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readRowPartial(projectId, instanceId, tableId);
}
public static void readRowPartial(String projectId, String instanceId, String tableId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
String rowkey = "phone#4c410523#20190501";
Filters.Filter filter =
FILTERS
.chain()
.filter(FILTERS.family().exactMatch("stats_summary"))
.filter(FILTERS.qualifier().exactMatch("os_build"));
Row row = dataClient.readRow(tableId, rowkey, filter);
printRow(row);
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reads_row_partial]
// [START bigtable_reads_rows]
public static void readRows() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readRows(projectId, instanceId, tableId);
}
public static void readRows(String projectId, String instanceId, String tableId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query =
Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502");
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reads_rows]
// [START bigtable_reads_row_range]
public static void readRowRange() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readRowRange(projectId, instanceId, tableId);
}
public static void readRowRange(String projectId, String instanceId, String tableId) {
String start = "phone#4c410523#20190501";
String end = "phone#4c410523#201906201";
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).range(start, end);
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reads_row_range]
// [START bigtable_reads_row_ranges]
public static void readRowRanges() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readRowRanges(projectId, instanceId, tableId);
}
public static void readRowRanges(String projectId, String instanceId, String tableId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query =
Query.create(tableId)
.range("phone#4c410523#20190501", "phone#4c410523#20190601")
.range("phone#5c10102#20190501", "phone#5c10102#20190601");
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reads_row_ranges]
// [START bigtable_reads_prefix]
public static void readPrefix() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readPrefix(projectId, instanceId, tableId);
}
public static void readPrefix(String projectId, String instanceId, String tableId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).prefix("phone");
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reads_prefix]
// [START bigtable_reverse_scan]
public static void readRowsReversed() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readRowsReversed(projectId, instanceId, tableId);
}
public static void readRowsReversed(String projectId, String instanceId, String tableId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query =
Query.create(tableId)
.reversed(true)
.limit(2)
.prefix("phone#4c410523")
.range("phone#5c10102", "phone#5c10103");
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reverse_scan]
// [START bigtable_reads_filter]
public static void readFilter() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
readFilter(projectId, instanceId, tableId);
}
public static void readFilter(String projectId, String instanceId, String tableId) {
Filters.Filter filter = FILTERS.value().regex("PQ2A.*");
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).filter(filter);
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
// [END bigtable_reads_filter]
// [END_EXCLUDE]
private static void printRow(Row row) {
System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8());
String colFamily = "";
for (RowCell cell : row.getCells()) {
if (!cell.getFamily().equals(colFamily)) {
colFamily = cell.getFamily();
System.out.printf("Column Family %s%n", colFamily);
}
System.out.printf(
"\t%s: %s @%s%n",
cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
}
System.out.println();
}
}
// [END bigtable_reads_print]