-
Notifications
You must be signed in to change notification settings - Fork 81
/
Filters.java
399 lines (355 loc) · 16.2 KB
/
Filters.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* 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_filters_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.Filter;
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 com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class Filters {
// Write your code here.
// [START_EXCLUDE]
// [START bigtable_filters_limit_row_sample]
public static void filterLimitRowSample() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitRowSample(projectId, instanceId, tableId);
}
public static void filterLimitRowSample(String projectId, String instanceId, String tableId) {
// A filter that matches cells from a row with probability .75
Filter filter = FILTERS.key().sample(.75);
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_row_sample]
// [START bigtable_filters_limit_row_regex]
public static void filterLimitRowRegex() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitRowRegex(projectId, instanceId, tableId);
}
public static void filterLimitRowRegex(String projectId, String instanceId, String tableId) {
// A filter that matches cells from rows whose keys satisfy the given regex
Filter filter = FILTERS.key().regex(".*#20190501$");
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_row_regex]
// [START bigtable_filters_limit_cells_per_col]
public static void filterLimitCellsPerCol() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitCellsPerCol(projectId, instanceId, tableId);
}
public static void filterLimitCellsPerCol(String projectId, String instanceId, String tableId) {
// A filter that matches only the most recent 2 cells within each column
Filter filter = FILTERS.limit().cellsPerColumn(2);
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_cells_per_col]
// [START bigtable_filters_limit_cells_per_row]
public static void filterLimitCellsPerRow() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitCellsPerRow(projectId, instanceId, tableId);
}
public static void filterLimitCellsPerRow(String projectId, String instanceId, String tableId) {
// A filter that matches the first 2 cells of each row
Filter filter = FILTERS.limit().cellsPerRow(2);
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_cells_per_row]
// [START bigtable_filters_limit_cells_per_row_offset]
public static void filterLimitCellsPerRowOffset() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitCellsPerRowOffset(projectId, instanceId, tableId);
}
public static void filterLimitCellsPerRowOffset(
String projectId, String instanceId, String tableId) {
// A filter that skips the first 2 cells per row
Filter filter = FILTERS.offset().cellsPerRow(2);
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_cells_per_row_offset]
// [START bigtable_filters_limit_col_family_regex]
public static void filterLimitColFamilyRegex() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitColFamilyRegex(projectId, instanceId, tableId);
}
public static void filterLimitColFamilyRegex(
String projectId, String instanceId, String tableId) {
// A filter that matches cells whose column family satisfies the given regex
Filter filter = FILTERS.family().regex("stats_.*$");
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_col_family_regex]
// [START bigtable_filters_limit_col_qualifier_regex]
public static void filterLimitColQualifierRegex() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitColQualifierRegex(projectId, instanceId, tableId);
}
public static void filterLimitColQualifierRegex(
String projectId, String instanceId, String tableId) {
// A filter that matches cells whose column qualifier satisfies the given regex
Filter filter = FILTERS.qualifier().regex("connected_.*$");
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_col_qualifier_regex]
// [START bigtable_filters_limit_col_range]
public static void filterLimitColRange() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitColRange(projectId, instanceId, tableId);
}
public static void filterLimitColRange(String projectId, String instanceId, String tableId) {
// A filter that matches cells whose column qualifiers are between data_plan_01gb and
// data_plan_10gb in the column family cell_plan
Filter filter =
FILTERS
.qualifier()
.rangeWithinFamily("cell_plan")
.startClosed("data_plan_01gb")
.endOpen("data_plan_10gb");
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_col_range]
// [START bigtable_filters_limit_value_range]
public static void filterLimitValueRange() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitValueRange(projectId, instanceId, tableId);
}
public static void filterLimitValueRange(String projectId, String instanceId, String tableId) {
// A filter that matches cells whose values are between the given values
Filter filter = FILTERS.value().range().startClosed("PQ2A.190405").endClosed("PQ2A.190406");
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_value_range]
// [START bigtable_filters_limit_value_regex]
public static void filterLimitValueRegex() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitValueRegex(projectId, instanceId, tableId);
}
public static void filterLimitValueRegex(String projectId, String instanceId, String tableId) {
// A filter that matches cells whose value satisfies the given regex
Filter filter = FILTERS.value().regex("PQ2A.*$");
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_value_regex]
// [START bigtable_filters_limit_timestamp_range]
public static void filterLimitTimestampRange() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitTimestampRange(projectId, instanceId, tableId);
}
public static void filterLimitTimestampRange(
String projectId, String instanceId, String tableId) {
// Get a time representing one hour ago
long timestamp = Instant.now().minus(1, ChronoUnit.HOURS).toEpochMilli() * 1000;
// A filter that matches cells whose timestamp is from an hour ago or earlier
Filter filter = FILTERS.timestamp().range().startClosed(0L).endOpen(timestamp);
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_timestamp_range]
// [START bigtable_filters_limit_block_all]
public static void filterLimitBlockAll() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitBlockAll(projectId, instanceId, tableId);
}
public static void filterLimitBlockAll(String projectId, String instanceId, String tableId) {
// A filter that does not match any cells
Filter filter = FILTERS.block();
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_block_all]
// [START bigtable_filters_limit_pass_all]
public static void filterLimitPassAll() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterLimitPassAll(projectId, instanceId, tableId);
}
public static void filterLimitPassAll(String projectId, String instanceId, String tableId) {
// A filter that matches all cells
Filter filter = FILTERS.pass();
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_limit_pass_all]
// [START bigtable_filters_modify_strip_value]
public static void filterModifyStripValue() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterModifyStripValue(projectId, instanceId, tableId);
}
public static void filterModifyStripValue(String projectId, String instanceId, String tableId) {
// A filter that replaces the outputted cell value with the empty string
Filter filter = FILTERS.value().strip();
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_modify_strip_value]
// [START bigtable_filters_modify_apply_label]
public static void filterModifyApplyLabel() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterModifyApplyLabel(projectId, instanceId, tableId);
}
public static void filterModifyApplyLabel(String projectId, String instanceId, String tableId) {
// A filter that applies the given label to the outputted cell
Filter filter = FILTERS.label("labelled");
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_modify_apply_label]
// [START bigtable_filters_composing_chain]
public static void filterComposingChain() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterComposingChain(projectId, instanceId, tableId);
}
public static void filterComposingChain(String projectId, String instanceId, String tableId) {
// A filter that selects one cell per column AND within the column family cell_plan
Filter filter =
FILTERS
.chain()
.filter(FILTERS.limit().cellsPerColumn(1))
.filter(FILTERS.family().exactMatch("cell_plan"));
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_composing_chain]
// [START bigtable_filters_composing_interleave]
public static void filterComposingInterleave() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterComposingInterleave(projectId, instanceId, tableId);
}
public static void filterComposingInterleave(
String projectId, String instanceId, String tableId) {
// A filter that matches cells with the value true OR with the column qualifier os_build
Filter filter =
FILTERS
.interleave()
.filter(FILTERS.value().exactMatch("true"))
.filter(FILTERS.qualifier().exactMatch("os_build"));
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_composing_interleave]
// [START bigtable_filters_composing_condition]
public static void filterComposingCondition() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String instanceId = "my-instance-id";
String tableId = "mobile-time-series";
filterComposingCondition(projectId, instanceId, tableId);
}
public static void filterComposingCondition(String projectId, String instanceId, String tableId) {
// A filter that applies the label passed-filter IF the cell has the column qualifier
// data_plan_10gb AND the value true, OTHERWISE applies the label filtered-out
Filter filter =
FILTERS
.condition(
FILTERS
.chain()
.filter(FILTERS.qualifier().exactMatch("data_plan_10gb"))
.filter(FILTERS.value().exactMatch("true")))
.then(FILTERS.label("passed-filter"))
.otherwise(FILTERS.label("filtered-out"));
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_filters_composing_condition]
// [END_EXCLUDE]
private static void readFilter(
String projectId, String instanceId, String tableId, Filter filter) {
// 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.of(tableId)).filter(filter);
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
System.out.println("Table filter completed.");
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e);
}
}
private static void printRow(Row row) {
if (row == null) {
return;
}
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);
}
String labels =
cell.getLabels().size() == 0 ? "" : " [" + String.join(",", cell.getLabels()) + "]";
System.out.printf(
"\t%s: %s @%s%s%n",
cell.getQualifier().toStringUtf8(),
cell.getValue().toStringUtf8(),
cell.getTimestamp(),
labels);
}
System.out.println();
}
}
// [END bigtable_filters_print]