27
27
import com .google .cloud .bigtable .data .v2 .models .Row ;
28
28
import com .google .cloud .bigtable .data .v2 .models .RowCell ;
29
29
import java .io .IOException ;
30
+ import java .nio .charset .StandardCharsets ;
30
31
import java .time .Instant ;
31
32
import java .time .temporal .ChronoUnit ;
33
+ import java .util .Base64 ;
32
34
33
35
public class Filters {
34
36
@@ -46,6 +48,7 @@ public static void filterLimitRowSample() {
46
48
public static void filterLimitRowSample (String projectId , String instanceId , String tableId ) {
47
49
// A filter that matches cells from a row with probability .75
48
50
Filter filter = FILTERS .key ().sample (.75 );
51
+ readRowFilter (projectId , instanceId , tableId , filter );
49
52
readFilter (projectId , instanceId , tableId , filter );
50
53
}
51
54
// [END bigtable_filters_limit_row_sample]
@@ -67,6 +70,7 @@ public static void filterLimitRowRegex(String projectId, String instanceId, Stri
67
70
// [END bigtable_filters_limit_row_regex]
68
71
69
72
// [START bigtable_filters_limit_cells_per_col]
73
+ // [START bigtable_hw_create_filter]
70
74
public static void filterLimitCellsPerCol () {
71
75
// TODO(developer): Replace these variables before running the sample.
72
76
String projectId = "my-project-id" ;
@@ -80,6 +84,7 @@ public static void filterLimitCellsPerCol(String projectId, String instanceId, S
80
84
Filter filter = FILTERS .limit ().cellsPerColumn (2 );
81
85
readFilter (projectId , instanceId , tableId , filter );
82
86
}
87
+ // [END bigtable_hw_create_filter]
83
88
// [END bigtable_filters_limit_cells_per_col]
84
89
85
90
// [START bigtable_filters_limit_cells_per_row]
@@ -354,6 +359,25 @@ public static void filterComposingCondition(String projectId, String instanceId,
354
359
// [END bigtable_filters_composing_condition]
355
360
// [END_EXCLUDE]
356
361
362
+ // [START bigtable_hw_get_with_filter]
363
+ private static void readRowFilter (
364
+ String projectId , String instanceId , String tableId , Filter filter ) {
365
+ // Initialize client that will be used to send requests. This client only needs to be created
366
+ // once, and can be reused for multiple requests.
367
+ try (BigtableDataClient dataClient = BigtableDataClient .create (projectId , instanceId )) {
368
+ String rowKey =
369
+ Base64 .getEncoder ().encodeToString ("greeting0" .getBytes (StandardCharsets .UTF_8 ));
370
+ Row row = dataClient .readRow (tableId , rowKey , filter );
371
+ printRow (row );
372
+ System .out .println ("Row filter completed." );
373
+ } catch (IOException e ) {
374
+ System .out .println (
375
+ "Unable to initialize service client, as a network error occurred: \n " + e );
376
+ }
377
+ }
378
+ // [END bigtable_hw_get_with_filter]
379
+
380
+ // [START bigtable_hw_scan_with_filter]
357
381
private static void readFilter (
358
382
String projectId , String instanceId , String tableId , Filter filter ) {
359
383
// Initialize client that will be used to send requests. This client only needs to be created
@@ -365,13 +389,19 @@ private static void readFilter(
365
389
for (Row row : rows ) {
366
390
printRow (row );
367
391
}
392
+ System .out .println ("Table filter completed." );
368
393
} catch (IOException e ) {
369
394
System .out .println (
370
- "Unable to initialize service client, as a network error occurred: \n " + e . toString () );
395
+ "Unable to initialize service client, as a network error occurred: \n " + e );
371
396
}
372
397
}
398
+ // [END bigtable_hw_scan_with_filter]
373
399
400
+ // [START bigtable_print_row]
374
401
private static void printRow (Row row ) {
402
+ if (row == null ) {
403
+ return ;
404
+ }
375
405
System .out .printf ("Reading data for %s%n" , row .getKey ().toStringUtf8 ());
376
406
String colFamily = "" ;
377
407
for (RowCell cell : row .getCells ()) {
@@ -390,5 +420,6 @@ private static void printRow(Row row) {
390
420
}
391
421
System .out .println ();
392
422
}
423
+ // [END bigtable_print_row]
393
424
}
394
425
// [END bigtable_filters_print]
0 commit comments