Skip to content

Commit

Permalink
chore: move snippets from Filters to hw class (#1764)
Browse files Browse the repository at this point in the history
* chore: move snippets from Filters to hw class

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* fix tests

* fix tests

* fix test

---------

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 9548e87 commit 9f249be
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
27 changes: 0 additions & 27 deletions samples/snippets/src/main/java/com/example/bigtable/Filters.java
Expand Up @@ -27,10 +27,8 @@
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 @@ -48,7 +46,6 @@ 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 @@ -70,7 +67,6 @@ 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 @@ -84,7 +80,6 @@ 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 @@ -359,25 +354,6 @@ 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 @@ -395,9 +371,7 @@ private static void readFilter(
"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;
Expand All @@ -420,6 +394,5 @@ private static void printRow(Row row) {
}
System.out.println();
}
// [END bigtable_print_row]
}
// [END bigtable_filters_print]
Expand Up @@ -17,19 +17,25 @@
package com.example.bigtable;

// [START bigtable_hw_imports]

import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;

import com.google.api.gax.rpc.NotFoundException;
import com.google.api.gax.rpc.ServerStream;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

// [END bigtable_hw_imports]
Expand All @@ -53,6 +59,8 @@ public class HelloWorld {
private static final String COLUMN_QUALIFIER_GREETING = "greeting";
private static final String COLUMN_QUALIFIER_NAME = "name";
private static final String ROW_KEY_PREFIX = "rowKey";
private final String projectId;
private final String instanceId;
private final String tableId;
private final BigtableDataClient dataClient;
private final BigtableTableAdminClient adminClient;
Expand All @@ -72,6 +80,8 @@ public static void main(String[] args) throws Exception {

public HelloWorld(String projectId, String instanceId, String tableId) throws IOException {
this.tableId = tableId;
this.projectId = projectId;
this.instanceId = instanceId;

// [START bigtable_hw_connect]
// Creates the settings to configure a bigtable data client.
Expand Down Expand Up @@ -99,6 +109,7 @@ public void run() throws Exception {
readSingleRow();
readSpecificCells();
readTable();
filterLimitCellsPerCol(this.projectId, this.instanceId, tableId);
deleteTable();
close();
}
Expand Down Expand Up @@ -209,6 +220,52 @@ public List<Row> readTable() {
// [END bigtable_hw_scan_all]
}

// [START bigtable_hw_create_filter]
public static void filterLimitCellsPerCol(String projectId, String instanceId, String tableId) {
// A filter that matches only the most recent cell within each column
Filter filter = FILTERS.limit().cellsPerColumn(1);
readRowFilter(projectId, instanceId, tableId, filter);
readFilter(projectId, instanceId, tableId, filter);
}
// [END bigtable_hw_create_filter]

// [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
// once, and can be reused for multiple requests.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).filter(filter);
ServerStream<Row> rows = dataClient.readRows(query);
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);
}
}
// [END bigtable_hw_scan_with_filter]

/** Demonstrates how to delete a table. */
public void deleteTable() {
// [START bigtable_hw_delete_table]
Expand All @@ -221,4 +278,29 @@ public void deleteTable() {
}
// [END bigtable_hw_delete_table]
}

// [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()) {
if (!cell.getFamily().equals(colFamily)) {
colFamily = cell.getFamily();
System.out.printf("Column Family %s%n", colFamily);
}
String labels =
cell.getLabels().size() == 0 ? "" : " [" + String.join(",", cell.getLabels()) + "]";
System.out.printf(
"\t%s: %s @%s%s%n",
cell.getQualifier().toStringUtf8(),
cell.getValue().toStringUtf8(),
cell.getTimestamp(),
labels);
}
System.out.println();
}
// [END bigtable_print_row]
}
Expand Up @@ -43,7 +43,6 @@ public void testFilterRowSample() {
Filters.filterLimitRowSample(projectId, instanceId, TABLE_ID);

String output = bout.toString();
assertThat(output).contains("Row filter completed.");
assertThat(output).contains("Table filter completed.");
}

Expand Down

0 comments on commit 9f249be

Please sign in to comment.