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: Update WriteBatch sample to use a batcher #2135

Merged
merged 8 commits into from
Feb 27, 2024
Merged
Changes from all 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
70 changes: 42 additions & 28 deletions samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

// [START bigtable_writes_batch]

import com.google.api.core.ApiFuture;
import com.google.api.gax.batching.Batcher;
import com.google.api.gax.batching.BatchingException;
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.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class WriteBatch {
private static final String COLUMN_FAMILY_NAME = "stats_summary";
Expand All @@ -32,34 +37,43 @@ public static void writeBatch(String projectId, String instanceId, String tableI
// String tableId = "mobile-time-series";

try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
long timestamp = System.currentTimeMillis() * 1000;
List<ApiFuture<Void>> batchFutures = new ArrayList<>();
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
long timestamp = System.currentTimeMillis() * 1000;
batchFutures.add(
batcher.add(
RowMutationEntry.create("tablet#a0b81f74#20190501")
.setCell(
COLUMN_FAMILY_NAME, ByteString.copyFromUtf8("connected_wifi"), timestamp, 1)
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")));
batchFutures.add(
batcher.add(
RowMutationEntry.create("tablet#a0b81f74#20190502")
.setCell(
COLUMN_FAMILY_NAME, ByteString.copyFromUtf8("connected_wifi"), timestamp, 1)
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6")));

BulkMutation bulkMutation =
BulkMutation.create(tableId)
.add(
"tablet#a0b81f74#20190501",
Mutation.create()
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom("connected_wifi".getBytes()),
timestamp,
1)
.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,
1)
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6"));

dataClient.bulkMutateRows(bulkMutation);

System.out.print("Successfully wrote 2 rows");
// Blocks until mutations are applied on all submitted row entries.
// flush will be called automatically when a batch is full.
batcher.flush();
// Before batcher is closed, all remaining (if any) mutations are applied.
} catch (BatchingException batchingException) {
System.out.println(
"At least one entry failed to apply. Summary of the errors: \n" + batchingException);
// get individual entry error details
for (ApiFuture<Void> future : batchFutures) {
try {
future.get();
} catch (ExecutionException entryException) {
System.out.println("Entry failure: " + entryException.getCause());
} catch (InterruptedException e) {
// handle interrupted exception
}
}
}
System.out.println("Successfully wrote 2 rows");
} catch (Exception e) {
mutianf marked this conversation as resolved.
Show resolved Hide resolved
System.out.println("Error during WriteBatch: \n" + e.toString());
System.out.println("Error during WriteBatch: \n" + e);
}
}
}
Expand Down
Loading