From ab3c1453d3c1fb627e773d0e7ca4ec991f8d38b7 Mon Sep 17 00:00:00 2001 From: Mike Wasson <3992422+MikeWasson@users.noreply.github.com> Date: Wed, 24 Mar 2021 17:14:02 -0700 Subject: [PATCH] docs(samples): Check for error from BatchCommitWriteStreams (#940) If the returned BatchCommitWriteStreamsResponse does not have a commit time, it means an error occurred. --- .../com/example/bigquerystorage/WritePendingStream.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java b/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java index 813e59d957..1c7803986b 100644 --- a/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java +++ b/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java @@ -25,6 +25,7 @@ import com.google.cloud.bigquery.storage.v1beta2.CreateWriteStreamRequest; import com.google.cloud.bigquery.storage.v1beta2.FinalizeWriteStreamResponse; import com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter; +import com.google.cloud.bigquery.storage.v1beta2.StorageError; import com.google.cloud.bigquery.storage.v1beta2.TableName; import com.google.cloud.bigquery.storage.v1beta2.WriteStream; import com.google.protobuf.Descriptors.DescriptorValidationException; @@ -90,6 +91,13 @@ public static void writePendingStream(String projectId, String datasetName, Stri .build(); BatchCommitWriteStreamsResponse commitResponse = client.batchCommitWriteStreams(commitRequest); + // If the response does not have a commit time, it means the commit operation failed. + if (commitResponse.hasCommitTime() == false) { + for (StorageError err : commitResponse.getStreamErrorsList()) { + System.out.println(err.getErrorMessage()); + } + throw new RuntimeException("Error committing the streams"); + } System.out.println("Appended and committed records successfully."); } catch (ExecutionException e) { // If the wrapped exception is a StatusRuntimeException, check the state of the operation.