From cb8b0ad25fbf07c3509b3d94244a999b9cb90e54 Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Thu, 23 Dec 2021 11:56:11 -0800 Subject: [PATCH] feat: support `append()` without offset in `StreamWriter` (#1452) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-bigquerystorage/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes # ☕️ --- .../bigquery/storage/v1/JsonStreamWriter.java | 6 ++++-- .../bigquery/storage/v1/StreamWriter.java | 21 +++++++++++++------ .../bigquery/storage/v1/StreamWriterTest.java | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.java index 6b20e89bcb..eb614ff48e 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.java @@ -80,7 +80,8 @@ private JsonStreamWriter(Builder builder) /** * Writes a JSONArray that contains JSONObjects to the BigQuery table by first converting the JSON - * data to protobuf messages, then using StreamWriter's append() to write the data. + * data to protobuf messages, then using StreamWriter's append() to write the data at current end + * of stream. * * @param jsonArr The JSON array that contains JSONObjects to be written * @return ApiFuture returns an AppendRowsResponse message wrapped in an @@ -92,7 +93,8 @@ public ApiFuture append(JSONArray jsonArr) { /** * Writes a JSONArray that contains JSONObjects to the BigQuery table by first converting the JSON - * data to protobuf messages, then using StreamWriter's append() to write the data. + * data to protobuf messages, then using StreamWriter's append() to write the data at the + * specified offset. * * @param jsonArr The JSON array that contains JSONObjects to be written * @param offset Offset for deduplication diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java index 3df82733ba..1cac296a03 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java @@ -224,14 +224,23 @@ public void run(Throwable finalStatus) { } /** - * Schedules the writing of a message. + * Schedules the writing of rows at the end of current stream. * - *

Example of writing a message. + * @param rows the rows in serialized format to write to BigQuery. + * @return the append response wrapped in a future. + */ + public ApiFuture append(ProtoRows rows) { + return append(rows, -1); + } + + /** + * Schedules the writing of rows at given offset. + * + *

Example of writing rows with specific offset. * *

{@code
-   * AppendRowsRequest message;
-   * ApiFuture messageIdFuture = writer.append(message);
-   * ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback() {
+   * ApiFuture future = writer.append(rows, 0);
+   * ApiFutures.addCallback(future, new ApiFutureCallback() {
    *   public void onSuccess(AppendRowsResponse response) {
    *     if (!response.hasError()) {
    *       System.out.println("written with offset: " + response.getAppendResult().getOffset());
@@ -247,7 +256,7 @@ public void run(Throwable finalStatus) {
    * }
* * @param rows the rows in serialized format to write to BigQuery. - * @param offset the offset of the first row. + * @param offset the offset of the first row. Provide -1 to write at the current end of stream. * @return the append response wrapped in a future. */ public ApiFuture append(ProtoRows rows, long offset) { diff --git a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/StreamWriterTest.java b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/StreamWriterTest.java index 9310b70b24..92740d2a4e 100644 --- a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/StreamWriterTest.java +++ b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/StreamWriterTest.java @@ -131,7 +131,7 @@ private AppendRowsResponse createAppendResponseWithError(Status.Code code, Strin } private ApiFuture sendTestMessage(StreamWriter writer, String[] messages) { - return writer.append(createProtoRows(messages), -1); + return writer.append(createProtoRows(messages)); } private ApiFuture sendTestMessage(