From 48a6ed028a56302d6dc6554b2ecdabc8fbdb68cf Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Thu, 25 May 2023 21:32:59 +0530 Subject: [PATCH] docs(samples): add bigtable filter snippet (#1762) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../java/com/example/bigtable/Filters.java | 33 ++++++++++++++++++- .../com/example/bigtable/FiltersTest.java | 3 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/Filters.java b/samples/snippets/src/main/java/com/example/bigtable/Filters.java index 9b0829f887..d5a2a9c2b0 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/samples/snippets/src/main/java/com/example/bigtable/Filters.java @@ -27,8 +27,10 @@ import com.google.cloud.bigtable.data.v2.models.Row; import com.google.cloud.bigtable.data.v2.models.RowCell; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.time.temporal.ChronoUnit; +import java.util.Base64; public class Filters { @@ -46,6 +48,7 @@ public static void filterLimitRowSample() { 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); + readRowFilter(projectId, instanceId, tableId, filter); readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_limit_row_sample] @@ -67,6 +70,7 @@ public static void filterLimitRowRegex(String projectId, String instanceId, Stri // [END bigtable_filters_limit_row_regex] // [START bigtable_filters_limit_cells_per_col] + // [START bigtable_hw_create_filter] public static void filterLimitCellsPerCol() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project-id"; @@ -80,6 +84,7 @@ public static void filterLimitCellsPerCol(String projectId, String instanceId, S Filter filter = FILTERS.limit().cellsPerColumn(2); readFilter(projectId, instanceId, tableId, filter); } + // [END bigtable_hw_create_filter] // [END bigtable_filters_limit_cells_per_col] // [START bigtable_filters_limit_cells_per_row] @@ -354,6 +359,25 @@ public static void filterComposingCondition(String projectId, String instanceId, // [END bigtable_filters_composing_condition] // [END_EXCLUDE] + // [START bigtable_hw_get_with_filter] + private static void readRowFilter( + 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. + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + String rowKey = + Base64.getEncoder().encodeToString("greeting0".getBytes(StandardCharsets.UTF_8)); + Row row = dataClient.readRow(tableId, rowKey, filter); + printRow(row); + System.out.println("Row filter completed."); + } catch (IOException e) { + System.out.println( + "Unable to initialize service client, as a network error occurred: \n" + e); + } + } + // [END bigtable_hw_get_with_filter] + + // [START bigtable_hw_scan_with_filter] 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 @@ -365,13 +389,19 @@ private static void readFilter( 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.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e); } } + // [END bigtable_hw_scan_with_filter] + // [START bigtable_print_row] 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()) { @@ -390,5 +420,6 @@ private static void printRow(Row row) { } System.out.println(); } + // [END bigtable_print_row] } // [END bigtable_filters_print] diff --git a/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java index e94602ec2b..968140f4c1 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -43,7 +43,8 @@ public void testFilterRowSample() { Filters.filterLimitRowSample(projectId, instanceId, TABLE_ID); String output = bout.toString(); - assertThat(output).contains("Reading data for"); + assertThat(output).contains("Row filter completed."); + assertThat(output).contains("Table filter completed."); } @Test