Skip to content

Commit 9f249be

Browse files
chore: move snippets from Filters to hw class (#1764)
* 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>
1 parent 9548e87 commit 9f249be

File tree

3 files changed

+82
-28
lines changed

3 files changed

+82
-28
lines changed

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
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;
3130
import java.time.Instant;
3231
import java.time.temporal.ChronoUnit;
33-
import java.util.Base64;
3432

3533
public class Filters {
3634

@@ -48,7 +46,6 @@ public static void filterLimitRowSample() {
4846
public static void filterLimitRowSample(String projectId, String instanceId, String tableId) {
4947
// A filter that matches cells from a row with probability .75
5048
Filter filter = FILTERS.key().sample(.75);
51-
readRowFilter(projectId, instanceId, tableId, filter);
5249
readFilter(projectId, instanceId, tableId, filter);
5350
}
5451
// [END bigtable_filters_limit_row_sample]
@@ -70,7 +67,6 @@ public static void filterLimitRowRegex(String projectId, String instanceId, Stri
7067
// [END bigtable_filters_limit_row_regex]
7168

7269
// [START bigtable_filters_limit_cells_per_col]
73-
// [START bigtable_hw_create_filter]
7470
public static void filterLimitCellsPerCol() {
7571
// TODO(developer): Replace these variables before running the sample.
7672
String projectId = "my-project-id";
@@ -84,7 +80,6 @@ public static void filterLimitCellsPerCol(String projectId, String instanceId, S
8480
Filter filter = FILTERS.limit().cellsPerColumn(2);
8581
readFilter(projectId, instanceId, tableId, filter);
8682
}
87-
// [END bigtable_hw_create_filter]
8883
// [END bigtable_filters_limit_cells_per_col]
8984

9085
// [START bigtable_filters_limit_cells_per_row]
@@ -359,25 +354,6 @@ public static void filterComposingCondition(String projectId, String instanceId,
359354
// [END bigtable_filters_composing_condition]
360355
// [END_EXCLUDE]
361356

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]
381357
private static void readFilter(
382358
String projectId, String instanceId, String tableId, Filter filter) {
383359
// Initialize client that will be used to send requests. This client only needs to be created
@@ -395,9 +371,7 @@ private static void readFilter(
395371
"Unable to initialize service client, as a network error occurred: \n" + e);
396372
}
397373
}
398-
// [END bigtable_hw_scan_with_filter]
399374

400-
// [START bigtable_print_row]
401375
private static void printRow(Row row) {
402376
if (row == null) {
403377
return;
@@ -420,6 +394,5 @@ private static void printRow(Row row) {
420394
}
421395
System.out.println();
422396
}
423-
// [END bigtable_print_row]
424397
}
425398
// [END bigtable_filters_print]

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@
1717
package com.example.bigtable;
1818

1919
// [START bigtable_hw_imports]
20+
21+
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
22+
2023
import com.google.api.gax.rpc.NotFoundException;
2124
import com.google.api.gax.rpc.ServerStream;
2225
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
2326
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
2427
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
2528
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2629
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
30+
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
2731
import com.google.cloud.bigtable.data.v2.models.Query;
2832
import com.google.cloud.bigtable.data.v2.models.Row;
2933
import com.google.cloud.bigtable.data.v2.models.RowCell;
3034
import com.google.cloud.bigtable.data.v2.models.RowMutation;
3135
import java.io.IOException;
36+
import java.nio.charset.StandardCharsets;
3237
import java.util.ArrayList;
38+
import java.util.Base64;
3339
import java.util.List;
3440

3541
// [END bigtable_hw_imports]
@@ -53,6 +59,8 @@ public class HelloWorld {
5359
private static final String COLUMN_QUALIFIER_GREETING = "greeting";
5460
private static final String COLUMN_QUALIFIER_NAME = "name";
5561
private static final String ROW_KEY_PREFIX = "rowKey";
62+
private final String projectId;
63+
private final String instanceId;
5664
private final String tableId;
5765
private final BigtableDataClient dataClient;
5866
private final BigtableTableAdminClient adminClient;
@@ -72,6 +80,8 @@ public static void main(String[] args) throws Exception {
7280

7381
public HelloWorld(String projectId, String instanceId, String tableId) throws IOException {
7482
this.tableId = tableId;
83+
this.projectId = projectId;
84+
this.instanceId = instanceId;
7585

7686
// [START bigtable_hw_connect]
7787
// Creates the settings to configure a bigtable data client.
@@ -99,6 +109,7 @@ public void run() throws Exception {
99109
readSingleRow();
100110
readSpecificCells();
101111
readTable();
112+
filterLimitCellsPerCol(this.projectId, this.instanceId, tableId);
102113
deleteTable();
103114
close();
104115
}
@@ -209,6 +220,52 @@ public List<Row> readTable() {
209220
// [END bigtable_hw_scan_all]
210221
}
211222

223+
// [START bigtable_hw_create_filter]
224+
public static void filterLimitCellsPerCol(String projectId, String instanceId, String tableId) {
225+
// A filter that matches only the most recent cell within each column
226+
Filter filter = FILTERS.limit().cellsPerColumn(1);
227+
readRowFilter(projectId, instanceId, tableId, filter);
228+
readFilter(projectId, instanceId, tableId, filter);
229+
}
230+
// [END bigtable_hw_create_filter]
231+
232+
// [START bigtable_hw_get_with_filter]
233+
private static void readRowFilter(
234+
String projectId, String instanceId, String tableId, Filter filter) {
235+
// Initialize client that will be used to send requests. This client only needs to be created
236+
// once, and can be reused for multiple requests.
237+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
238+
String rowKey =
239+
Base64.getEncoder().encodeToString("greeting0".getBytes(StandardCharsets.UTF_8));
240+
Row row = dataClient.readRow(tableId, rowKey, filter);
241+
printRow(row);
242+
System.out.println("Row filter completed.");
243+
} catch (IOException e) {
244+
System.out.println(
245+
"Unable to initialize service client, as a network error occurred: \n" + e);
246+
}
247+
}
248+
// [END bigtable_hw_get_with_filter]
249+
250+
// [START bigtable_hw_scan_with_filter]
251+
private static void readFilter(
252+
String projectId, String instanceId, String tableId, Filter filter) {
253+
// Initialize client that will be used to send requests. This client only needs to be created
254+
// once, and can be reused for multiple requests.
255+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
256+
Query query = Query.create(tableId).filter(filter);
257+
ServerStream<Row> rows = dataClient.readRows(query);
258+
for (Row row : rows) {
259+
printRow(row);
260+
}
261+
System.out.println("Table filter completed.");
262+
} catch (IOException e) {
263+
System.out.println(
264+
"Unable to initialize service client, as a network error occurred: \n" + e);
265+
}
266+
}
267+
// [END bigtable_hw_scan_with_filter]
268+
212269
/** Demonstrates how to delete a table. */
213270
public void deleteTable() {
214271
// [START bigtable_hw_delete_table]
@@ -221,4 +278,29 @@ public void deleteTable() {
221278
}
222279
// [END bigtable_hw_delete_table]
223280
}
281+
282+
// [START bigtable_print_row]
283+
private static void printRow(Row row) {
284+
if (row == null) {
285+
return;
286+
}
287+
System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8());
288+
String colFamily = "";
289+
for (RowCell cell : row.getCells()) {
290+
if (!cell.getFamily().equals(colFamily)) {
291+
colFamily = cell.getFamily();
292+
System.out.printf("Column Family %s%n", colFamily);
293+
}
294+
String labels =
295+
cell.getLabels().size() == 0 ? "" : " [" + String.join(",", cell.getLabels()) + "]";
296+
System.out.printf(
297+
"\t%s: %s @%s%s%n",
298+
cell.getQualifier().toStringUtf8(),
299+
cell.getValue().toStringUtf8(),
300+
cell.getTimestamp(),
301+
labels);
302+
}
303+
System.out.println();
304+
}
305+
// [END bigtable_print_row]
224306
}

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

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

4545
String output = bout.toString();
46-
assertThat(output).contains("Row filter completed.");
4746
assertThat(output).contains("Table filter completed.");
4847
}
4948

0 commit comments

Comments
 (0)