From d9afa06621c2c8c8074c8b02c09d4ee87a7b75cd Mon Sep 17 00:00:00 2001 From: kolea2 <45548808+kolea2@users.noreply.github.com> Date: Tue, 11 Apr 2023 11:04:12 -0400 Subject: [PATCH] samples: add delete samples (#1472) --- README.md | 7 + .../bigtable/deletes/BatchDeleteExample.java | 44 +++++ .../deletes/ConditionalDeleteExample.java | 39 ++++ .../deletes/DeleteColumnCellsExample.java | 34 ++++ .../deletes/DeleteColumnFamilyExample.java | 33 ++++ .../bigtable/deletes/DeleteRowExample.java | 33 ++++ .../bigtable/deletes/DeleteTableExample.java | 31 ++++ .../bigtable/deletes/DropRowRangeExample.java | 31 ++++ .../bigtable/MobileTimeSeriesBaseTest.java | 4 +- .../example/bigtable/deletes/DeletesTest.java | 173 ++++++++++++++++++ 10 files changed, 428 insertions(+), 1 deletion(-) create mode 100644 samples/snippets/src/main/java/com/example/bigtable/deletes/BatchDeleteExample.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/deletes/ConditionalDeleteExample.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnCellsExample.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnFamilyExample.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteRowExample.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteTableExample.java create mode 100644 samples/snippets/src/main/java/com/example/bigtable/deletes/DropRowRangeExample.java create mode 100644 samples/snippets/src/test/java/com/example/bigtable/deletes/DeletesTest.java diff --git a/README.md b/README.md index e3d8931d71..cb5dee5bd1 100644 --- a/README.md +++ b/README.md @@ -502,6 +502,13 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigtable/tree | Write Conditionally | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java) | | Write Increment | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java) | | Write Simple | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/WriteSimple.java) | +| Batch Delete Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/deletes/BatchDeleteExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/deletes/BatchDeleteExample.java) | +| Conditional Delete Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/deletes/ConditionalDeleteExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/deletes/ConditionalDeleteExample.java) | +| Delete Column Cells Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnCellsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnCellsExample.java) | +| Delete Column Family Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnFamilyExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnFamilyExample.java) | +| Delete Row Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteRowExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteRowExample.java) | +| Delete Table Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteTableExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteTableExample.java) | +| Drop Row Range Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/deletes/DropRowRangeExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/deletes/DropRowRangeExample.java) | diff --git a/samples/snippets/src/main/java/com/example/bigtable/deletes/BatchDeleteExample.java b/samples/snippets/src/main/java/com/example/bigtable/deletes/BatchDeleteExample.java new file mode 100644 index 0000000000..2b814c4e42 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/deletes/BatchDeleteExample.java @@ -0,0 +1,44 @@ +/* + * Copyright 2022 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.deletes; + +// [START bigtable_streaming_and_batching] +import com.google.api.gax.batching.Batcher; +import com.google.api.gax.rpc.ServerStream; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +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.RowMutationEntry; +import java.io.IOException; + +public class BatchDeleteExample { + public void batchDelete(String projectId, String instanceId, String tableId) + throws InterruptedException, IOException { + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + try (Batcher batcher = dataClient.newBulkMutationBatcher(tableId)) { + ServerStream rows = dataClient.readRows(Query.create(tableId)); + for (Row row : rows) { + batcher.add( + RowMutationEntry.create(row.getKey()).deleteCells("cell_plan", "data_plan_05gb")); + } + // Blocks until mutations are applied on all submitted row entries. + batcher.flush(); + } + } + } +} +// [END bigtable_streaming_and_batching] diff --git a/samples/snippets/src/main/java/com/example/bigtable/deletes/ConditionalDeleteExample.java b/samples/snippets/src/main/java/com/example/bigtable/deletes/ConditionalDeleteExample.java new file mode 100644 index 0000000000..4fac36404c --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/deletes/ConditionalDeleteExample.java @@ -0,0 +1,39 @@ +/* + * Copyright 2022 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.deletes; + +// [START bigtable_delete_check_and_mutate] +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; +import com.google.cloud.bigtable.data.v2.models.Mutation; +import java.io.IOException; + +public class ConditionalDeleteExample { + public void conditionalDelete(String projectId, String instanceId, String tableId) + throws IOException { + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Filters.Filter condition = Filters.FILTERS.value().exactMatch("PQ2A.190405.004"); + Mutation mutation = Mutation.create().deleteCells("stats_summary", "os_build"); + dataClient.checkAndMutateRow( + ConditionalRowMutation.create(tableId, "phone#4c410523#20190502") + .condition(condition) + .then(mutation)); + } + } +} +// [END bigtable_delete_check_and_mutate] diff --git a/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnCellsExample.java b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnCellsExample.java new file mode 100644 index 0000000000..eefc4e1d84 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnCellsExample.java @@ -0,0 +1,34 @@ +/* + * Copyright 2022 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.deletes; + +// [START bigtable_delete_from_column] +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.Mutation; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.io.IOException; + +public class DeleteColumnCellsExample { + public void deleteColumnCells(String projectId, String instanceId, String tableId) + throws IOException { + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Mutation mutation = Mutation.create().deleteCells("cell_plan", "data_plan_01gb"); + dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation)); + } + } +} +// [END bigtable_delete_from_column] diff --git a/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnFamilyExample.java b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnFamilyExample.java new file mode 100644 index 0000000000..556c842cf6 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteColumnFamilyExample.java @@ -0,0 +1,33 @@ +/* + * Copyright 2022 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.deletes; + +// [START bigtable_delete_column_family] +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.io.IOException; + +public class DeleteColumnFamilyExample { + public void deleteColumnFamily(String projectId, String instanceId, String tableId) + throws IOException { + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + dataClient.mutateRow( + RowMutation.create(tableId, "phone#5c10102#20190501").deleteFamily("stats_summary")); + } + } +} +// [END bigtable_delete_column_family] diff --git a/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteRowExample.java b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteRowExample.java new file mode 100644 index 0000000000..9607a17079 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteRowExample.java @@ -0,0 +1,33 @@ +/* + * Copyright 2022 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.deletes; + +// [START bigtable_delete_from_row] +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.Mutation; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.io.IOException; + +public class DeleteRowExample { + public void deleteRow(String projectId, String instanceId, String tableId) throws IOException { + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + Mutation mutation = Mutation.create().deleteRow(); + dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation)); + } + } +} +// [END bigtable_delete_from_row] diff --git a/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteTableExample.java b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteTableExample.java new file mode 100644 index 0000000000..bdded5fbef --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteTableExample.java @@ -0,0 +1,31 @@ +/* + * Copyright 2022 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.deletes; + +// [START bigtable_delete_table] +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import java.io.IOException; + +public class DeleteTableExample { + public void deleteTable(String projectId, String instanceId, String tableId) throws IOException { + try (BigtableTableAdminClient tableAdminClient = + BigtableTableAdminClient.create(projectId, instanceId)) { + tableAdminClient.deleteTable(tableId); + } + } +} +// [END bigtable_delete_table] diff --git a/samples/snippets/src/main/java/com/example/bigtable/deletes/DropRowRangeExample.java b/samples/snippets/src/main/java/com/example/bigtable/deletes/DropRowRangeExample.java new file mode 100644 index 0000000000..0575fb38ec --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/deletes/DropRowRangeExample.java @@ -0,0 +1,31 @@ +/* + * Copyright 2022 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.deletes; + +// [START bigtable_drop_row_range] +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import java.io.IOException; + +public class DropRowRangeExample { + public void dropRowRange(String projectId, String instanceId, String tableId) throws IOException { + try (BigtableTableAdminClient tableAdminClient = + BigtableTableAdminClient.create(projectId, instanceId)) { + tableAdminClient.dropRowRange(tableId, "phone#4c410523"); + } + } +} +// [END bigtable_drop_row_range] diff --git a/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java b/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java index c31ec11ce2..00aec60498 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java @@ -170,7 +170,9 @@ public static void writePlanData() throws IOException { public static void cleanupTable() throws IOException { try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) { - adminClient.deleteTable(TABLE_ID); + if (adminClient.exists(TABLE_ID)) { + adminClient.deleteTable(TABLE_ID); + } } catch (Exception e) { System.out.println("Error during afterClass: \n" + e.toString()); throw (e); diff --git a/samples/snippets/src/test/java/com/example/bigtable/deletes/DeletesTest.java b/samples/snippets/src/test/java/com/example/bigtable/deletes/DeletesTest.java new file mode 100644 index 0000000000..4ac12c1e47 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigtable/deletes/DeletesTest.java @@ -0,0 +1,173 @@ +/* + * Copyright 2022 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.deletes; + +import com.example.bigtable.MobileTimeSeriesBaseTest; +import com.google.api.gax.rpc.ServerStream; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +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.common.truth.Truth; +import java.io.IOException; +import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +/* + * These tests are order dependent because they delete rows and cells from a table. + * They are prefixed with `testN_` to signal the order in which they should run. + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class DeletesTest extends MobileTimeSeriesBaseTest { + public static BigtableDataClient bigtableDataClient; + + @BeforeClass + public static void beforeClass() throws IOException { + initializeVariables(); + createTable(); + writeStatsData(); + writePlanData(); + bigtableDataClient = BigtableDataClient.create(projectId, instanceId); + } + + @AfterClass + public static void afterClass() throws IOException { + cleanupTable(); + } + + @Test + public void test1_testDeleteFromColumn() throws IOException { + String rowKey = "phone#4c410523#20190501"; + Row row = bigtableDataClient.readRow(TABLE_ID, rowKey); + String qualifier = "data_plan_01gb"; + List cells = row.getCells(COLUMN_FAMILY_NAME_PLAN, qualifier); + + Truth.assertThat(cells).isNotEmpty(); + + DeleteColumnCellsExample deleteColumnCellsExample = new DeleteColumnCellsExample(); + deleteColumnCellsExample.deleteColumnCells(projectId, instanceId, TABLE_ID); + row = bigtableDataClient.readRow(TABLE_ID, rowKey); + List cellsAfterDelete = row.getCells(COLUMN_FAMILY_NAME_PLAN, qualifier); + + Truth.assertThat(cellsAfterDelete).isEmpty(); + } + + @Test + public void test2_testDeleteFromRow() throws IOException { + String rowKey = "phone#4c410523#20190501"; + Row row = bigtableDataClient.readRow(TABLE_ID, rowKey); + + Truth.assertThat(row).isNotNull(); + + DeleteRowExample deleteRowExample = new DeleteRowExample(); + deleteRowExample.deleteRow(projectId, instanceId, TABLE_ID); + row = bigtableDataClient.readRow(TABLE_ID, rowKey); + + Truth.assertThat(row).isNull(); + } + + @Test + public void test3_testStreamingAndBatching() throws IOException, InterruptedException { + String rowKey = "phone#4c410523#20190502"; + Row row = bigtableDataClient.readRow(TABLE_ID, rowKey); + String qualifier = "data_plan_05gb"; + List cells = row.getCells(COLUMN_FAMILY_NAME_PLAN, qualifier); + + Truth.assertThat(cells).isNotEmpty(); + + BatchDeleteExample batchDeleteExample = new BatchDeleteExample(); + batchDeleteExample.batchDelete(projectId, instanceId, TABLE_ID); + row = bigtableDataClient.readRow(TABLE_ID, rowKey); + List cellsAfterDelete = row.getCells(COLUMN_FAMILY_NAME_PLAN, qualifier); + + Truth.assertThat(cellsAfterDelete).isEmpty(); + } + + @Test + public void test4_testCheckAndMutate() throws IOException { + String rowKey = "phone#4c410523#20190502"; + Row row = bigtableDataClient.readRow(TABLE_ID, rowKey); + String qualifier = "os_build"; + List cells = row.getCells(COLUMN_FAMILY_NAME_STATS, qualifier); + + Truth.assertThat(cells).isNotEmpty(); + + ConditionalDeleteExample conditionalDeleteExample = new ConditionalDeleteExample(); + conditionalDeleteExample.conditionalDelete(projectId, instanceId, TABLE_ID); + row = bigtableDataClient.readRow(TABLE_ID, rowKey); + List cellsAfterDelete = row.getCells(COLUMN_FAMILY_NAME_STATS, qualifier); + + Truth.assertThat(cellsAfterDelete).isEmpty(); + } + + @Test + public void test5_testDropRowRange() throws IOException { + String rowPrefix = "phone#4c410523"; + Query query = Query.create(TABLE_ID).prefix(rowPrefix); + ServerStream rows = bigtableDataClient.readRows(query); + int rowCount = 0; + for (Row ignored : rows) { + rowCount++; + } + + Truth.assertThat(rowCount).isGreaterThan(1); + + DropRowRangeExample dropRowRangeExample = new DropRowRangeExample(); + dropRowRangeExample.dropRowRange(projectId, instanceId, TABLE_ID); + rows = bigtableDataClient.readRows(query); + rowCount = 0; + for (Row ignored : rows) { + rowCount++; + } + + Truth.assertThat(rowCount).isEqualTo(0); + } + + @Test + public void test6_testDeleteColumnFamily() throws IOException { + String rowKey = "phone#5c10102#20190501"; + Row row = bigtableDataClient.readRow(TABLE_ID, rowKey); + List cells = row.getCells(COLUMN_FAMILY_NAME_STATS); + + Truth.assertThat(cells).isNotEmpty(); + + DeleteColumnFamilyExample deleteColumnFamilyExample = new DeleteColumnFamilyExample(); + deleteColumnFamilyExample.deleteColumnFamily(projectId, instanceId, TABLE_ID); + row = bigtableDataClient.readRow(TABLE_ID, rowKey); + List cellsAfterDelete = row.getCells(COLUMN_FAMILY_NAME_STATS); + + Truth.assertThat(cellsAfterDelete).isEmpty(); + } + + @Test + public void test7_testDeleteTable() throws IOException { + try (BigtableTableAdminClient tableAdminClient = + BigtableTableAdminClient.create(projectId, instanceId)) { + Truth.assertThat(tableAdminClient.exists(TABLE_ID)).isTrue(); + + DeleteTableExample deleteTableExample = new DeleteTableExample(); + deleteTableExample.deleteTable(projectId, instanceId, TABLE_ID); + + Truth.assertThat(tableAdminClient.exists(TABLE_ID)).isFalse(); + } + } +}