Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: add delete samples #1472

Merged
merged 10 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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_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 DeleteCheckAndMutateExample {
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
public void checkAndMutate(
String projectId,
String instanceId,
String tableId,
String rowKey,
String family,
String qualifier) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Filters.Filter condition = Filters.FILTERS.qualifier().exactMatch(qualifier);
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
Mutation mutation = Mutation.create().deleteCells(family, qualifier);
dataClient.checkAndMutateRow(
ConditionalRowMutation.create(tableId, rowKey).condition(condition).then(mutation));
} catch (IOException e) {
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
System.err.println("An exception has occurred: " + e.getMessage());
}
}
}
// [END bigtable_delete_check_and_mutate]
Original file line number Diff line number Diff line change
@@ -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_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, String rowKey, String familyName) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
dataClient.mutateRow(RowMutation.create(tableId, rowKey).deleteFamily(familyName));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I saw the delete table example, I rethought about this one. This is a bit unclear because you're not deleting the entire column family from the table right? You're just deleting the data in the family for the row?

Anyway, if needed might want a separate example doing deleting column families from the table if it is needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably clarified by the documentation/reading the code – I think I might've been thrown off by it since I didn't know you can delete all the column family data for a row and defaulting to thinking about deleting CF for the table

} catch (IOException e) {
System.err.println("An exception has occurred: " + e.getMessage());
}
}
}
// [END bigtable_delete_column_family]
Original file line number Diff line number Diff line change
@@ -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_drop_row_range]
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import java.io.IOException;

public class DeleteDropRowRangeExample {
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
public void dropRowRange(String projectId, String instanceId, String tableId, String rowPrefix) {
try (BigtableTableAdminClient tableAdminClient =
BigtableTableAdminClient.create(projectId, instanceId)) {
tableAdminClient.dropRowRange(tableId, rowPrefix);
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
System.err.println("An exception has occurred: " + e.getMessage());
}
}
}
// [END bigtable_drop_row_range]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 DeleteFromColumnExample {
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
public void deleteFromColumn(
String projectId,
String instanceId,
String tableId,
String rowKey,
String familyName,
String qualifier) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Mutation mutation = Mutation.create().deleteCells(familyName, qualifier);
dataClient.mutateRow(RowMutation.create(tableId, rowKey, mutation));
} catch (IOException e) {
System.err.println("An exception has occurred: " + e.getMessage());
}
}
}
// [END bigtable_delete_from_column]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 DeleteFromRowExample {
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
public void deleteFromRow(String projectId, String instanceId, String tableId, String rowKey) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Mutation mutation = Mutation.create().deleteRow();
dataClient.mutateRow(RowMutation.create(tableId, rowKey, mutation));
} catch (IOException e) {
System.err.println("An exception has occurred: " + e.getMessage());
}
}
}
// [END bigtable_delete_from_row]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.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.RowMutationEntry;
import java.io.IOException;

public class DeleteStreamingAndBatchingExample {
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
public void streamingAndBatching(
String projectId, String instanceId, String tableId, String familyName, String qualifier) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
ServerStream<Row> rows =
dataClient.readRows(Query.create(tableId).filter(Filters.FILTERS.pass()));
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
for (Row row : rows) {
batcher.add(RowMutationEntry.create(row.getKey()).deleteCells(familyName, qualifier));
}
// Blocks until mutations are applied on all submitted row entries.
batcher.flush();
} catch (InterruptedException e) {
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
e.printStackTrace();
}
} catch (IOException e) {
System.err.println("An exception has occurred: " + e.getMessage());
}
}
}
// [END bigtable_streaming_and_batching]
Original file line number Diff line number Diff line change
@@ -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_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) {
try (BigtableTableAdminClient tableAdminClient =
BigtableTableAdminClient.create(projectId, instanceId)) {
tableAdminClient.deleteTable(tableId);
} catch (IOException e) {
System.err.println("An exception has occurred: " + e.getMessage());
}
}
}
// [END bigtable_delete_table]
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading