Skip to content

Commit

Permalink
handle exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed Feb 27, 2024
1 parent bed8006 commit df00b0b
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +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.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,33 +36,39 @@ public static void writeBatch(String projectId, String instanceId, String tableI
// String instanceId = "my-instance-id";
// String tableId = "mobile-time-series";

List<ApiFuture<Void>> batchFutures = new ArrayList<>();
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
long timestamp = System.currentTimeMillis() * 1000;

batcher.add(
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"));
batcher.add(
.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"));
.setCell(
COLUMN_FAMILY_NAME, ByteString.copyFromUtf8("connected_wifi"), timestamp, 1)
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6")));

// 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.

System.out.print("Successfully wrote 2 rows");
} catch (BatchingException e) {
System.out.println("Some mutations failed to apply: \n" + e);
} catch (BatchingException batchingException) {
System.out.println(
"At least one entry failed to apply. Summary of the errors: \n" + batchingException);
for (ApiFuture<Void> future : batchFutures) {
try {
future.get();
} catch (ExecutionException entryException) {
System.out.println("Entry failure: " + entryException.getCause());
} catch (InterruptedException e) {
}
}
} catch (Exception e) {
System.out.println("Error during WriteBatch: \n" + e);
}
Expand Down

0 comments on commit df00b0b

Please sign in to comment.