From 357763030d46da9cbc173192c1b16f64d5eecf28 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Tue, 27 Feb 2024 12:23:56 -0500 Subject: [PATCH] handle exception --- .../java/com/example/bigtable/WriteBatch.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java b/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java index b6b6c197c0..09b68ea2e8 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java @@ -18,12 +18,17 @@ // [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"; @@ -32,33 +37,41 @@ public static void writeBatch(String projectId, String instanceId, String tableI // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; + List> batchFutures = new ArrayList<>(); try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { try (Batcher 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, "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, "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 future : batchFutures) { + try { + future.get(); + } catch (ExecutionException entryException) { + System.out.println("Entry failure: " + entryException.getCause()); + } catch (InterruptedException e) { + }g + } } catch (Exception e) { System.out.println("Error during WriteBatch: \n" + e); }