Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(samples): add bigtable filter snippet #1762

Merged
merged 7 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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