From abb08617e2719fc1d58df2eb0ea647aaf224ccdd Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Thu, 18 Jul 2019 17:12:22 -0400 Subject: [PATCH 1/8] samples: Bigtable Write samples (#1513) * Bigtable Writes samples for Java Client * Adding increment comment * Correcting bytes written in previous tests --- .../java/com/example/bigtable/WriteBatch.java | 68 ++++++++++ .../example/bigtable/WriteConditionally.java | 65 ++++++++++ .../com/example/bigtable/WriteIncrement.java | 51 ++++++++ .../com/example/bigtable/WriteSimple.java | 62 ++++++++++ .../java/com/example/bigtable/WritesTest.java | 116 ++++++++++++++++++ 5 files changed, 362 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java create mode 100644 samples/snippets/src/test/java/com/example/bigtable/WritesTest.java diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java b/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java new file mode 100644 index 0000000000..9b4e58a505 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java @@ -0,0 +1,68 @@ +/* + * 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_writes_batch] + +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; + +public class WriteBatch { + private static final String COLUMN_FAMILY_NAME = "stats_summary"; + + public static void writeBatch(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)) { + long timestamp = System.currentTimeMillis() * 1000; + ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1}); + + BulkMutation bulkMutation = + BulkMutation.create(tableId) + .add( + "tablet#a0b81f74#20190501", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + timestamp, + one) + .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")) + .add( + "tablet#a0b81f74#20190502", + Mutation.create() + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + timestamp, + one) + .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6")); + + dataClient.bulkMutateRows(bulkMutation); + + System.out.print("Successfully wrote 2 rows"); + } catch (Exception e) { + System.out.println("Error during WriteBatch: \n" + e.toString()); + } + } +} + +// [END bigtable_writes_batch] diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java b/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java new file mode 100644 index 0000000000..7cc8fd2244 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java @@ -0,0 +1,65 @@ +/* + * 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_writes_conditional] + +import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; + +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation; +import com.google.cloud.bigtable.data.v2.models.Filters.Filter; +import com.google.cloud.bigtable.data.v2.models.Mutation; + +public class WriteConditionally { + private static final String COLUMN_FAMILY_NAME = "stats_summary"; + + public static void writeConditionally(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)) { + long timestamp = System.currentTimeMillis() * 1000; + + String rowKey = "phone#4c410523#20190501"; + + Mutation mutation = + Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android"); + + Filter filter = + FILTERS + .chain() + .filter(FILTERS.family().exactMatch(COLUMN_FAMILY_NAME)) + .filter(FILTERS.qualifier().exactMatch("os_build")) + .filter(FILTERS.value().regex("PQ2A\\..*")); + + ConditionalRowMutation conditionalRowMutation = + ConditionalRowMutation.create(tableId, rowKey).condition(filter).then(mutation); + + boolean success = dataClient.checkAndMutateRow(conditionalRowMutation); + + System.out.printf("Successfully updated row's os_name: %b", success); + + } catch (Exception e) { + System.out.println("Error during WriteConditionally: \n" + e.toString()); + e.printStackTrace(); + } + } +} + +// [END bigtable_writes_conditional] diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java b/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java new file mode 100644 index 0000000000..ec6d756b62 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java @@ -0,0 +1,51 @@ +/* + * 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_writes_increment] + +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow; +import com.google.cloud.bigtable.data.v2.models.Row; +import java.nio.charset.Charset; + +public class WriteIncrement { + private static final String COLUMN_FAMILY_NAME = "stats_summary"; + + public static void writeIncrement(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)) { + // 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"; + ReadModifyWriteRow mutation = + ReadModifyWriteRow.create(tableId, rowKey) + .increment(COLUMN_FAMILY_NAME, "connected_cell", -1); + Row success = dataClient.readModifyWriteRow(mutation); + + System.out.printf( + "Successfully updated row %s", success.getKey().toString(Charset.defaultCharset())); + } catch (Exception e) { + System.out.println("Error during WriteIncrement: \n" + e.toString()); + } + } +} + +// [END bigtable_writes_increment] diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java b/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java new file mode 100644 index 0000000000..5067198921 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java @@ -0,0 +1,62 @@ +/* + * 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_writes_simple] + +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import com.google.protobuf.ByteString; + +public class WriteSimple { + private static final String COLUMN_FAMILY_NAME = "stats_summary"; + + public static void writeSimple(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)) { + long timestamp = System.currentTimeMillis() * 1000; + + 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) + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_cell".getBytes()), + timestamp, + one) + .setCell( + COLUMN_FAMILY_NAME, + ByteString.copyFrom("connected_wifi".getBytes()), + timestamp, + one) + .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003"); + + dataClient.mutateRow(rowMutation); + System.out.printf("Successfully wrote row %s", rowKey); + + } catch (Exception e) { + System.out.println("Error during WriteSimple: \n" + e.toString()); + } + } +} + +// [END bigtable_writes_simple] diff --git a/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java b/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java new file mode 100644 index 0000000000..afe70c4027 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java @@ -0,0 +1,116 @@ +/* + * 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 org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.hamcrest.CoreMatchers; +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 WritesTest { + + 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 String projectId; + private static String instanceId; + private ByteArrayOutputStream bout; + + private static String requireEnv(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(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); + } 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 test1_WriteSimple() { + WriteSimple.writeSimple(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output, CoreMatchers.containsString("Successfully wrote row")); + } + + @Test + public void test2_WriteBatch() { + WriteBatch.writeBatch(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output, CoreMatchers.containsString("Successfully wrote 2 rows")); + } + + @Test + public void test3_WriteConditionally() { + WriteConditionally.writeConditionally(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output, CoreMatchers.containsString("Successfully updated row's os_name: true")); + } + + @Test + public void test4_WriteIncrement() { + WriteIncrement.writeIncrement(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat( + output, CoreMatchers.containsString("Successfully updated row phone#4c410523#20190501")); + } +} From cb4738b53a1e4b48dfaacc49bad4d19f356cf6ca Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Tue, 3 Dec 2019 14:46:46 -0500 Subject: [PATCH 2/8] samples: Read and Filter Snippets (#1747) * WIP for Bigtable Read and Filter snippets * Remaining Filter snippets WIP * All tests working, improved interleave label and condition, removed sink * Changing time to java.time Adding comments about filtering Handling exceptions Reformatting Reads print output other cleanups * Updating function declarations * Cleanup requireEnv function. --- .../java/com/example/bigtable/Filters.java | 459 +++++++++++ .../main/java/com/example/bigtable/Reads.java | 272 +++++++ .../example/bigtable/WriteConditionally.java | 4 +- .../com/example/bigtable/WriteIncrement.java | 4 +- .../com/example/bigtable/WriteSimple.java | 6 +- .../com/example/bigtable/FiltersTest.java | 745 ++++++++++++++++++ .../java/com/example/bigtable/ReadsTest.java | 343 ++++++++ .../java/com/example/bigtable/WritesTest.java | 8 +- 8 files changed, 1829 insertions(+), 12 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/bigtable/Filters.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/Reads.java create mode 100644 samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java create mode 100644 samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java diff --git a/samples/snippets/src/main/java/com/example/bigtable/Filters.java b/samples/snippets/src/main/java/com/example/bigtable/Filters.java new file mode 100644 index 0000000000..93367eeace --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/Filters.java @@ -0,0 +1,459 @@ +/* + * 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] +// [START bigtable_filters_limit_block_all] +// [START bigtable_filters_limit_pass_all] +// [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; +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] + // [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] + // [END bigtable_filters_limit_block_all] + // [END bigtable_filters_limit_pass_all] + // [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() { + // 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); + } + // [END bigtable_filters_limit_row_sample] + + // [START bigtable_filters_limit_row_regex] + 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); + } + // [END bigtable_filters_limit_row_regex] + + // [START bigtable_filters_limit_cells_per_col] + 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); + } + // [END bigtable_filters_limit_cells_per_col] + + // [START bigtable_filters_limit_cells_per_row] + 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); + } + // [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) { + // A filter that skips the first 2 cells per row + 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() { + // 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) { + // A filter that matches cells whose column family satisfies the given regex + 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() { + // 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) { + // A filter that matches cells whose column qualifier satisfies the given regex + 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() { + // 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 = + 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() { + // 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); + } + // [END bigtable_filters_limit_value_range] + + // [START bigtable_filters_limit_value_regex] + 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); + } + // [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) { + // 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] + + // [START bigtable_filters_limit_block_all] + 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); + } + // [END bigtable_filters_limit_block_all] + + // [START bigtable_filters_limit_pass_all] + 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); + } + // [END bigtable_filters_limit_pass_all] + + // [START bigtable_filters_modify_strip_value] + 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); + } + // [END bigtable_filters_modify_strip_value] + + // [START bigtable_filters_modify_apply_label] + 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); + } + // [END bigtable_filters_modify_apply_label] + + // [START bigtable_filters_composing_chain] + 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 + .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() { + // 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) { + // A filter that matches cells with the value true OR with the column qualifier os_build + Filter filter = + FILTERS + .interleave() + .filter(FILTERS.value().exactMatch("true")) + .filter(FILTERS.qualifier().exactMatch("os_build")); + readFilter(projectId, instanceId, tableId, filter); + } + // [END bigtable_filters_composing_interleave] + + // [START bigtable_filters_composing_condition] + 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 = + FILTERS + .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] + + // [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] + // [START bigtable_filters_limit_block_all] + // [START bigtable_filters_limit_pass_all] + // [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] + 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 (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); + } + } + + 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); + } + String labels = + cell.getLabels().size() == 0 ? "" : " [" + String.join(",", cell.getLabels()) + "]"; + System.out.printf( + "\t%s: %s @%s%s%n", + cell.getQualifier().toStringUtf8(), + cell.getValue().toStringUtf8(), + cell.getTimestamp(), + labels); + } + System.out.println(); + } +} +// [END bigtable_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] +// [END bigtable_filters_limit_block_all] +// [END bigtable_filters_limit_pass_all] +// [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/samples/snippets/src/main/java/com/example/bigtable/Reads.java b/samples/snippets/src/main/java/com/example/bigtable/Reads.java new file mode 100644 index 0000000000..1807fc2f81 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/Reads.java @@ -0,0 +1,272 @@ +/* + * 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; +import java.io.IOException; + +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() { + // 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. + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + String rowkey = "phone#4c410523#20190501"; + + Row row = dataClient.readRow(tableId, rowkey); + printRow(row); + + } catch (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); + } + } + // [END bigtable_reads_row] + + // [START bigtable_reads_row_partial] + 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. + 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 (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); + } + } + // [END bigtable_reads_row_partial] + + // [START bigtable_reads_rows] + 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. + 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 (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); + } + } + // [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 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 (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); + } + } + // [END bigtable_reads_row_range] + + // [START bigtable_reads_row_ranges] + 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. + 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 (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); + } + } + // [END bigtable_reads_row_ranges] + + // [START bigtable_reads_prefix] + 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. + 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 (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \n" + e.toString()); + } + } + // [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) { + 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 (IOException e) { + System.out.println( + "Unable to intailize service client, as a network error occured: \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()); + 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( + "\t%s: %s @%s%n", + 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/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java b/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java index 7cc8fd2244..ac01cb0c63 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java +++ b/samples/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/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java b/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java index ec6d756b62..0f91a13717 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java +++ b/samples/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/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java b/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java index 5067198921..a782542c3e 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java +++ b/samples/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/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java new file mode 100644 index 0000000000..ab9c7af669 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -0,0 +1,745 @@ +/* + * 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.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; +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 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; + private ByteArrayOutputStream bout; + + private static String requireEnv(String varName) { + String value = System.getenv(varName); + assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + return value; + } + + @BeforeClass + public static void beforeClass() throws IOException { + 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_MINUS_HR, "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()); + throw (e); + } + } + + @Before + public void setupStream() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @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); + } + } + + @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" + + "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" + + "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_MINUS_HR)); + } + + @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" + + "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_MINUS_HR)); + } + + @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" + + "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" + + "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" + + "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" + + "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" + + "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_MINUS_HR)); + } + + @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" + + "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" + + "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" + + "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" + + "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" + + "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)); + } + + @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" + + "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 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 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 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 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)); + } + + @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" + + "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" + + "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" + + "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" + + "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" + + "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)); + } + + @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" + + "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" + + "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", + TIMESTAMP, TIMESTAMP_MINUS_HR)); + } + + @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" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family stats_summary\n" + + "\tos_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" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family stats_summary\n" + + "\tos_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" + + "Column Family cell_plan\n" + + "\tdata_plan_01gb: true @%s\n", + TIMESTAMP_MINUS_HR)); + } + + @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_MINUS_HR)); + } + + @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_MINUS_HR)); + } + + @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 [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 [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 [labelled]\n" + + "Column Family stats_summary\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 [labelled]\n" + + "Column Family stats_summary\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 [labelled]\n" + + "Column Family stats_summary\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 [labelled]\n" + + "Column Family stats_summary\n" + + "\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_MINUS_HR)); + } + + @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: 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" + + "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" + + "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" + + "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" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s", + TIMESTAMP, TIMESTAMP_MINUS_HR)); + } + + @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 [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 [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 [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 [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 [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_MINUS_HR)); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java b/samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java new file mode 100644 index 0000000000..e7afadee07 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java @@ -0,0 +1,343 @@ +/* + * 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.IOException; +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) { + String value = System.getenv(varName); + assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + return value; + } + + @BeforeClass + public static void beforeClass() throws IOException { + 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()); + throw (e); + } + } + + @Before + public void setupStream() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @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); + } + } + + @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" + + "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)); + } + + @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" + + "Column Family stats_summary\n" + + "\tos_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" + + "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 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)); + } + + @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" + + "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 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 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)); + } + + @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" + + "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 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 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 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 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)); + } + + @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" + + "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 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 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 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 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)); + } + + @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" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.003 @%1$s\n\n" + + "Reading data for phone#4c410523#20190502\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190405.004 @%1$s\n\n" + + "Reading data for phone#4c410523#20190505\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s\n\n" + + "Reading data for phone#5c10102#20190501\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190401.002 @%1$s\n\n" + + "Reading data for phone#5c10102#20190502\n" + + "Column Family stats_summary\n" + + "\tos_build: PQ2A.190406.000 @%1$s", + TIMESTAMP)); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java b/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java index afe70c4027..364b527a96 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java +++ b/samples/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( - System.getenv(varName), - "Environment variable '%s' is required to perform these tests.".format(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 From a29f9f70d288575d002c044c9e5bb1267fbea3e7 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Fri, 6 Dec 2019 16:51:50 -0500 Subject: [PATCH 3/8] samples: Bigtable: Using cleaner number input for snippets (#1787) --- .../src/main/java/com/example/bigtable/WriteBatch.java | 5 ++--- .../src/main/java/com/example/bigtable/WriteSimple.java | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java b/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java index 9b4e58a505..180793a10b 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java @@ -33,7 +33,6 @@ public static void writeBatch(String projectId, String instanceId, String tableI try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { long timestamp = System.currentTimeMillis() * 1000; - ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1}); BulkMutation bulkMutation = BulkMutation.create(tableId) @@ -44,7 +43,7 @@ public static void writeBatch(String projectId, String instanceId, String tableI COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, - one) + 1) .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")) .add( "tablet#a0b81f74#20190502", @@ -53,7 +52,7 @@ public static void writeBatch(String projectId, String instanceId, String tableI COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, - one) + 1) .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6")); dataClient.bulkMutateRows(bulkMutation); diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java b/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java index a782542c3e..5d7f4d5b2a 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java @@ -34,7 +34,6 @@ public static void writeSimple(String projectId, String instanceId, String table long timestamp = System.currentTimeMillis() * 1000; 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) @@ -42,12 +41,12 @@ public static void writeSimple(String projectId, String instanceId, String table COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), timestamp, - one) + 1) .setCell( COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, - one) + 1) .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003"); dataClient.mutateRow(rowMutation); From e7994fab18e9465624cc20904e9e7df4c3efec31 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Tue, 14 Jan 2020 14:50:57 -0500 Subject: [PATCH 4/8] samples: bigtable hbase: read and filter snippets (#1947) * Read and filter snippets for Hbase Bigtable Reads done Filters WIP, issues with Pass and ColRange. Some nuances to sort out between HBase and other clients still * Filter snippets * Remove unused region tags Co-authored-by: Les Vogel --- .../main/java/com/example/bigtable/Filters.java | 6 +++--- .../src/main/java/com/example/bigtable/Reads.java | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/Filters.java b/samples/snippets/src/main/java/com/example/bigtable/Filters.java index 93367eeace..bb8d8cbd3f 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/samples/snippets/src/main/java/com/example/bigtable/Filters.java @@ -75,8 +75,8 @@ public static void filterLimitRowSample() { } 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); + // A filter that matches cells from a row with probability .75 + Filter filter = FILTERS.key().sample(.75); readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_limit_row_sample] @@ -415,7 +415,7 @@ private static void readFilter( } } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } diff --git a/samples/snippets/src/main/java/com/example/bigtable/Reads.java b/samples/snippets/src/main/java/com/example/bigtable/Reads.java index 1807fc2f81..778108725a 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/Reads.java +++ b/samples/snippets/src/main/java/com/example/bigtable/Reads.java @@ -66,7 +66,7 @@ public static void readRow(String projectId, String instanceId, String tableId) } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } // [END bigtable_reads_row] @@ -97,7 +97,7 @@ public static void readRowPartial(String projectId, String instanceId, String ta } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } // [END bigtable_reads_row_partial] @@ -124,7 +124,7 @@ public static void readRows(String projectId, String instanceId, String tableId) } } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } // [END bigtable_reads_rows] @@ -153,7 +153,7 @@ public static void readRowRange(String projectId, String instanceId, String tabl } } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } // [END bigtable_reads_row_range] @@ -182,7 +182,7 @@ public static void readRowRanges(String projectId, String instanceId, String tab } } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } // [END bigtable_reads_row_ranges] @@ -208,7 +208,7 @@ public static void readPrefix(String projectId, String instanceId, String tableI } } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } // [END bigtable_reads_prefix] @@ -236,7 +236,7 @@ public static void readFilter(String projectId, String instanceId, String tableI } } catch (IOException e) { System.out.println( - "Unable to intailize service client, as a network error occured: \n" + e.toString()); + "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } // [END bigtable_reads_filter] From 76ffe70934ea9945453c5eb63d90f40be92a4495 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Thu, 18 Jun 2020 13:37:50 -0400 Subject: [PATCH 5/8] samples: [bigtable] Update region tags for reads and filters (#3148) * Update read region tags * Update region tags for filters * Move region tags in filters * Move region tags out of class * Update tags for java * change generic to print * update java bigtable filters region tags * make into one sample * untrack hbase-test * change function to code * lint everything --- .../java/com/example/bigtable/Filters.java | 80 ++----------------- .../main/java/com/example/bigtable/Reads.java | 34 ++------ .../com/example/bigtable/FiltersTest.java | 64 ++++++++++----- .../java/com/example/bigtable/ReadsTest.java | 4 +- .../java/com/example/bigtable/WritesTest.java | 5 +- 5 files changed, 63 insertions(+), 124 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/Filters.java b/samples/snippets/src/main/java/com/example/bigtable/Filters.java index bb8d8cbd3f..cb6e6175d8 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/samples/snippets/src/main/java/com/example/bigtable/Filters.java @@ -16,25 +16,10 @@ package com.example.bigtable; +// [START bigtable_filters_print] + 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] -// [START bigtable_filters_limit_block_all] -// [START bigtable_filters_limit_pass_all] -// [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; @@ -45,26 +30,11 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; + 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] - // [END bigtable_filters_limit_block_all] - // [END bigtable_filters_limit_pass_all] - // [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] + // Write your code here. + // [START_EXCLUDE] // [START bigtable_filters_limit_row_sample] public static void filterLimitRowSample() { // TODO(developer): Replace these variables before running the sample. @@ -383,25 +353,8 @@ public static void filterComposingCondition(String projectId, String instanceId, readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_composing_condition] + // [END_EXCLUDE] - // [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] - // [START bigtable_filters_limit_block_all] - // [START bigtable_filters_limit_pass_all] - // [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] 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 @@ -439,21 +392,4 @@ private static void printRow(Row row) { 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] -// [END bigtable_filters_limit_block_all] -// [END bigtable_filters_limit_pass_all] -// [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] +// [END bigtable_filters_print] diff --git a/samples/snippets/src/main/java/com/example/bigtable/Reads.java b/samples/snippets/src/main/java/com/example/bigtable/Reads.java index 778108725a..fc0762ac1a 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/Reads.java +++ b/samples/snippets/src/main/java/com/example/bigtable/Reads.java @@ -16,13 +16,7 @@ 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] +// [START bigtable_reads_print] import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; @@ -37,15 +31,9 @@ import java.io.IOException; 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] + // Write your code here. + // [START_EXCLUDE] public static void readRow() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project-id"; @@ -240,14 +228,8 @@ public static void readFilter(String projectId, String instanceId, String tableI } } // [END bigtable_reads_filter] + // [END_EXCLUDE] - // [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()); String colFamily = ""; @@ -263,10 +245,4 @@ private static void printRow(Row row) { 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] +// [END bigtable_reads_print] diff --git a/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java index ab9c7af669..429b5712c4 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -57,7 +57,9 @@ public class FiltersTest { private static String requireEnv(String varName) { String value = System.getenv(varName); - assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + value); return value; } @@ -597,36 +599,46 @@ public void testFilterApplyLabel() { + "\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 [labelled]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\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 [labelled]\n" + "Column Family stats_summary\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" + + "\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 [labelled]\n" + "Column Family stats_summary\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" + + "\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 [labelled]\n" + "Column Family stats_summary\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" + + "\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 [labelled]\n" + "Column Family stats_summary\n" - + "\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" + + "\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_MINUS_HR)); } @@ -709,36 +721,46 @@ public void testFilterCondition() { + "\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" + + "\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 [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" + + "\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 [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" + + "\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 [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" + + "\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 [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" + + "\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_MINUS_HR)); } diff --git a/samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java b/samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java index e7afadee07..7b526c87ea 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/ReadsTest.java @@ -52,7 +52,9 @@ public class ReadsTest { private static String requireEnv(String varName) { String value = System.getenv(varName); - assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + value); return value; } diff --git a/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java b/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java index 364b527a96..b28a1ef74d 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java @@ -43,9 +43,12 @@ public class WritesTest { private static String projectId; private static String instanceId; private ByteArrayOutputStream bout; + private static String requireEnv(String varName) { String value = System.getenv(varName); - assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + value); return value; } From 8fdf63ec09a5bbc761ce21e7a4b80f89cb9b32fb Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Wed, 24 Jun 2020 14:00:03 -0400 Subject: [PATCH 6/8] samples: fix region tag (#3238) Fixes #issue > It's a good idea to open an issue first for discussion. - [ ] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [ ] `pom.xml` parent set to latest `shared-configuration` - [ ] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [x] **Tests** pass: `mvn clean verify` **required** - [x] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [x] Please **merge** this PR for me once it is approved. --- samples/snippets/src/main/java/com/example/bigtable/Reads.java | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/snippets/src/main/java/com/example/bigtable/Reads.java b/samples/snippets/src/main/java/com/example/bigtable/Reads.java index fc0762ac1a..989da4f62f 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/Reads.java +++ b/samples/snippets/src/main/java/com/example/bigtable/Reads.java @@ -34,6 +34,7 @@ public class Reads { // Write your code here. // [START_EXCLUDE] + // [START bigtable_reads_row] public static void readRow() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project-id"; From 865649778097d838f27961a6e8df72dbb5f97419 Mon Sep 17 00:00:00 2001 From: Kristen O'Leary Date: Fri, 7 Aug 2020 13:26:15 -0400 Subject: [PATCH 7/8] update kokoro configs --- .kokoro/nightly/samples.cfg | 5 +++++ .kokoro/presubmit/samples.cfg | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/.kokoro/nightly/samples.cfg b/.kokoro/nightly/samples.cfg index f25429314f..b72bc63092 100644 --- a/.kokoro/nightly/samples.cfg +++ b/.kokoro/nightly/samples.cfg @@ -36,3 +36,8 @@ env_vars: { key: "ENABLE_BUILD_COP" value: "true" } + +env_vars: { + key: "BIGTABLE_TESTING_INSTANCE" + value: "instance" +} \ No newline at end of file diff --git a/.kokoro/presubmit/samples.cfg b/.kokoro/presubmit/samples.cfg index 01e0960047..1e677bfdff 100644 --- a/.kokoro/presubmit/samples.cfg +++ b/.kokoro/presubmit/samples.cfg @@ -30,4 +30,9 @@ env_vars: { env_vars: { key: "SECRET_MANAGER_KEYS" value: "java-docs-samples-service-account" +} + +env_vars: { + key: "BIGTABLE_TESTING_INSTANCE" + value: "instance" } \ No newline at end of file From 012fbc392618b53d14d28b8eb65a232de1765f3a Mon Sep 17 00:00:00 2001 From: Kristen O'Leary Date: Fri, 7 Aug 2020 16:06:52 -0400 Subject: [PATCH 8/8] update kokoro configs --- synth.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/synth.py b/synth.py index a8d7565dea..799187531c 100644 --- a/synth.py +++ b/synth.py @@ -37,6 +37,8 @@ def main(): 'README.md', '.kokoro/presubmit/integration.cfg', '.kokoro/nightly/integration.cfg', + '.kokoro/presubmit/samples.cfg', + '.kokoro/nightly/samples.cfg', # todo remove once template is updated '.github/ISSUE_TEMPLATE/bug_report.md', 'CONTRIBUTING.md',