Skip to content

Commit d9afa06

Browse files
authored
samples: add delete samples (#1472)
1 parent e02b5b8 commit d9afa06

File tree

10 files changed

+428
-1
lines changed

10 files changed

+428
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,13 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigtable/tree
502502
| 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) |
503503
| 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) |
504504
| 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) |
505+
| 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) |
506+
| 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) |
507+
| 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) |
508+
| 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) |
509+
| 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) |
510+
| 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) |
511+
| 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) |
505512

506513

507514

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable.deletes;
18+
19+
// [START bigtable_streaming_and_batching]
20+
import com.google.api.gax.batching.Batcher;
21+
import com.google.api.gax.rpc.ServerStream;
22+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
23+
import com.google.cloud.bigtable.data.v2.models.Query;
24+
import com.google.cloud.bigtable.data.v2.models.Row;
25+
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
26+
import java.io.IOException;
27+
28+
public class BatchDeleteExample {
29+
public void batchDelete(String projectId, String instanceId, String tableId)
30+
throws InterruptedException, IOException {
31+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
32+
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
33+
ServerStream<Row> rows = dataClient.readRows(Query.create(tableId));
34+
for (Row row : rows) {
35+
batcher.add(
36+
RowMutationEntry.create(row.getKey()).deleteCells("cell_plan", "data_plan_05gb"));
37+
}
38+
// Blocks until mutations are applied on all submitted row entries.
39+
batcher.flush();
40+
}
41+
}
42+
}
43+
}
44+
// [END bigtable_streaming_and_batching]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable.deletes;
18+
19+
// [START bigtable_delete_check_and_mutate]
20+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
21+
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
22+
import com.google.cloud.bigtable.data.v2.models.Filters;
23+
import com.google.cloud.bigtable.data.v2.models.Mutation;
24+
import java.io.IOException;
25+
26+
public class ConditionalDeleteExample {
27+
public void conditionalDelete(String projectId, String instanceId, String tableId)
28+
throws IOException {
29+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
30+
Filters.Filter condition = Filters.FILTERS.value().exactMatch("PQ2A.190405.004");
31+
Mutation mutation = Mutation.create().deleteCells("stats_summary", "os_build");
32+
dataClient.checkAndMutateRow(
33+
ConditionalRowMutation.create(tableId, "phone#4c410523#20190502")
34+
.condition(condition)
35+
.then(mutation));
36+
}
37+
}
38+
}
39+
// [END bigtable_delete_check_and_mutate]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable.deletes;
18+
19+
// [START bigtable_delete_from_column]
20+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
21+
import com.google.cloud.bigtable.data.v2.models.Mutation;
22+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
23+
import java.io.IOException;
24+
25+
public class DeleteColumnCellsExample {
26+
public void deleteColumnCells(String projectId, String instanceId, String tableId)
27+
throws IOException {
28+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
29+
Mutation mutation = Mutation.create().deleteCells("cell_plan", "data_plan_01gb");
30+
dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation));
31+
}
32+
}
33+
}
34+
// [END bigtable_delete_from_column]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable.deletes;
18+
19+
// [START bigtable_delete_column_family]
20+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
21+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
22+
import java.io.IOException;
23+
24+
public class DeleteColumnFamilyExample {
25+
public void deleteColumnFamily(String projectId, String instanceId, String tableId)
26+
throws IOException {
27+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
28+
dataClient.mutateRow(
29+
RowMutation.create(tableId, "phone#5c10102#20190501").deleteFamily("stats_summary"));
30+
}
31+
}
32+
}
33+
// [END bigtable_delete_column_family]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable.deletes;
18+
19+
// [START bigtable_delete_from_row]
20+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
21+
import com.google.cloud.bigtable.data.v2.models.Mutation;
22+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
23+
import java.io.IOException;
24+
25+
public class DeleteRowExample {
26+
public void deleteRow(String projectId, String instanceId, String tableId) throws IOException {
27+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
28+
Mutation mutation = Mutation.create().deleteRow();
29+
dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation));
30+
}
31+
}
32+
}
33+
// [END bigtable_delete_from_row]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable.deletes;
18+
19+
// [START bigtable_delete_table]
20+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
21+
import java.io.IOException;
22+
23+
public class DeleteTableExample {
24+
public void deleteTable(String projectId, String instanceId, String tableId) throws IOException {
25+
try (BigtableTableAdminClient tableAdminClient =
26+
BigtableTableAdminClient.create(projectId, instanceId)) {
27+
tableAdminClient.deleteTable(tableId);
28+
}
29+
}
30+
}
31+
// [END bigtable_delete_table]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable.deletes;
18+
19+
// [START bigtable_drop_row_range]
20+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
21+
import java.io.IOException;
22+
23+
public class DropRowRangeExample {
24+
public void dropRowRange(String projectId, String instanceId, String tableId) throws IOException {
25+
try (BigtableTableAdminClient tableAdminClient =
26+
BigtableTableAdminClient.create(projectId, instanceId)) {
27+
tableAdminClient.dropRowRange(tableId, "phone#4c410523");
28+
}
29+
}
30+
}
31+
// [END bigtable_drop_row_range]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ public static void writePlanData() throws IOException {
170170
public static void cleanupTable() throws IOException {
171171
try (BigtableTableAdminClient adminClient =
172172
BigtableTableAdminClient.create(projectId, instanceId)) {
173-
adminClient.deleteTable(TABLE_ID);
173+
if (adminClient.exists(TABLE_ID)) {
174+
adminClient.deleteTable(TABLE_ID);
175+
}
174176
} catch (Exception e) {
175177
System.out.println("Error during afterClass: \n" + e.toString());
176178
throw (e);

0 commit comments

Comments
 (0)