Skip to content

Commit 48a6ed0

Browse files
docs(samples): add bigtable filter snippet (#1762)
* docs(samples): add region tag and sample for filter snippets * lint fix * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor * lint fix * removed comment --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent a5d4215 commit 48a6ed0

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

samples/snippets/src/main/java/com/example/bigtable/Filters.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import com.google.cloud.bigtable.data.v2.models.Row;
2828
import com.google.cloud.bigtable.data.v2.models.RowCell;
2929
import java.io.IOException;
30+
import java.nio.charset.StandardCharsets;
3031
import java.time.Instant;
3132
import java.time.temporal.ChronoUnit;
33+
import java.util.Base64;
3234

3335
public class Filters {
3436

@@ -46,6 +48,7 @@ public static void filterLimitRowSample() {
4648
public static void filterLimitRowSample(String projectId, String instanceId, String tableId) {
4749
// A filter that matches cells from a row with probability .75
4850
Filter filter = FILTERS.key().sample(.75);
51+
readRowFilter(projectId, instanceId, tableId, filter);
4952
readFilter(projectId, instanceId, tableId, filter);
5053
}
5154
// [END bigtable_filters_limit_row_sample]
@@ -67,6 +70,7 @@ public static void filterLimitRowRegex(String projectId, String instanceId, Stri
6770
// [END bigtable_filters_limit_row_regex]
6871

6972
// [START bigtable_filters_limit_cells_per_col]
73+
// [START bigtable_hw_create_filter]
7074
public static void filterLimitCellsPerCol() {
7175
// TODO(developer): Replace these variables before running the sample.
7276
String projectId = "my-project-id";
@@ -80,6 +84,7 @@ public static void filterLimitCellsPerCol(String projectId, String instanceId, S
8084
Filter filter = FILTERS.limit().cellsPerColumn(2);
8185
readFilter(projectId, instanceId, tableId, filter);
8286
}
87+
// [END bigtable_hw_create_filter]
8388
// [END bigtable_filters_limit_cells_per_col]
8489

8590
// [START bigtable_filters_limit_cells_per_row]
@@ -354,6 +359,25 @@ public static void filterComposingCondition(String projectId, String instanceId,
354359
// [END bigtable_filters_composing_condition]
355360
// [END_EXCLUDE]
356361

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]
357381
private static void readFilter(
358382
String projectId, String instanceId, String tableId, Filter filter) {
359383
// Initialize client that will be used to send requests. This client only needs to be created
@@ -365,13 +389,19 @@ private static void readFilter(
365389
for (Row row : rows) {
366390
printRow(row);
367391
}
392+
System.out.println("Table filter completed.");
368393
} catch (IOException e) {
369394
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);
371396
}
372397
}
398+
// [END bigtable_hw_scan_with_filter]
373399

400+
// [START bigtable_print_row]
374401
private static void printRow(Row row) {
402+
if (row == null) {
403+
return;
404+
}
375405
System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8());
376406
String colFamily = "";
377407
for (RowCell cell : row.getCells()) {
@@ -390,5 +420,6 @@ private static void printRow(Row row) {
390420
}
391421
System.out.println();
392422
}
423+
// [END bigtable_print_row]
393424
}
394425
// [END bigtable_filters_print]

samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public void testFilterRowSample() {
4343
Filters.filterLimitRowSample(projectId, instanceId, TABLE_ID);
4444

4545
String output = bout.toString();
46-
assertThat(output).contains("Reading data for");
46+
assertThat(output).contains("Row filter completed.");
47+
assertThat(output).contains("Table filter completed.");
4748
}
4849

4950
@Test

0 commit comments

Comments
 (0)