Skip to content

Commit

Permalink
docs(samples): add bigtable filter snippet (#1762)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Sita04 and gcf-owl-bot[bot] committed May 25, 2023
1 parent a5d4215 commit 48a6ed0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
33 changes: 32 additions & 1 deletion samples/snippets/src/main/java/com/example/bigtable/Filters.java
Expand Up @@ -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 {

Expand All @@ -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]
Expand All @@ -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";
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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()) {
Expand All @@ -390,5 +420,6 @@ private static void printRow(Row row) {
}
System.out.println();
}
// [END bigtable_print_row]
}
// [END bigtable_filters_print]
Expand Up @@ -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
Expand Down

0 comments on commit 48a6ed0

Please sign in to comment.