From bd5766346cc9ff47b3ef5567f843e1ecdd747896 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Mon, 18 Nov 2019 16:43:11 -0500 Subject: [PATCH 1/6] WIP for Bigtable Read and Filter snippets --- bigtable/snippets/pom.xml | 5 + .../java/com/example/bigtable/Filters.java | 232 ++++++++++ .../main/java/com/example/bigtable/Reads.java | 216 +++++++++ .../example/bigtable/WriteConditionally.java | 4 +- .../com/example/bigtable/WriteIncrement.java | 4 +- .../com/example/bigtable/WriteSimple.java | 6 +- .../com/example/bigtable/FiltersTest.java | 425 ++++++++++++++++++ .../java/com/example/bigtable/ReadsTest.java | 319 +++++++++++++ .../java/com/example/bigtable/WritesTest.java | 4 +- 9 files changed, 1206 insertions(+), 9 deletions(-) create mode 100644 bigtable/snippets/src/main/java/com/example/bigtable/Filters.java create mode 100644 bigtable/snippets/src/main/java/com/example/bigtable/Reads.java create mode 100644 bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java create mode 100644 bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java diff --git a/bigtable/snippets/pom.xml b/bigtable/snippets/pom.xml index 9b603e26fcc..b7cf86fc6cd 100644 --- a/bigtable/snippets/pom.xml +++ b/bigtable/snippets/pom.xml @@ -37,6 +37,11 @@ 4.13-beta-3 test + + com.google.truth + truth + 1.0 + com.google.cloud diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java new file mode 100644 index 00000000000..7c06ebaeee4 --- /dev/null +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java @@ -0,0 +1,232 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigtable; + +import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; +// [START bigtable_filters_limit_row_sample] +// [START bigtable_filters_limit_row_regex] +// [START bigtable_filters_limit_cells_per_col] +// [START bigtable_filters_limit_cells_per_row] +// [START bigtable_filters_limit_cells_per_row_offset] +// [START bigtable_filters_limit_col_family_regex] +// [START bigtable_filters_limit_col_qualifier_regex] +// [START bigtable_filters_limit_col_range] +// [START bigtable_filters_limit_value_range] +// [START bigtable_filters_limit_value_regex] +// [START bigtable_filters_limit_timestamp_range] +import com.google.api.gax.rpc.ServerStream; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +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; + +public class Filters { + // [END bigtable_filters_limit_row_sample] + // [END bigtable_filters_limit_row_regex] + // [END bigtable_filters_limit_cells_per_col] + // [END bigtable_filters_limit_cells_per_row] + // [END bigtable_filters_limit_cells_per_row_offset] + // [END bigtable_filters_limit_col_family_regex] + // [END bigtable_filters_limit_col_qualifier_regex] + // [END bigtable_filters_limit_col_range] + // [END bigtable_filters_limit_value_range] + // [END bigtable_filters_limit_value_regex] + // [END bigtable_filters_limit_timestamp_range] + + // [START bigtable_filters_limit_row_sample] + public static void filterLimitRowSample(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.key().sample(.5); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_row_sample] + + // [START bigtable_filters_limit_row_regex] + public static void filterLimitRowRegex(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.key().regex(".*#20190501$"); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_row_regex] + + // [START bigtable_filters_limit_cells_per_col] + public static void filterLimitCellsPerCol(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.limit().cellsPerColumn(2); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_cells_per_col] + + // [START bigtable_filters_limit_cells_per_row] + public static void filterLimitCellsPerRow(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.limit().cellsPerRow(2); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_cells_per_row] + + // [START bigtable_filters_limit_cells_per_row_offset] + public static void filterLimitCellsPerRowOffset( + String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.offset().cellsPerRow(2); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_cells_per_row_offset] + + // [START bigtable_filters_limit_col_family_regex] + public static void filterLimitColFamilyRegex( + String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.family().regex("stats_.*$"); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_col_family_regex] + + // [START bigtable_filters_limit_col_qualifier_regex] + public static void filterLimitColQualifierRegex( + String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.qualifier().regex("connected_.*$"); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_col_qualifier_regex] + + // [START bigtable_filters_limit_col_range] + public static void filterLimitColRange(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = + FILTERS + .qualifier() + .rangeWithinFamily("cell_plan") + .startClosed("data_plan_01gb") + .endOpen("data_plan_10gb"); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_col_range] + + // [START bigtable_filters_limit_value_range] + public static void filterLimitValueRange(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.value().range().startClosed("PQ2A.190405").endClosed("PQ2A.190406"); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_value_range] + + // [START bigtable_filters_limit_value_regex] + public static void filterLimitValueRegex(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.value().regex("PQ2A.*$"); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_value_regex] + + // [START bigtable_filters_limit_timestamp_range] + public static void filterLimitTimestampRange( + String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + long timestamp = System.currentTimeMillis() * 1000; + long hour = 60L * 60 * 1000 * 1000; + + Filter filter = FILTERS.timestamp().range().startClosed(0L).endOpen(timestamp - hour); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_timestamp_range] + + // [START bigtable_filters_limit_row_sample] + // [START bigtable_filters_limit_row_regex] + // [START bigtable_filters_limit_cells_per_col] + // [START bigtable_filters_limit_cells_per_row] + // [START bigtable_filters_limit_cells_per_row_offset] + // [START bigtable_filters_limit_col_family_regex] + // [START bigtable_filters_limit_col_qualifier_regex] + // [START bigtable_filters_limit_col_range] + // [START bigtable_filters_limit_value_range] + // [START bigtable_filters_limit_value_regex] + // [START bigtable_filters_limit_timestamp_range] + + public static void readFilter( + String projectId, String instanceId, String tableId, Filter filter) { + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Query query = Query.create(tableId).filter(filter); + ServerStream rows = dataClient.readRows(query); + for (Row row : rows) { + printRow(row); + } + + } catch (Exception e) { + System.out.println("Error during readFilter: \n" + e.toString()); + } + } + + private static void printRow(Row row) { + System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8()); + for (RowCell cell : row.getCells()) { + System.out.printf( + "%s:%s %s @%s%n", + cell.getFamily(), + cell.getQualifier().toStringUtf8(), + cell.getValue().toStringUtf8(), + cell.getTimestamp()); + } + System.out.println(); + } +} +// [END bigtable_filters_limit_row_sample] +// [END bigtable_filters_limit_row_regex] +// [END bigtable_filters_limit_cells_per_col] +// [END bigtable_filters_limit_cells_per_row] +// [END bigtable_filters_limit_cells_per_row_offset] +// [END bigtable_filters_limit_col_family_regex] +// [END bigtable_filters_limit_col_qualifier_regex] +// [END bigtable_filters_limit_col_range] +// [END bigtable_filters_limit_value_range] +// [END bigtable_filters_limit_value_regex] +// [END bigtable_filters_limit_timestamp_range] diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java new file mode 100644 index 00000000000..f7a728e54e6 --- /dev/null +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java @@ -0,0 +1,216 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigtable; + +// [START bigtable_reads_row] +// [START bigtable_reads_row_partial] +// [START bigtable_reads_rows] +// [START bigtable_reads_row_range] +// [START bigtable_reads_row_ranges] +// [START bigtable_reads_prefix] +// [START bigtable_reads_filter] + +import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; + +import com.google.api.gax.rpc.ServerStream; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.Filters; +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 com.google.protobuf.ByteString; + +public class Reads { + // [END bigtable_reads_row] + // [END bigtable_reads_row_partial] + // [END bigtable_reads_rows] + // [END bigtable_reads_row_range] + // [END bigtable_reads_row_ranges] + // [END bigtable_reads_prefix] + // [END bigtable_reads_filter] + + // [START bigtable_reads_row] + public static void readRow(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + String rowkey = "phone#4c410523#20190501"; + + Row row = dataClient.readRow(tableId, rowkey); + printRow(row); + + } catch (Exception e) { + System.out.println("Error during readRow: \n" + e.toString()); + } + } + // [END bigtable_reads_row] + + // [START bigtable_reads_row_partial] + public static void readRowPartial(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + String rowkey = "phone#4c410523#20190501"; + Filters.Filter filter = + FILTERS + .chain() + .filter(FILTERS.family().exactMatch("stats_summary")) + .filter(FILTERS.qualifier().exactMatch("os_build")); + + Row row = dataClient.readRow(tableId, rowkey, filter); + printRow(row); + + } catch (Exception e) { + System.out.println("Error during RowPartial: \n" + e.toString()); + } + } + // [END bigtable_reads_row_partial] + + // [START bigtable_reads_rows] + public static void readRows(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Query query = + Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502"); + ServerStream rows = dataClient.readRows(query); + for (Row row : rows) { + printRow(row); + } + + } catch (Exception e) { + System.out.println("Error during readRows: \n" + e.toString()); + } + } + // [END bigtable_reads_rows] + + // [START bigtable_reads_row_range] + public static void readRowRange(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + String start = "phone#4c410523#20190501"; + String end = "phone#4c410523#201906201"; + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Query query = Query.create(tableId).range(start, end); + ServerStream rows = dataClient.readRows(query); + for (Row row : rows) { + printRow(row); + } + + } catch (Exception e) { + System.out.println("Error during readRange: \n" + e.toString()); + } + } + // [END bigtable_reads_row_range] + + // [START bigtable_reads_row_ranges] + public static void readRowRanges(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Query query = + Query.create(tableId) + .range("phone#4c410523#20190501", "phone#4c410523#20190601") + .range("phone#5c10102#20190501", "phone#5c10102#20190601"); + ServerStream rows = dataClient.readRows(query); + for (Row row : rows) { + printRow(row); + } + + } catch (Exception e) { + System.out.println("Error during readRanges: \n" + e.toString()); + } + } + // [END bigtable_reads_row_ranges] + + // [START bigtable_reads_prefix] + public static void readPrefix(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Query query = Query.create(tableId).prefix("phone"); + ServerStream rows = dataClient.readRows(query); + for (Row row : rows) { + printRow(row); + } + + } catch (Exception e) { + System.out.println("Error during readPrefix: \n" + e.toString()); + } + } + // [END bigtable_reads_prefix] + + // [START bigtable_reads_filter] + public static void readFilter(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + Filters.Filter filter = FILTERS.value().regex("PQ2A.*"); + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Query query = Query.create(tableId).filter(filter); + ServerStream rows = dataClient.readRows(query); + for (Row row : rows) { + printRow(row); + } + + } catch (Exception e) { + System.out.println("Error during readFilter: \n" + e.toString()); + } + } + // [END bigtable_reads_filter] + + // [START bigtable_reads_row] + // [START bigtable_reads_row_partial] + // [START bigtable_reads_rows] + // [START bigtable_reads_row_range] + // [START bigtable_reads_row_ranges] + // [START bigtable_reads_prefix] + // [START bigtable_reads_filter] + private static void printRow(Row row) { + System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8()); + for (RowCell cell : row.getCells()) { + System.out.printf( + "%s:%s %s @%s%n", + cell.getFamily(), + cell.getQualifier().toStringUtf8(), + cell.getValue().toStringUtf8(), + cell.getTimestamp()); + } + System.out.println(); + } +} +// [END bigtable_reads_row] +// [END bigtable_reads_row_partial] +// [END bigtable_reads_rows] +// [END bigtable_reads_row_range] +// [END bigtable_reads_row_ranges] +// [END bigtable_reads_prefix] +// [END bigtable_reads_filter] diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/WriteConditionally.java b/bigtable/snippets/src/main/java/com/example/bigtable/WriteConditionally.java index 7cc8fd22441..ac01cb0c634 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/WriteConditionally.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/WriteConditionally.java @@ -36,7 +36,7 @@ public static void writeConditionally(String projectId, String instanceId, Strin try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { long timestamp = System.currentTimeMillis() * 1000; - String rowKey = "phone#4c410523#20190501"; + String rowkey = "phone#4c410523#20190501"; Mutation mutation = Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android"); @@ -49,7 +49,7 @@ public static void writeConditionally(String projectId, String instanceId, Strin .filter(FILTERS.value().regex("PQ2A\\..*")); ConditionalRowMutation conditionalRowMutation = - ConditionalRowMutation.create(tableId, rowKey).condition(filter).then(mutation); + ConditionalRowMutation.create(tableId, rowkey).condition(filter).then(mutation); boolean success = dataClient.checkAndMutateRow(conditionalRowMutation); diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/WriteIncrement.java b/bigtable/snippets/src/main/java/com/example/bigtable/WriteIncrement.java index ec6d756b625..0f91a137177 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/WriteIncrement.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/WriteIncrement.java @@ -34,9 +34,9 @@ public static void writeIncrement(String projectId, String instanceId, String ta try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { // Get an existing row that has a cell with an incrementable value. A value can be incremented // if it is encoded as a 64-bit big-endian signed integer. - String rowKey = "phone#4c410523#20190501"; + String rowkey = "phone#4c410523#20190501"; ReadModifyWriteRow mutation = - ReadModifyWriteRow.create(tableId, rowKey) + ReadModifyWriteRow.create(tableId, rowkey) .increment(COLUMN_FAMILY_NAME, "connected_cell", -1); Row success = dataClient.readModifyWriteRow(mutation); diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/WriteSimple.java b/bigtable/snippets/src/main/java/com/example/bigtable/WriteSimple.java index 50671989218..a782542c3e7 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/WriteSimple.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/WriteSimple.java @@ -33,11 +33,11 @@ public static void writeSimple(String projectId, String instanceId, String table try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { long timestamp = System.currentTimeMillis() * 1000; - String rowKey = "phone#4c410523#20190501"; + String rowkey = "phone#4c410523#20190501"; ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1}); RowMutation rowMutation = - RowMutation.create(tableId, rowKey) + RowMutation.create(tableId, rowkey) .setCell( COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), @@ -51,7 +51,7 @@ public static void writeSimple(String projectId, String instanceId, String table .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003"); dataClient.mutateRow(rowMutation); - System.out.printf("Successfully wrote row %s", rowKey); + System.out.printf("Successfully wrote row %s", rowkey); } catch (Exception e) { System.out.println("Error during WriteSimple: \n" + e.toString()); diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java new file mode 100644 index 00000000000..2029b88f107 --- /dev/null +++ b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -0,0 +1,425 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigtable; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.BulkMutation; +import com.google.cloud.bigtable.data.v2.models.Mutation; +import com.google.protobuf.ByteString; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Filter; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +public class FiltersTest { + + private static final String INSTANCE_ENV = "BIGTABLE_TESTING_INSTANCE"; + private static final String TABLE_ID = + "mobile-time-series-" + UUID.randomUUID().toString().substring(0, 20); + private static final String COLUMN_FAMILY_NAME_STATS = "stats_summary"; + private static final String COLUMN_FAMILY_NAME_DATA = "cell_plan"; + private static final long TIMESTAMP = System.currentTimeMillis() * 1000; + private static final long HOUR = 60L * 60 * 1000 * 1000; + + private static String projectId; + private static String instanceId; + private ByteArrayOutputStream bout; + + private static String requireEnv(String varName) { + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + System.getenv(varName)); + return System.getenv(varName); + } + + @BeforeClass + public static void beforeClass() { + projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); + instanceId = requireEnv(INSTANCE_ENV); + + try (BigtableTableAdminClient adminClient = + BigtableTableAdminClient.create(projectId, instanceId)) { + CreateTableRequest createTableRequest = + CreateTableRequest.of(TABLE_ID) + .addFamily(COLUMN_FAMILY_NAME_STATS) + .addFamily(COLUMN_FAMILY_NAME_DATA); + adminClient.createTable(createTableRequest); + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + BulkMutation bulkMutation = + BulkMutation.create(TABLE_ID) + .add( + "phone#4c410523#20190501", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190405.003") + .setCell( + COLUMN_FAMILY_NAME_DATA, "data_plan_01gb", TIMESTAMP - HOUR, "true") + .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_01gb", TIMESTAMP, "false") + .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_05gb", TIMESTAMP, "true")) + .add( + "phone#4c410523#20190502", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190405.004") + .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_05gb", TIMESTAMP, "true")) + .add( + "phone#4c410523#20190505", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 0) + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190406.000") + .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_05gb", TIMESTAMP, "true")) + .add( + "phone#5c10102#20190501", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190401.002") + .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_10gb", TIMESTAMP, "true")) + .add( + "phone#5c10102#20190502", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME_STATS, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 0) + .setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190406.000") + .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_10gb", TIMESTAMP, "true")); + + dataClient.bulkMutateRows(bulkMutation); + } + } catch (Exception e) { + System.out.println("Error during beforeClass: \n" + e.toString()); + } + } + + @Before + public void setupStream() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @AfterClass + public static void afterClass() { + try (BigtableTableAdminClient adminClient = + BigtableTableAdminClient.create(projectId, instanceId)) { + adminClient.deleteTable(TABLE_ID); + } catch (Exception e) { + System.out.println("Error during afterClass: \n" + e.toString()); + } + } + + @Test + public void testFilterRowSample() { + Filters.filterLimitRowSample(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output).contains("Reading data for"); + } + + @Test + public void testFilterRowRegex() { + Filters.filterLimitRowRegex(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "cell_plan:data_plan_01gb false @%1$s\n" + + "cell_plan:data_plan_01gb true @%2$s\n" + + "cell_plan:data_plan_05gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "cell_plan:data_plan_10gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterCellsPerCol() { + Filters.filterLimitCellsPerCol(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "cell_plan:data_plan_01gb false @%1$s\n" + + "cell_plan:data_plan_01gb true @%2$s\n" + + "cell_plan:data_plan_05gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "cell_plan:data_plan_05gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "cell_plan:data_plan_05gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "cell_plan:data_plan_10gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "cell_plan:data_plan_10gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterCellsPerRow() { + Filters.filterLimitCellsPerRow(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "cell_plan:data_plan_01gb false @%1$s\n" + + "cell_plan:data_plan_01gb true @%2$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "cell_plan:data_plan_05gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "cell_plan:data_plan_05gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "cell_plan:data_plan_10gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "cell_plan:data_plan_10gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterLimitCellsPerRowOffset() { + Filters.filterLimitCellsPerRowOffset(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "cell_plan:data_plan_05gb true @%1$s\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } + + @Test + public void testFilterColFamilyRegex() { + Filters.filterLimitColFamilyRegex(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } + + @Test + public void testFilterColQualifierRegex() { + Filters.filterLimitColQualifierRegex(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s", + TIMESTAMP)); + } + + @Test + public void testFilterColRange() { + Filters.filterLimitColRange(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "cell_plan:data_plan_01gb false @%1$s\n" + + "cell_plan:data_plan_01gb true @%2$s\n" + + "cell_plan:data_plan_05gb true @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "cell_plan:data_plan_05gb true @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "cell_plan:data_plan_05gb true @%1$s", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterValueRange() { + Filters.filterLimitValueRange(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s", + TIMESTAMP)); + } + + @Test + public void testFilterValueRegex() { + Filters.filterLimitValueRegex(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } + + @Test + public void testFilterTimestampRange() { + Filters.filterLimitTimestampRange(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "cell_plan:data_plan_01gb true @%s\n", + TIMESTAMP - HOUR)); + } +} diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java new file mode 100644 index 00000000000..ad41b9251df --- /dev/null +++ b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java @@ -0,0 +1,319 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigtable; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.Table; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.BulkMutation; +import com.google.cloud.bigtable.data.v2.models.Mutation; +import com.google.protobuf.ByteString; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ReadsTest { + + private static final String INSTANCE_ENV = "BIGTABLE_TESTING_INSTANCE"; + private static final String TABLE_ID = + "mobile-time-series-" + UUID.randomUUID().toString().substring(0, 20); + private static final String COLUMN_FAMILY_NAME = "stats_summary"; + private static final long TIMESTAMP = System.currentTimeMillis() * 1000; + + private static String projectId; + private static String instanceId; + private ByteArrayOutputStream bout; + + private static String requireEnv(String varName) { + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + System.getenv(varName)); + return System.getenv(varName); + } + + @BeforeClass + public static void beforeClass() { + projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); + instanceId = requireEnv(INSTANCE_ENV); + + try (BigtableTableAdminClient adminClient = + BigtableTableAdminClient.create(projectId, instanceId)) { + CreateTableRequest createTableRequest = + CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME); + adminClient.createTable(createTableRequest); + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + BulkMutation bulkMutation = + BulkMutation.create(TABLE_ID) + .add( + "phone#4c410523#20190501", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP, "PQ2A.190405.003")) + .add( + "phone#4c410523#20190502", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP, "PQ2A.190405.004")) + .add( + "phone#4c410523#20190505", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 0) + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP, "PQ2A.190406.000")) + .add( + "phone#5c10102#20190501", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 1) + .setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP, "PQ2A.190401.002")) + .add( + "phone#5c10102#20190502", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_cell".getBytes()), + TIMESTAMP, + 1) + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + TIMESTAMP, + 0) + .setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP, "PQ2A.190406.000")); + + dataClient.bulkMutateRows(bulkMutation); + } + } catch (Exception e) { + System.out.println("Error during beforeClass: \n" + e.toString()); + } + } + + @Before + public void setupStream() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @AfterClass + public static void afterClass() { + try (BigtableTableAdminClient adminClient = + BigtableTableAdminClient.create(projectId, instanceId)) { + adminClient.deleteTable(TABLE_ID); + } catch (Exception e) { + System.out.println("Error during afterClass: \n" + e.toString()); + } + } + + @Test + public void testReadRow() { + Reads.readRow(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s", + TIMESTAMP)); + } + + @Test + public void testReadRowPartial() { + Reads.readRowPartial(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s", + TIMESTAMP)); + } + + @Test + public void testReadRows() { + Reads.readRows(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s", + TIMESTAMP)); + } + + @Test + public void testReadRowRange() { + Reads.readRowRange(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } + + @Test + public void testReadRowRanges() { + Reads.readRowRanges(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } + + @Test + public void testReadPrefix() { + Reads.readPrefix(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } + + @Test + public void testReadFilter() { + Reads.readFilter(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "stats_summary:os_build PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } +} diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java index afe70c4027c..2c2d2bd901e 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java @@ -46,8 +46,8 @@ public class WritesTest { private static String requireEnv(String varName) { assertNotNull( - System.getenv(varName), - "Environment variable '%s' is required to perform these tests.".format(varName)); + String.format("Environment variable '%s' is required to perform these tests.", varName), + System.getenv(varName)); return System.getenv(varName); } From 33906d4f51e37bc46bac03bbda0b1badd63dfc58 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Tue, 19 Nov 2019 14:39:36 -0500 Subject: [PATCH 2/6] Remaining Filter snippets WIP --- .../java/com/example/bigtable/Filters.java | 147 ++++- .../com/example/bigtable/FiltersTest.java | 514 ++++++++++++++---- 2 files changed, 565 insertions(+), 96 deletions(-) diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java index 7c06ebaeee4..6e6a899c81e 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java @@ -28,6 +28,14 @@ // [START bigtable_filters_limit_value_range] // [START bigtable_filters_limit_value_regex] // [START bigtable_filters_limit_timestamp_range] +// [START bigtable_filters_limit_block_all] +// [START bigtable_filters_limit_pass_all] +// [START bigtable_filters_limit_sink] +// [START bigtable_filters_modify_strip_value] +// [START bigtable_filters_modify_apply_label] +// [START bigtable_filters_composing_chain] +// [START bigtable_filters_composing_interleave] +// [START bigtable_filters_composing_condition] import com.google.api.gax.rpc.ServerStream; import com.google.cloud.bigtable.data.v2.BigtableDataClient; import com.google.cloud.bigtable.data.v2.models.Filters.Filter; @@ -47,6 +55,14 @@ public class Filters { // [END bigtable_filters_limit_value_range] // [END bigtable_filters_limit_value_regex] // [END bigtable_filters_limit_timestamp_range] + // [END bigtable_filters_limit_block_all] + // [END bigtable_filters_limit_pass_all] + // [END bigtable_filters_limit_sink] + // [END bigtable_filters_modify_strip_value] + // [END bigtable_filters_modify_apply_label] + // [END bigtable_filters_composing_chain] + // [END bigtable_filters_composing_interleave] + // [END bigtable_filters_composing_condition] // [START bigtable_filters_limit_row_sample] public static void filterLimitRowSample(String projectId, String instanceId, String tableId) { @@ -180,6 +196,107 @@ public static void filterLimitTimestampRange( } // [END bigtable_filters_limit_timestamp_range] + // [START bigtable_filters_limit_block_all] + public static void filterLimitBlockAll(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.block(); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_block_all] + + // [START bigtable_filters_limit_pass_all] + public static void filterLimitPassAll(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.pass(); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_pass_all] + + // [START bigtable_filters_limit_sink] + public static void filterLimitSink(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.sink(); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_limit_sink] + + // [START bigtable_filters_modify_strip_value] + public static void filterModifyStripValue(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.value().strip(); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_modify_strip_value] + + // [START bigtable_filters_modify_apply_label] + public static void filterModifyApplyLabel(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = FILTERS.label("my-label"); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_modify_apply_label] + + // [START bigtable_filters_composing_chain] + public static void filterComposingChain(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = + FILTERS + .chain() + .filter(FILTERS.limit().cellsPerColumn(1)) + .filter(FILTERS.family().exactMatch("cell_plan")); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_composing_chain] + + // [START bigtable_filters_composing_interleave] + public static void filterComposingInterleave( + String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = + FILTERS + .interleave() + .filter(FILTERS.limit().cellsPerColumn(1)) + .filter(FILTERS.family().exactMatch("cell_plan")); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_composing_interleave] + + // [START bigtable_filters_composing_condition] + public static void filterComposingCondition(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "mobile-time-series"; + + Filter filter = + FILTERS + .condition(FILTERS.value().regex("PQ2A.*$")) + .otherwise(FILTERS.label("my-label2")) + .then(FILTERS.label("my-label")); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_composing_condition] + // [START bigtable_filters_limit_row_sample] // [START bigtable_filters_limit_row_regex] // [START bigtable_filters_limit_cells_per_col] @@ -191,7 +308,14 @@ public static void filterLimitTimestampRange( // [START bigtable_filters_limit_value_range] // [START bigtable_filters_limit_value_regex] // [START bigtable_filters_limit_timestamp_range] - + // [START bigtable_filters_limit_block_all] + // [START bigtable_filters_limit_pass_all] + // [START bigtable_filters_limit_sink] + // [START bigtable_filters_modify_strip_value] + // [START bigtable_filters_modify_apply_label] + // [START bigtable_filters_composing_chain] + // [START bigtable_filters_composing_interleave] + // [START bigtable_filters_composing_condition] public static void readFilter( String projectId, String instanceId, String tableId, Filter filter) { try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { @@ -208,13 +332,20 @@ public static void readFilter( private static void printRow(Row row) { System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8()); + String colFamily = ""; for (RowCell cell : row.getCells()) { + if (cell.getFamily() != 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( - "%s:%s %s @%s%n", - cell.getFamily(), + "\t%s: %s @%s%s%n", cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), - cell.getTimestamp()); + cell.getTimestamp(), + labels); } System.out.println(); } @@ -230,3 +361,11 @@ private static void printRow(Row row) { // [END bigtable_filters_limit_value_range] // [END bigtable_filters_limit_value_regex] // [END bigtable_filters_limit_timestamp_range] +// [END bigtable_filters_limit_block_all] +// [END bigtable_filters_limit_pass_all] +// [END bigtable_filters_limit_sink] +// [END bigtable_filters_modify_strip_value] +// [END bigtable_filters_modify_apply_label] +// [END bigtable_filters_composing_chain] +// [END bigtable_filters_composing_interleave] +// [END bigtable_filters_composing_condition] diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java index 2029b88f107..d1ee1ff8966 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -192,17 +192,21 @@ public void testFilterRowRegex() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "cell_plan:data_plan_01gb false @%1$s\n" - + "cell_plan:data_plan_01gb true @%2$s\n" - + "cell_plan:data_plan_05gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_01gb: true @%2$s\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "cell_plan:data_plan_10gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s", + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s", TIMESTAMP, TIMESTAMP - HOUR)); } @@ -215,32 +219,42 @@ public void testFilterCellsPerCol() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "cell_plan:data_plan_01gb false @%1$s\n" - + "cell_plan:data_plan_01gb true @%2$s\n" - + "cell_plan:data_plan_05gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_01gb: true @%2$s\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "cell_plan:data_plan_05gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "cell_plan:data_plan_05gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "cell_plan:data_plan_10gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "cell_plan:data_plan_10gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP, TIMESTAMP - HOUR)); } @@ -253,20 +267,29 @@ public void testFilterCellsPerRow() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "cell_plan:data_plan_01gb false @%1$s\n" - + "cell_plan:data_plan_01gb true @%2$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_01gb: true @%2$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "cell_plan:data_plan_05gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "cell_plan:data_plan_05gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "cell_plan:data_plan_10gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "cell_plan:data_plan_10gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n", + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n", TIMESTAMP, TIMESTAMP - HOUR)); } @@ -279,22 +302,28 @@ public void testFilterLimitCellsPerRowOffset() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "cell_plan:data_plan_05gb true @%1$s\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP)); } @@ -307,25 +336,30 @@ public void testFilterColFamilyRegex() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP)); } @@ -338,20 +372,25 @@ public void testFilterColQualifierRegex() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s", TIMESTAMP)); } @@ -364,13 +403,16 @@ public void testFilterColRange() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "cell_plan:data_plan_01gb false @%1$s\n" - + "cell_plan:data_plan_01gb true @%2$s\n" - + "cell_plan:data_plan_05gb true @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_01gb: true @%2$s\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "cell_plan:data_plan_05gb true @%1$s\n\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "cell_plan:data_plan_05gb true @%1$s", + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s", TIMESTAMP, TIMESTAMP - HOUR)); } @@ -383,9 +425,11 @@ public void testFilterValueRange() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s", + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.004 @%1$s", TIMESTAMP)); } @@ -398,15 +442,20 @@ public void testFilterValueRegex() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP)); } @@ -419,7 +468,288 @@ public void testFilterTimestampRange() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "cell_plan:data_plan_01gb true @%s\n", + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: true @%s\n", TIMESTAMP - HOUR)); } + + @Test + public void testFilterBlockAll() { + Filters.filterLimitBlockAll(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output).doesNotContain("Reading data for"); + } + + @Test + public void testFilterPassAll() { + Filters.filterLimitPassAll(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_01gb: true @%2$s\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterSink() { + Filters.filterLimitSink(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_01gb: true @%2$s\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterStripValue() { + Filters.filterModifyStripValue(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: @%1$s\n" + + "\tdata_plan_01gb: @%2$s\n" + + "\tdata_plan_05gb: @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: @%1$s\n" + + "\tconnected_wifi: @%1$s\n" + + "\tos_build: @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: @%1$s\n" + + "\tconnected_wifi: @%1$s\n" + + "\tos_build: @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: @%1$s\n" + + "\tconnected_wifi: @%1$s\n" + + "\tos_build: @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: @%1$s\n" + + "\tconnected_wifi: @%1$s\n" + + "\tos_build: @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: @%1$s\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: @%1$s\n" + + "\tconnected_wifi: @%1$s\n" + + "\tos_build: @%1$s", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterApplyLabel() { + Filters.filterModifyApplyLabel(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s [my-label]\n" + + "\tdata_plan_01gb: true @%2$s [my-label]\n" + + "\tdata_plan_05gb: true @%1$s [my-label]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tos_build: PQ2A.190405.003 @%1$s [my-label]\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s [my-label]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tos_build: PQ2A.190405.004 @%1$s [my-label]\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s [my-label]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [my-label]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tos_build: PQ2A.190406.000 @%1$s [my-label]\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s [my-label]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tos_build: PQ2A.190401.002 @%1$s [my-label]\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s [my-label]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [my-label]\n" + + "\tos_build: PQ2A.190406.000 @%1$s [my-label]", + TIMESTAMP, TIMESTAMP - HOUR)); + } + + @Test + public void testFilterChain() { + Filters.filterComposingChain(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n", + TIMESTAMP)); + } + + @Test + public void testFilterInterleave() { + Filters.filterComposingInterleave(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n", + TIMESTAMP)); + } + @Test + public void testFilterCondition() { + Filters.filterComposingCondition(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output) + .contains( + String.format( + "Reading data for phone#4c410523#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: false @%1$s\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family cell_plan\n" + + "\tdata_plan_05gb: true @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family cell_plan\n" + + "\tdata_plan_10gb: true @%1$s\n", + TIMESTAMP)); + } } From 5fa516396c1660f2c914f975b20647a228c19504 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Wed, 20 Nov 2019 14:08:41 -0500 Subject: [PATCH 3/6] All tests working, improved interleave label and condition, removed sink --- .../java/com/example/bigtable/Filters.java | 31 ++-- .../com/example/bigtable/FiltersTest.java | 151 ++++++++---------- 2 files changed, 77 insertions(+), 105 deletions(-) diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java index 6e6a899c81e..22516028c77 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java @@ -30,7 +30,6 @@ // [START bigtable_filters_limit_timestamp_range] // [START bigtable_filters_limit_block_all] // [START bigtable_filters_limit_pass_all] -// [START bigtable_filters_limit_sink] // [START bigtable_filters_modify_strip_value] // [START bigtable_filters_modify_apply_label] // [START bigtable_filters_composing_chain] @@ -57,7 +56,6 @@ public class Filters { // [END bigtable_filters_limit_timestamp_range] // [END bigtable_filters_limit_block_all] // [END bigtable_filters_limit_pass_all] - // [END bigtable_filters_limit_sink] // [END bigtable_filters_modify_strip_value] // [END bigtable_filters_modify_apply_label] // [END bigtable_filters_composing_chain] @@ -218,17 +216,6 @@ public static void filterLimitPassAll(String projectId, String instanceId, Strin } // [END bigtable_filters_limit_pass_all] - // [START bigtable_filters_limit_sink] - public static void filterLimitSink(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; - - Filter filter = FILTERS.sink(); - readFilter(projectId, instanceId, tableId, filter); - } - // [END bigtable_filters_limit_sink] - // [START bigtable_filters_modify_strip_value] public static void filterModifyStripValue(String projectId, String instanceId, String tableId) { // String projectId = "my-project-id"; @@ -246,7 +233,7 @@ public static void filterModifyApplyLabel(String projectId, String instanceId, S // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; - Filter filter = FILTERS.label("my-label"); + Filter filter = FILTERS.label("labelled"); readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_modify_apply_label] @@ -276,8 +263,8 @@ public static void filterComposingInterleave( Filter filter = FILTERS .interleave() - .filter(FILTERS.limit().cellsPerColumn(1)) - .filter(FILTERS.family().exactMatch("cell_plan")); + .filter(FILTERS.value().exactMatch("true")) + .filter(FILTERS.qualifier().exactMatch("os_build")); readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_composing_interleave] @@ -290,9 +277,13 @@ public static void filterComposingCondition(String projectId, String instanceId, Filter filter = FILTERS - .condition(FILTERS.value().regex("PQ2A.*$")) - .otherwise(FILTERS.label("my-label2")) - .then(FILTERS.label("my-label")); + .condition( + FILTERS + .chain() + .filter(FILTERS.qualifier().exactMatch("data_plan_10gb")) + .filter(FILTERS.value().exactMatch("true"))) + .then(FILTERS.label("passed-filter")) + .otherwise(FILTERS.label("filtered-out")); readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_composing_condition] @@ -310,7 +301,6 @@ public static void filterComposingCondition(String projectId, String instanceId, // [START bigtable_filters_limit_timestamp_range] // [START bigtable_filters_limit_block_all] // [START bigtable_filters_limit_pass_all] - // [START bigtable_filters_limit_sink] // [START bigtable_filters_modify_strip_value] // [START bigtable_filters_modify_apply_label] // [START bigtable_filters_composing_chain] @@ -363,7 +353,6 @@ private static void printRow(Row row) { // [END bigtable_filters_limit_timestamp_range] // [END bigtable_filters_limit_block_all] // [END bigtable_filters_limit_pass_all] -// [END bigtable_filters_limit_sink] // [END bigtable_filters_modify_strip_value] // [END bigtable_filters_modify_apply_label] // [END bigtable_filters_composing_chain] diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java index d1ee1ff8966..844aaa2e52e 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -529,54 +529,6 @@ public void testFilterPassAll() { TIMESTAMP, TIMESTAMP - HOUR)); } - @Test - public void testFilterSink() { - Filters.filterLimitSink(projectId, instanceId, TABLE_ID); - - String output = bout.toString(); - assertThat(output) - .contains( - String.format( - "Reading data for phone#4c410523#20190501\n" - + "Column Family cell_plan\n" - + "\tdata_plan_01gb: false @%1$s\n" - + "\tdata_plan_01gb: true @%2$s\n" - + "\tdata_plan_05gb: true @%1$s\n" - + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tos_build: PQ2A.190405.003 @%1$s\n\n" - + "Reading data for phone#4c410523#20190502\n" - + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s\n" - + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tos_build: PQ2A.190405.004 @%1$s\n\n" - + "Reading data for phone#4c410523#20190505\n" - + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s\n" - + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tos_build: PQ2A.190406.000 @%1$s\n\n" - + "Reading data for phone#5c10102#20190501\n" - + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s\n" - + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tos_build: PQ2A.190401.002 @%1$s\n\n" - + "Reading data for phone#5c10102#20190502\n" - + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s\n" - + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "\tos_build: PQ2A.190406.000 @%1$s", - TIMESTAMP, TIMESTAMP - HOUR)); - } - @Test public void testFilterStripValue() { Filters.filterModifyStripValue(projectId, instanceId, TABLE_ID); @@ -635,41 +587,41 @@ public void testFilterApplyLabel() { String.format( "Reading data for phone#4c410523#20190501\n" + "Column Family cell_plan\n" - + "\tdata_plan_01gb: false @%1$s [my-label]\n" - + "\tdata_plan_01gb: true @%2$s [my-label]\n" - + "\tdata_plan_05gb: true @%1$s [my-label]\n" + + "\tdata_plan_01gb: false @%1$s [labelled]\n" + + "\tdata_plan_01gb: true @%2$s [labelled]\n" + + "\tdata_plan_05gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tos_build: PQ2A.190405.003 @%1$s [my-label]\n\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tos_build: PQ2A.190405.003 @%1$s [labelled]\n\n" + "Reading data for phone#4c410523#20190502\n" + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s [my-label]\n" + + "\tdata_plan_05gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tos_build: PQ2A.190405.004 @%1$s [my-label]\n\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tos_build: PQ2A.190405.004 @%1$s [labelled]\n\n" + "Reading data for phone#4c410523#20190505\n" + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s [my-label]\n" + + "\tdata_plan_05gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [my-label]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tos_build: PQ2A.190406.000 @%1$s [my-label]\n\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tos_build: PQ2A.190406.000 @%1$s [labelled]\n\n" + "Reading data for phone#5c10102#20190501\n" + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s [my-label]\n" + + "\tdata_plan_10gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tos_build: PQ2A.190401.002 @%1$s [my-label]\n\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tos_build: PQ2A.190401.002 @%1$s [labelled]\n\n" + "Reading data for phone#5c10102#20190502\n" + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s [my-label]\n" + + "\tdata_plan_10gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [my-label]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [my-label]\n" - + "\tos_build: PQ2A.190406.000 @%1$s [my-label]", + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [labelled]\n" + + "\tos_build: PQ2A.190406.000 @%1$s [labelled]", TIMESTAMP, TIMESTAMP - HOUR)); } @@ -710,21 +662,31 @@ public void testFilterInterleave() { String.format( "Reading data for phone#4c410523#20190501\n" + "Column Family cell_plan\n" - + "\tdata_plan_01gb: false @%1$s\n" - + "\tdata_plan_05gb: true @%1$s\n\n" + + "\tdata_plan_01gb: true @%2$s\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s\n\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s\n\n" + + "\tdata_plan_05gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s\n\n" + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s\n", - TIMESTAMP)); + + "\tdata_plan_10gb: true @%1$s\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s", + TIMESTAMP, TIMESTAMP - HOUR)); } @Test public void testFilterCondition() { @@ -736,20 +698,41 @@ public void testFilterCondition() { String.format( "Reading data for phone#4c410523#20190501\n" + "Column Family cell_plan\n" - + "\tdata_plan_01gb: false @%1$s\n" - + "\tdata_plan_05gb: true @%1$s\n\n" + + "\tdata_plan_01gb: false @%1$s [filtered-out]\n" + + "\tdata_plan_01gb: true @%2$s [filtered-out]\n" + + "\tdata_plan_05gb: true @%1$s [filtered-out]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tos_build: PQ2A.190405.003 @%1$s [filtered-out]\n\n" + "Reading data for phone#4c410523#20190502\n" + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s\n\n" + + "\tdata_plan_05gb: true @%1$s [filtered-out]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tos_build: PQ2A.190405.004 @%1$s [filtered-out]\n\n" + "Reading data for phone#4c410523#20190505\n" + "Column Family cell_plan\n" - + "\tdata_plan_05gb: true @%1$s\n\n" + + "\tdata_plan_05gb: true @%1$s [filtered-out]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [filtered-out]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tos_build: PQ2A.190406.000 @%1$s [filtered-out]\n\n" + "Reading data for phone#5c10102#20190501\n" + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s\n\n" + + "\tdata_plan_10gb: true @%1$s [passed-filter]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [passed-filter]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [passed-filter]\n" + + "\tos_build: PQ2A.190401.002 @%1$s [passed-filter]\n\n" + "Reading data for phone#5c10102#20190502\n" + "Column Family cell_plan\n" - + "\tdata_plan_10gb: true @%1$s\n", - TIMESTAMP)); + + "\tdata_plan_10gb: true @%1$s [passed-filter]\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [passed-filter]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [passed-filter]\n" + + "\tos_build: PQ2A.190406.000 @%1$s [passed-filter]", + TIMESTAMP, TIMESTAMP - HOUR)); } } From c1cf376531ae4822b092585e5cf0428876c89050 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Tue, 26 Nov 2019 12:04:25 -0500 Subject: [PATCH 4/6] Changing time to java.time Adding comments about filtering Handling exceptions Reformatting Reads print output other cleanups --- bigtable/snippets/pom.xml | 1 + .../java/com/example/bigtable/Filters.java | 43 +++++- .../main/java/com/example/bigtable/Reads.java | 74 +++++++--- .../com/example/bigtable/FiltersTest.java | 38 +++-- .../java/com/example/bigtable/ReadsTest.java | 137 +++++++++++------- 5 files changed, 190 insertions(+), 103 deletions(-) diff --git a/bigtable/snippets/pom.xml b/bigtable/snippets/pom.xml index b7cf86fc6cd..e035cadc183 100644 --- a/bigtable/snippets/pom.xml +++ b/bigtable/snippets/pom.xml @@ -41,6 +41,7 @@ com.google.truth truth 1.0 + test diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java index 22516028c77..94d24bef750 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java @@ -41,6 +41,9 @@ 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 java.io.IOException; +import java.time.Instant; +import java.time.temporal.ChronoUnit; public class Filters { // [END bigtable_filters_limit_row_sample] @@ -68,6 +71,7 @@ public static void filterLimitRowSample(String projectId, String instanceId, Str // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells from a row with probability .5 Filter filter = FILTERS.key().sample(.5); readFilter(projectId, instanceId, tableId, filter); } @@ -79,6 +83,7 @@ public static void filterLimitRowRegex(String projectId, String instanceId, Stri // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells from rows whose keys satisfy the given regex Filter filter = FILTERS.key().regex(".*#20190501$"); readFilter(projectId, instanceId, tableId, filter); } @@ -90,6 +95,7 @@ public static void filterLimitCellsPerCol(String projectId, String instanceId, S // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches only the most recent 2 cells within each column Filter filter = FILTERS.limit().cellsPerColumn(2); readFilter(projectId, instanceId, tableId, filter); } @@ -101,6 +107,7 @@ public static void filterLimitCellsPerRow(String projectId, String instanceId, S // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches the first 2 cells of each row Filter filter = FILTERS.limit().cellsPerRow(2); readFilter(projectId, instanceId, tableId, filter); } @@ -113,6 +120,7 @@ public static void filterLimitCellsPerRowOffset( // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that skips the first 2 cells per row Filter filter = FILTERS.offset().cellsPerRow(2); readFilter(projectId, instanceId, tableId, filter); } @@ -125,6 +133,7 @@ public static void filterLimitColFamilyRegex( // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells whose column family satisfies the given regex Filter filter = FILTERS.family().regex("stats_.*$"); readFilter(projectId, instanceId, tableId, filter); } @@ -137,6 +146,7 @@ public static void filterLimitColQualifierRegex( // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells whose column qualifier satisfies the given regex Filter filter = FILTERS.qualifier().regex("connected_.*$"); readFilter(projectId, instanceId, tableId, filter); } @@ -148,6 +158,8 @@ public static void filterLimitColRange(String projectId, String instanceId, Stri // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells whose column qualifiers are between data_plan_01gb and + // data_plan_10gb in the column family cell_plan Filter filter = FILTERS .qualifier() @@ -164,6 +176,7 @@ public static void filterLimitValueRange(String projectId, String instanceId, St // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells whose values are between the given values Filter filter = FILTERS.value().range().startClosed("PQ2A.190405").endClosed("PQ2A.190406"); readFilter(projectId, instanceId, tableId, filter); } @@ -175,6 +188,7 @@ public static void filterLimitValueRegex(String projectId, String instanceId, St // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells whose value satisfies the given regex Filter filter = FILTERS.value().regex("PQ2A.*$"); readFilter(projectId, instanceId, tableId, filter); } @@ -186,10 +200,12 @@ public static void filterLimitTimestampRange( // String projectId = "my-project-id"; // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; - long timestamp = System.currentTimeMillis() * 1000; - long hour = 60L * 60 * 1000 * 1000; - Filter filter = FILTERS.timestamp().range().startClosed(0L).endOpen(timestamp - hour); + // Get a time representing one hour ago + long timestamp = Instant.now().minus(1, ChronoUnit.HOURS).toEpochMilli() * 1000; + + // A filter that matches cells whose timestamp is from an hour ago or earlier + Filter filter = FILTERS.timestamp().range().startClosed(0L).endOpen(timestamp); readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_limit_timestamp_range] @@ -200,6 +216,7 @@ public static void filterLimitBlockAll(String projectId, String instanceId, Stri // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that does not match any cells Filter filter = FILTERS.block(); readFilter(projectId, instanceId, tableId, filter); } @@ -211,6 +228,7 @@ public static void filterLimitPassAll(String projectId, String instanceId, Strin // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches all cells Filter filter = FILTERS.pass(); readFilter(projectId, instanceId, tableId, filter); } @@ -222,6 +240,7 @@ public static void filterModifyStripValue(String projectId, String instanceId, S // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that replaces the outputted cell value with the empty string Filter filter = FILTERS.value().strip(); readFilter(projectId, instanceId, tableId, filter); } @@ -233,6 +252,7 @@ public static void filterModifyApplyLabel(String projectId, String instanceId, S // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that applies the given label to the outputted cell Filter filter = FILTERS.label("labelled"); readFilter(projectId, instanceId, tableId, filter); } @@ -244,6 +264,7 @@ public static void filterComposingChain(String projectId, String instanceId, Str // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that selects one cell per column AND within the column family cell_plan Filter filter = FILTERS .chain() @@ -260,6 +281,7 @@ public static void filterComposingInterleave( // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that matches cells with the value true OR with the column qualifier os_build Filter filter = FILTERS .interleave() @@ -275,6 +297,8 @@ public static void filterComposingCondition(String projectId, String instanceId, // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // A filter that applies the label passed-filter IF the cell has the column qualifier + // data_plan_10gb AND the value true, OTHERWISE applies the label filtered-out Filter filter = FILTERS .condition( @@ -306,17 +330,20 @@ public static void filterComposingCondition(String projectId, String instanceId, // [START bigtable_filters_composing_chain] // [START bigtable_filters_composing_interleave] // [START bigtable_filters_composing_condition] - public static void readFilter( + 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. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { Query query = Query.create(tableId).filter(filter); ServerStream rows = dataClient.readRows(query); for (Row row : rows) { printRow(row); } - - } catch (Exception e) { - System.out.println("Error during readFilter: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } @@ -324,7 +351,7 @@ private static void printRow(Row row) { System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8()); String colFamily = ""; for (RowCell cell : row.getCells()) { - if (cell.getFamily() != colFamily) { + if (!cell.getFamily().equals(colFamily)) { colFamily = cell.getFamily(); System.out.printf("Column Family %s%n", colFamily); } diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java index f7a728e54e6..30bfb8ca796 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java @@ -34,6 +34,7 @@ import com.google.cloud.bigtable.data.v2.models.RowCell; import com.google.cloud.bigtable.data.v2.models.RowMutation; import com.google.protobuf.ByteString; +import java.io.IOException; public class Reads { // [END bigtable_reads_row] @@ -50,14 +51,18 @@ public static void readRow(String projectId, String instanceId, String tableId) // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { String rowkey = "phone#4c410523#20190501"; Row row = dataClient.readRow(tableId, rowkey); printRow(row); - } catch (Exception e) { - System.out.println("Error during readRow: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } // [END bigtable_reads_row] @@ -68,6 +73,9 @@ public static void readRowPartial(String projectId, String instanceId, String ta // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { String rowkey = "phone#4c410523#20190501"; Filters.Filter filter = @@ -79,8 +87,9 @@ public static void readRowPartial(String projectId, String instanceId, String ta Row row = dataClient.readRow(tableId, rowkey, filter); printRow(row); - } catch (Exception e) { - System.out.println("Error during RowPartial: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } // [END bigtable_reads_row_partial] @@ -91,6 +100,9 @@ public static void readRows(String projectId, String instanceId, String tableId) // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { Query query = Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502"); @@ -98,9 +110,9 @@ public static void readRows(String projectId, String instanceId, String tableId) for (Row row : rows) { printRow(row); } - - } catch (Exception e) { - System.out.println("Error during readRows: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } // [END bigtable_reads_rows] @@ -113,15 +125,18 @@ public static void readRowRange(String projectId, String instanceId, String tabl String start = "phone#4c410523#20190501"; String end = "phone#4c410523#201906201"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { Query query = Query.create(tableId).range(start, end); ServerStream rows = dataClient.readRows(query); for (Row row : rows) { printRow(row); } - - } catch (Exception e) { - System.out.println("Error during readRange: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } // [END bigtable_reads_row_range] @@ -132,6 +147,9 @@ public static void readRowRanges(String projectId, String instanceId, String tab // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { Query query = Query.create(tableId) @@ -141,9 +159,9 @@ public static void readRowRanges(String projectId, String instanceId, String tab for (Row row : rows) { printRow(row); } - - } catch (Exception e) { - System.out.println("Error during readRanges: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } // [END bigtable_reads_row_ranges] @@ -154,15 +172,18 @@ public static void readPrefix(String projectId, String instanceId, String tableI // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { Query query = Query.create(tableId).prefix("phone"); ServerStream rows = dataClient.readRows(query); for (Row row : rows) { printRow(row); } - - } catch (Exception e) { - System.out.println("Error during readPrefix: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } // [END bigtable_reads_prefix] @@ -174,15 +195,18 @@ public static void readFilter(String projectId, String instanceId, String tableI // String tableId = "mobile-time-series"; Filters.Filter filter = FILTERS.value().regex("PQ2A.*"); + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { Query query = Query.create(tableId).filter(filter); ServerStream rows = dataClient.readRows(query); for (Row row : rows) { printRow(row); } - - } catch (Exception e) { - System.out.println("Error during readFilter: \n" + e.toString()); + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); } } // [END bigtable_reads_filter] @@ -196,13 +220,15 @@ public static void readFilter(String projectId, String instanceId, String tableI // [START bigtable_reads_filter] private static void printRow(Row row) { 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); + } System.out.printf( - "%s:%s %s @%s%n", - cell.getFamily(), - cell.getQualifier().toStringUtf8(), - cell.getValue().toStringUtf8(), - cell.getTimestamp()); + "\t%s: %s @%s%n", + cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp()); } System.out.println(); } diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java index 844aaa2e52e..edce08eb20a 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -26,7 +26,10 @@ import com.google.cloud.bigtable.data.v2.models.Mutation; import com.google.protobuf.ByteString; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.UUID; import java.util.logging.Filter; import org.junit.AfterClass; @@ -43,8 +46,10 @@ public class FiltersTest { "mobile-time-series-" + UUID.randomUUID().toString().substring(0, 20); private static final String COLUMN_FAMILY_NAME_STATS = "stats_summary"; private static final String COLUMN_FAMILY_NAME_DATA = "cell_plan"; - private static final long TIMESTAMP = System.currentTimeMillis() * 1000; - private static final long HOUR = 60L * 60 * 1000 * 1000; + private static final Instant CURRENT_TIME = Instant.now(); + private static final long TIMESTAMP = CURRENT_TIME.toEpochMilli() * 1000; + private static final long TIMESTAMP_MINUS_HR = + CURRENT_TIME.minus(1, ChronoUnit.HOURS).toEpochMilli() * 1000; private static String projectId; private static String instanceId; @@ -58,7 +63,7 @@ private static String requireEnv(String varName) { } @BeforeClass - public static void beforeClass() { + public static void beforeClass() throws IOException { projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); instanceId = requireEnv(INSTANCE_ENV); @@ -88,7 +93,7 @@ public static void beforeClass() { 1) .setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190405.003") .setCell( - COLUMN_FAMILY_NAME_DATA, "data_plan_01gb", TIMESTAMP - HOUR, "true") + COLUMN_FAMILY_NAME_DATA, "data_plan_01gb", TIMESTAMP_MINUS_HR, "true") .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_01gb", TIMESTAMP, "false") .setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_05gb", TIMESTAMP, "true")) .add( @@ -156,6 +161,7 @@ public static void beforeClass() { } } catch (Exception e) { System.out.println("Error during beforeClass: \n" + e.toString()); + throw (e); } } @@ -166,12 +172,13 @@ public void setupStream() { } @AfterClass - public static void afterClass() { + public static void afterClass() throws IOException { try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) { adminClient.deleteTable(TABLE_ID); } catch (Exception e) { System.out.println("Error during afterClass: \n" + e.toString()); + throw (e); } } @@ -207,7 +214,7 @@ public void testFilterRowRegex() { + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + "\tos_build: PQ2A.190401.002 @%1$s", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } @Test @@ -255,7 +262,7 @@ public void testFilterCellsPerCol() { + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + "\tos_build: PQ2A.190406.000 @%1$s", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } @Test @@ -290,7 +297,7 @@ public void testFilterCellsPerRow() { + "\tdata_plan_10gb: true @%1$s\n" + "Column Family stats_summary\n" + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } @Test @@ -413,7 +420,7 @@ public void testFilterColRange() { + "Reading data for phone#4c410523#20190505\n" + "Column Family cell_plan\n" + "\tdata_plan_05gb: true @%1$s", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } @Test @@ -470,7 +477,7 @@ public void testFilterTimestampRange() { "Reading data for phone#4c410523#20190501\n" + "Column Family cell_plan\n" + "\tdata_plan_01gb: true @%s\n", - TIMESTAMP - HOUR)); + TIMESTAMP_MINUS_HR)); } @Test @@ -526,7 +533,7 @@ public void testFilterPassAll() { + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + "\tos_build: PQ2A.190406.000 @%1$s", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } @Test @@ -574,7 +581,7 @@ public void testFilterStripValue() { + "\tconnected_cell: @%1$s\n" + "\tconnected_wifi: @%1$s\n" + "\tos_build: @%1$s", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } @Test @@ -622,7 +629,7 @@ public void testFilterApplyLabel() { + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [labelled]\n" + "\tos_build: PQ2A.190406.000 @%1$s [labelled]", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } @Test @@ -686,8 +693,9 @@ public void testFilterInterleave() { + "\tdata_plan_10gb: true @%1$s\n" + "Column Family stats_summary\n" + "\tos_build: PQ2A.190406.000 @%1$s", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } + @Test public void testFilterCondition() { Filters.filterComposingCondition(projectId, instanceId, TABLE_ID); @@ -733,6 +741,6 @@ public void testFilterCondition() { + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [passed-filter]\n" + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [passed-filter]\n" + "\tos_build: PQ2A.190406.000 @%1$s [passed-filter]", - TIMESTAMP, TIMESTAMP - HOUR)); + TIMESTAMP, TIMESTAMP_MINUS_HR)); } } diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java index ad41b9251df..d97be32d282 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java @@ -27,6 +27,7 @@ import com.google.cloud.bigtable.data.v2.models.Mutation; import com.google.protobuf.ByteString; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; import java.util.UUID; import org.junit.AfterClass; @@ -57,7 +58,7 @@ private static String requireEnv(String varName) { } @BeforeClass - public static void beforeClass() { + public static void beforeClass() throws IOException { projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); instanceId = requireEnv(INSTANCE_ENV); @@ -145,6 +146,7 @@ public static void beforeClass() { } } catch (Exception e) { System.out.println("Error during beforeClass: \n" + e.toString()); + throw (e); } } @@ -155,12 +157,13 @@ public void setupStream() { } @AfterClass - public static void afterClass() { + public static void afterClass() throws IOException { try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) { adminClient.deleteTable(TABLE_ID); } catch (Exception e) { System.out.println("Error during afterClass: \n" + e.toString()); + throw (e); } } @@ -173,9 +176,10 @@ public void testReadRow() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s", TIMESTAMP)); } @@ -188,7 +192,8 @@ public void testReadRowPartial() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s", + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s", TIMESTAMP)); } @@ -201,13 +206,15 @@ public void testReadRows() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s", TIMESTAMP)); } @@ -220,17 +227,20 @@ public void testReadRowRange() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP)); } @@ -243,25 +253,30 @@ public void testReadRowRanges() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP)); } @@ -274,25 +289,30 @@ public void testReadPrefix() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "stats_summary:connected_cell \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" - + "stats_summary:connected_wifi \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family stats_summary\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP)); } @@ -305,15 +325,20 @@ public void testReadFilter() { .contains( String.format( "Reading data for phone#4c410523#20190501\n" - + "stats_summary:os_build PQ2A.190405.003 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + "Reading data for phone#4c410523#20190502\n" - + "stats_summary:os_build PQ2A.190405.004 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + "Reading data for phone#4c410523#20190505\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + "Reading data for phone#5c10102#20190501\n" - + "stats_summary:os_build PQ2A.190401.002 @%1$s\n\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + "Reading data for phone#5c10102#20190502\n" - + "stats_summary:os_build PQ2A.190406.000 @%1$s", + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s", TIMESTAMP)); } } From c150de8dd0e18e3e826cb08e11bfa9ccdf644c3e Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Tue, 26 Nov 2019 20:22:29 -0500 Subject: [PATCH 5/6] Updating function declarations --- .../java/com/example/bigtable/Filters.java | 216 ++++++++++++------ .../main/java/com/example/bigtable/Reads.java | 82 ++++--- 2 files changed, 200 insertions(+), 98 deletions(-) diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java index 94d24bef750..93367eeace7 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java @@ -66,11 +66,15 @@ public class Filters { // [END bigtable_filters_composing_condition] // [START bigtable_filters_limit_row_sample] - public static void filterLimitRowSample(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitRowSample() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitRowSample(projectId, instanceId, tableId); + } + public static void filterLimitRowSample(String projectId, String instanceId, String tableId) { // A filter that matches cells from a row with probability .5 Filter filter = FILTERS.key().sample(.5); readFilter(projectId, instanceId, tableId, filter); @@ -78,11 +82,15 @@ public static void filterLimitRowSample(String projectId, String instanceId, Str // [END bigtable_filters_limit_row_sample] // [START bigtable_filters_limit_row_regex] - public static void filterLimitRowRegex(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitRowRegex() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitRowRegex(projectId, instanceId, tableId); + } + public static void filterLimitRowRegex(String projectId, String instanceId, String tableId) { // A filter that matches cells from rows whose keys satisfy the given regex Filter filter = FILTERS.key().regex(".*#20190501$"); readFilter(projectId, instanceId, tableId, filter); @@ -90,11 +98,15 @@ public static void filterLimitRowRegex(String projectId, String instanceId, Stri // [END bigtable_filters_limit_row_regex] // [START bigtable_filters_limit_cells_per_col] - public static void filterLimitCellsPerCol(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitCellsPerCol() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitCellsPerCol(projectId, instanceId, tableId); + } + public static void filterLimitCellsPerCol(String projectId, String instanceId, String tableId) { // A filter that matches only the most recent 2 cells within each column Filter filter = FILTERS.limit().cellsPerColumn(2); readFilter(projectId, instanceId, tableId, filter); @@ -102,11 +114,15 @@ public static void filterLimitCellsPerCol(String projectId, String instanceId, S // [END bigtable_filters_limit_cells_per_col] // [START bigtable_filters_limit_cells_per_row] - public static void filterLimitCellsPerRow(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitCellsPerRow() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitCellsPerRow(projectId, instanceId, tableId); + } + public static void filterLimitCellsPerRow(String projectId, String instanceId, String tableId) { // A filter that matches the first 2 cells of each row Filter filter = FILTERS.limit().cellsPerRow(2); readFilter(projectId, instanceId, tableId, filter); @@ -114,12 +130,16 @@ public static void filterLimitCellsPerRow(String projectId, String instanceId, S // [END bigtable_filters_limit_cells_per_row] // [START bigtable_filters_limit_cells_per_row_offset] + public static void filterLimitCellsPerRowOffset() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitCellsPerRowOffset(projectId, instanceId, tableId); + } + public static void filterLimitCellsPerRowOffset( String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; - // A filter that skips the first 2 cells per row Filter filter = FILTERS.offset().cellsPerRow(2); readFilter(projectId, instanceId, tableId, filter); @@ -127,12 +147,16 @@ public static void filterLimitCellsPerRowOffset( // [END bigtable_filters_limit_cells_per_row_offset] // [START bigtable_filters_limit_col_family_regex] + public static void filterLimitColFamilyRegex() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitColFamilyRegex(projectId, instanceId, tableId); + } + public static void filterLimitColFamilyRegex( String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; - // A filter that matches cells whose column family satisfies the given regex Filter filter = FILTERS.family().regex("stats_.*$"); readFilter(projectId, instanceId, tableId, filter); @@ -140,12 +164,16 @@ public static void filterLimitColFamilyRegex( // [END bigtable_filters_limit_col_family_regex] // [START bigtable_filters_limit_col_qualifier_regex] + public static void filterLimitColQualifierRegex() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitColQualifierRegex(projectId, instanceId, tableId); + } + public static void filterLimitColQualifierRegex( String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; - // A filter that matches cells whose column qualifier satisfies the given regex Filter filter = FILTERS.qualifier().regex("connected_.*$"); readFilter(projectId, instanceId, tableId, filter); @@ -153,11 +181,15 @@ public static void filterLimitColQualifierRegex( // [END bigtable_filters_limit_col_qualifier_regex] // [START bigtable_filters_limit_col_range] - public static void filterLimitColRange(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitColRange() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitColRange(projectId, instanceId, tableId); + } + public static void filterLimitColRange(String projectId, String instanceId, String tableId) { // A filter that matches cells whose column qualifiers are between data_plan_01gb and // data_plan_10gb in the column family cell_plan Filter filter = @@ -171,11 +203,15 @@ public static void filterLimitColRange(String projectId, String instanceId, Stri // [END bigtable_filters_limit_col_range] // [START bigtable_filters_limit_value_range] - public static void filterLimitValueRange(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitValueRange() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitValueRange(projectId, instanceId, tableId); + } + public static void filterLimitValueRange(String projectId, String instanceId, String tableId) { // A filter that matches cells whose values are between the given values Filter filter = FILTERS.value().range().startClosed("PQ2A.190405").endClosed("PQ2A.190406"); readFilter(projectId, instanceId, tableId, filter); @@ -183,11 +219,15 @@ public static void filterLimitValueRange(String projectId, String instanceId, St // [END bigtable_filters_limit_value_range] // [START bigtable_filters_limit_value_regex] - public static void filterLimitValueRegex(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitValueRegex() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitValueRegex(projectId, instanceId, tableId); + } + public static void filterLimitValueRegex(String projectId, String instanceId, String tableId) { // A filter that matches cells whose value satisfies the given regex Filter filter = FILTERS.value().regex("PQ2A.*$"); readFilter(projectId, instanceId, tableId, filter); @@ -195,12 +235,16 @@ public static void filterLimitValueRegex(String projectId, String instanceId, St // [END bigtable_filters_limit_value_regex] // [START bigtable_filters_limit_timestamp_range] + public static void filterLimitTimestampRange() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitTimestampRange(projectId, instanceId, tableId); + } + public static void filterLimitTimestampRange( String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; - // Get a time representing one hour ago long timestamp = Instant.now().minus(1, ChronoUnit.HOURS).toEpochMilli() * 1000; @@ -211,11 +255,15 @@ public static void filterLimitTimestampRange( // [END bigtable_filters_limit_timestamp_range] // [START bigtable_filters_limit_block_all] - public static void filterLimitBlockAll(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitBlockAll() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitBlockAll(projectId, instanceId, tableId); + } + public static void filterLimitBlockAll(String projectId, String instanceId, String tableId) { // A filter that does not match any cells Filter filter = FILTERS.block(); readFilter(projectId, instanceId, tableId, filter); @@ -223,11 +271,15 @@ public static void filterLimitBlockAll(String projectId, String instanceId, Stri // [END bigtable_filters_limit_block_all] // [START bigtable_filters_limit_pass_all] - public static void filterLimitPassAll(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterLimitPassAll() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterLimitPassAll(projectId, instanceId, tableId); + } + public static void filterLimitPassAll(String projectId, String instanceId, String tableId) { // A filter that matches all cells Filter filter = FILTERS.pass(); readFilter(projectId, instanceId, tableId, filter); @@ -235,11 +287,15 @@ public static void filterLimitPassAll(String projectId, String instanceId, Strin // [END bigtable_filters_limit_pass_all] // [START bigtable_filters_modify_strip_value] - public static void filterModifyStripValue(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterModifyStripValue() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterModifyStripValue(projectId, instanceId, tableId); + } + public static void filterModifyStripValue(String projectId, String instanceId, String tableId) { // A filter that replaces the outputted cell value with the empty string Filter filter = FILTERS.value().strip(); readFilter(projectId, instanceId, tableId, filter); @@ -247,11 +303,15 @@ public static void filterModifyStripValue(String projectId, String instanceId, S // [END bigtable_filters_modify_strip_value] // [START bigtable_filters_modify_apply_label] - public static void filterModifyApplyLabel(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterModifyApplyLabel() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterModifyApplyLabel(projectId, instanceId, tableId); + } + public static void filterModifyApplyLabel(String projectId, String instanceId, String tableId) { // A filter that applies the given label to the outputted cell Filter filter = FILTERS.label("labelled"); readFilter(projectId, instanceId, tableId, filter); @@ -259,11 +319,15 @@ public static void filterModifyApplyLabel(String projectId, String instanceId, S // [END bigtable_filters_modify_apply_label] // [START bigtable_filters_composing_chain] - public static void filterComposingChain(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterComposingChain() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterComposingChain(projectId, instanceId, tableId); + } + public static void filterComposingChain(String projectId, String instanceId, String tableId) { // A filter that selects one cell per column AND within the column family cell_plan Filter filter = FILTERS @@ -275,12 +339,16 @@ public static void filterComposingChain(String projectId, String instanceId, Str // [END bigtable_filters_composing_chain] // [START bigtable_filters_composing_interleave] + public static void filterComposingInterleave() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterComposingInterleave(projectId, instanceId, tableId); + } + public static void filterComposingInterleave( String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; - // A filter that matches cells with the value true OR with the column qualifier os_build Filter filter = FILTERS @@ -292,11 +360,15 @@ public static void filterComposingInterleave( // [END bigtable_filters_composing_interleave] // [START bigtable_filters_composing_condition] - public static void filterComposingCondition(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void filterComposingCondition() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + filterComposingCondition(projectId, instanceId, tableId); + } + public static void filterComposingCondition(String projectId, String instanceId, String tableId) { // A filter that applies the label passed-filter IF the cell has the column qualifier // data_plan_10gb AND the value true, OTHERWISE applies the label filtered-out Filter filter = diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java index 30bfb8ca796..1807fc2f819 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java @@ -46,11 +46,15 @@ public class Reads { // [END bigtable_reads_filter] // [START bigtable_reads_row] - public static void readRow(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void readRow() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + readRow(projectId, instanceId, tableId); + } + public static void readRow(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -68,11 +72,15 @@ public static void readRow(String projectId, String instanceId, String tableId) // [END bigtable_reads_row] // [START bigtable_reads_row_partial] - public static void readRowPartial(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void readRowPartial() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + readRowPartial(projectId, instanceId, tableId); + } + public static void readRowPartial(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -95,11 +103,15 @@ public static void readRowPartial(String projectId, String instanceId, String ta // [END bigtable_reads_row_partial] // [START bigtable_reads_rows] - public static void readRows(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void readRows() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + readRows(projectId, instanceId, tableId); + } + public static void readRows(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -118,10 +130,15 @@ public static void readRows(String projectId, String instanceId, String tableId) // [END bigtable_reads_rows] // [START bigtable_reads_row_range] + public static void readRowRange() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + readRowRange(projectId, instanceId, tableId); + } + public static void readRowRange(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; String start = "phone#4c410523#20190501"; String end = "phone#4c410523#201906201"; @@ -142,11 +159,15 @@ public static void readRowRange(String projectId, String instanceId, String tabl // [END bigtable_reads_row_range] // [START bigtable_reads_row_ranges] - public static void readRowRanges(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void readRowRanges() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + readRowRanges(projectId, instanceId, tableId); + } + public static void readRowRanges(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -167,11 +188,15 @@ public static void readRowRanges(String projectId, String instanceId, String tab // [END bigtable_reads_row_ranges] // [START bigtable_reads_prefix] - public static void readPrefix(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; + public static void readPrefix() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + readPrefix(projectId, instanceId, tableId); + } + public static void readPrefix(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -189,10 +214,15 @@ public static void readPrefix(String projectId, String instanceId, String tableI // [END bigtable_reads_prefix] // [START bigtable_reads_filter] + public static void readFilter() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String instanceId = "my-instance-id"; + String tableId = "mobile-time-series"; + readFilter(projectId, instanceId, tableId); + } + public static void readFilter(String projectId, String instanceId, String tableId) { - // String projectId = "my-project-id"; - // String instanceId = "my-instance-id"; - // String tableId = "mobile-time-series"; Filters.Filter filter = FILTERS.value().regex("PQ2A.*"); // Initialize client that will be used to send requests. This client only needs to be created From 7942c09b22c6bd10eddca8c4d1724176733c7aa9 Mon Sep 17 00:00:00 2001 From: kurtisvg <31518063+kurtisvg@users.noreply.github.com> Date: Mon, 2 Dec 2019 16:15:57 -0800 Subject: [PATCH 6/6] Cleanup requireEnv function. --- .../src/test/java/com/example/bigtable/FiltersTest.java | 7 +++---- .../src/test/java/com/example/bigtable/ReadsTest.java | 7 +++---- .../src/test/java/com/example/bigtable/WritesTest.java | 8 +++----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java index edce08eb20a..ab9c7af6692 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -56,10 +56,9 @@ public class FiltersTest { private ByteArrayOutputStream bout; private static String requireEnv(String varName) { - assertNotNull( - String.format("Environment variable '%s' is required to perform these tests.", varName), - System.getenv(varName)); - return System.getenv(varName); + String value = System.getenv(varName); + assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + return value; } @BeforeClass diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java index d97be32d282..e7afadee07d 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java @@ -51,10 +51,9 @@ public class ReadsTest { private ByteArrayOutputStream bout; private static String requireEnv(String varName) { - assertNotNull( - String.format("Environment variable '%s' is required to perform these tests.", varName), - System.getenv(varName)); - return System.getenv(varName); + String value = System.getenv(varName); + assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + return value; } @BeforeClass diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java index 2c2d2bd901e..364b527a962 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java @@ -43,12 +43,10 @@ public class WritesTest { private static String projectId; private static String instanceId; private ByteArrayOutputStream bout; - private static String requireEnv(String varName) { - assertNotNull( - String.format("Environment variable '%s' is required to perform these tests.", varName), - System.getenv(varName)); - return System.getenv(varName); + String value = System.getenv(varName); + assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + return value; } @BeforeClass