Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit a5b8a0f

Browse files
feat: provide append() methods that accept com.google.gson objects
1 parent d67ec1a commit a5b8a0f

9 files changed

Lines changed: 116 additions & 105 deletions

File tree

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/ConnectionWorker.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,7 @@ public long getInflightWaitSeconds() {
679679
return inflightWaitSec.longValue();
680680
}
681681

682-
/**
683-
* @return a unique Id for the writer.
684-
*/
682+
/** @return a unique Id for the writer. */
685683
public String getWriterId() {
686684
return writerId;
687685
}

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.api.gax.core.ExecutorProvider;
2222
import com.google.api.gax.retrying.RetrySettings;
2323
import com.google.api.gax.rpc.TransportChannelProvider;
24+
import com.google.gson.JsonArray;
2425
import com.google.protobuf.Descriptors;
2526
import java.io.IOException;
2627
import java.time.Duration;
@@ -45,9 +46,7 @@ public class JsonStreamWriter implements AutoCloseable {
4546
* @param builder The Builder object for the JsonStreamWriter
4647
*/
4748
private JsonStreamWriter(SchemaAwareStreamWriter.Builder<Object> builder)
48-
throws Descriptors.DescriptorValidationException,
49-
IllegalArgumentException,
50-
IOException,
49+
throws Descriptors.DescriptorValidationException, IllegalArgumentException, IOException,
5150
InterruptedException {
5251
this.schemaAwareStreamWriter = builder.build();
5352
}
@@ -83,13 +82,46 @@ public ApiFuture<AppendRowsResponse> append(JSONArray jsonArr, long offset)
8382
return this.schemaAwareStreamWriter.append(jsonArr, offset);
8483
}
8584

86-
public String getStreamName() {
87-
return this.schemaAwareStreamWriter.getStreamName();
85+
private JSONArray gsonToOrgJSON(JsonArray jsonArr) {
86+
return new JSONArray(jsonArr.toString());
87+
}
88+
89+
/**
90+
* Writes a JsonArray that contains JsonObjects to the BigQuery table by first converting the JSON
91+
* data to protobuf messages, then using StreamWriter's append() to write the data at current end
92+
* of stream. If there is a schema update, the current StreamWriter is closed. A new StreamWriter
93+
* is created with the updated TableSchema.
94+
*
95+
* @param jsonArr The JSON array that contains JsonObjects to be written
96+
* @return {@code ApiFuture<AppendRowsResponse>} returns an AppendRowsResponse message wrapped in
97+
* an ApiFuture
98+
*/
99+
public ApiFuture<AppendRowsResponse> append(JsonArray jsonArr)
100+
throws IOException, Descriptors.DescriptorValidationException {
101+
return this.append(jsonArr, -1);
88102
}
89103

90104
/**
91-
* @return A unique Id for this writer.
105+
* Writes a JsonArray that contains JsonObjects to the BigQuery table by first converting the JSON
106+
* data to protobuf messages, then using StreamWriter's append() to write the data at the
107+
* specified offset. If there is a schema update, the current StreamWriter is closed. A new
108+
* StreamWriter is created with the updated TableSchema.
109+
*
110+
* @param jsonArr The JSON array that contains JSONObjects to be written
111+
* @param offset Offset for deduplication
112+
* @return {@code ApiFuture<AppendRowsResponse>} returns an AppendRowsResponse message wrapped in
113+
* an ApiFuture
92114
*/
115+
public ApiFuture<AppendRowsResponse> append(JsonArray jsonArr, long offset)
116+
throws IOException, Descriptors.DescriptorValidationException {
117+
return this.append(gsonToOrgJSON(jsonArr), offset);
118+
}
119+
120+
public String getStreamName() {
121+
return this.schemaAwareStreamWriter.getStreamName();
122+
}
123+
124+
/** @return A unique Id for this writer. */
93125
public String getWriterId() {
94126
return this.schemaAwareStreamWriter.getWriterId();
95127
}
@@ -123,9 +155,7 @@ public long getInflightWaitSeconds() {
123155
return this.schemaAwareStreamWriter.getInflightWaitSeconds();
124156
}
125157

126-
/**
127-
* @return the missing value interpretation map used for the writer.
128-
*/
158+
/** @return the missing value interpretation map used for the writer. */
129159
public Map<String, AppendRowsRequest.MissingValueInterpretation>
130160
getMissingValueInterpretationMap() {
131161
return this.schemaAwareStreamWriter.getMissingValueInterpretationMap();
@@ -213,9 +243,7 @@ public boolean isClosed() {
213243
return this.schemaAwareStreamWriter.isClosed();
214244
}
215245

216-
/**
217-
* @return if user explicitly closed the writer.
218-
*/
246+
/** @return if user explicitly closed the writer. */
219247
public boolean isUserClosed() {
220248
return this.schemaAwareStreamWriter.isUserClosed();
221249
}
@@ -444,9 +472,7 @@ public Builder setMissingValueInterpretationMap(
444472
* @return JsonStreamWriter
445473
*/
446474
public JsonStreamWriter build()
447-
throws Descriptors.DescriptorValidationException,
448-
IllegalArgumentException,
449-
IOException,
475+
throws Descriptors.DescriptorValidationException, IllegalArgumentException, IOException,
450476
InterruptedException {
451477
return new JsonStreamWriter(this.schemaAwareStreamWriterBuilder);
452478
}

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/SchemaAwareStreamWriter.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,12 @@ ApiFuture<AppendRowsResponse> appendWithUniqueId(
261261
}
262262
}
263263

264-
/**
265-
* @return The name of the write stream associated with this writer.
266-
*/
264+
/** @return The name of the write stream associated with this writer. */
267265
public String getStreamName() {
268266
return this.streamName;
269267
}
270268

271-
/**
272-
* @return A unique Id for this writer.
273-
*/
269+
/** @return A unique Id for this writer. */
274270
public String getWriterId() {
275271
return streamWriter.getWriterId();
276272
}
@@ -304,9 +300,7 @@ public long getInflightWaitSeconds() {
304300
return streamWriter.getInflightWaitSeconds();
305301
}
306302

307-
/**
308-
* @return the missing value interpretation map used for the writer.
309-
*/
303+
/** @return the missing value interpretation map used for the writer. */
310304
public Map<String, AppendRowsRequest.MissingValueInterpretation>
311305
getMissingValueInterpretationMap() {
312306
return streamWriter.getMissingValueInterpretationMap();
@@ -442,9 +436,7 @@ public boolean isClosed() {
442436
return this.streamWriter.isClosed();
443437
}
444438

445-
/**
446-
* @return if user explicitly closed the writer.
447-
*/
439+
/** @return if user explicitly closed the writer. */
448440
public boolean isUserClosed() {
449441
return this.streamWriter.isUserClosed();
450442
}
@@ -730,9 +722,7 @@ public Builder setEnableOpenTelemetry(boolean enableOpenTelemetry) {
730722
* @return SchemaAwareStreamWriter
731723
*/
732724
public SchemaAwareStreamWriter<T> build()
733-
throws DescriptorValidationException,
734-
IllegalArgumentException,
735-
IOException,
725+
throws DescriptorValidationException, IllegalArgumentException, IOException,
736726
InterruptedException {
737727
return new SchemaAwareStreamWriter<>(this);
738728
}

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -508,37 +508,27 @@ public long getInflightWaitSeconds() {
508508
return singleConnectionOrConnectionPool.getInflightWaitSeconds(this);
509509
}
510510

511-
/**
512-
* @return a unique Id for the writer.
513-
*/
511+
/** @return a unique Id for the writer. */
514512
public String getWriterId() {
515513
return singleConnectionOrConnectionPool.getWriterId(writerId);
516514
}
517515

518-
/**
519-
* @return name of the Stream that this writer is working on.
520-
*/
516+
/** @return name of the Stream that this writer is working on. */
521517
public String getStreamName() {
522518
return streamName;
523519
}
524520

525-
/**
526-
* @return the passed in user schema.
527-
*/
521+
/** @return the passed in user schema. */
528522
public ProtoSchema getProtoSchema() {
529523
return writerSchema;
530524
}
531525

532-
/**
533-
* @return the location of the destination.
534-
*/
526+
/** @return the location of the destination. */
535527
public String getLocation() {
536528
return location;
537529
}
538530

539-
/**
540-
* @return the missing value interpretation map used for the writer.
541-
*/
531+
/** @return the missing value interpretation map used for the writer. */
542532
public Map<String, AppendRowsRequest.MissingValueInterpretation>
543533
getMissingValueInterpretationMap() {
544534
return missingValueInterpretationMap;
@@ -559,9 +549,7 @@ public boolean isClosed() {
559549
}
560550
}
561551

562-
/**
563-
* @return if user explicitly closed the writer.
564-
*/
552+
/** @return if user explicitly closed the writer. */
565553
public boolean isUserClosed() {
566554
return userClosed.get();
567555
}
@@ -610,9 +598,7 @@ public static void setMaxRequestCallbackWaitTime(Duration waitTime) {
610598
ConnectionWorker.MAXIMUM_REQUEST_CALLBACK_WAIT_TIME = waitTime;
611599
}
612600

613-
/**
614-
* @return the default stream name associated with tableName
615-
*/
601+
/** @return the default stream name associated with tableName */
616602
public static String getDefaultStreamName(TableName tableName) {
617603
return tableName + defaultStreamMatching;
618604
}

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonStreamWriter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ public class JsonStreamWriter implements AutoCloseable {
5858
* @param builder The Builder object for the JsonStreamWriter
5959
*/
6060
private JsonStreamWriter(Builder builder)
61-
throws Descriptors.DescriptorValidationException,
62-
IllegalArgumentException,
63-
IOException,
61+
throws Descriptors.DescriptorValidationException, IllegalArgumentException, IOException,
6462
InterruptedException {
6563
this.client = builder.client;
6664
this.descriptor =
@@ -333,9 +331,7 @@ public Builder setTraceId(String traceId) {
333331
* @return JsonStreamWriter
334332
*/
335333
public JsonStreamWriter build()
336-
throws Descriptors.DescriptorValidationException,
337-
IllegalArgumentException,
338-
IOException,
334+
throws Descriptors.DescriptorValidationException, IllegalArgumentException, IOException,
339335
InterruptedException {
340336
return new JsonStreamWriter(this);
341337
}

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriterTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import com.google.cloud.bigquery.storage.v1.Exceptions.AppendSerializationError;
3939
import com.google.cloud.bigquery.storage.v1.TableFieldSchema.Mode;
4040
import com.google.common.collect.ImmutableMap;
41+
import com.google.gson.JsonArray;
42+
import com.google.gson.JsonObject;
4143
import com.google.protobuf.ByteString;
4244
import com.google.protobuf.Descriptors.DescriptorValidationException;
4345
import com.google.protobuf.Int64Value;
@@ -251,6 +253,49 @@ public void testSingleAppendSimpleJson() throws Exception {
251253
}
252254
}
253255

256+
@Test
257+
public void testSingleAppendSimpleGson() throws Exception {
258+
FooType expectedProto = FooType.newBuilder().setFoo("allen").build();
259+
JsonObject foo = new JsonObject();
260+
foo.addProperty("foo", "allen");
261+
JsonArray jsonArr = new JsonArray();
262+
jsonArr.add(foo);
263+
264+
try (JsonStreamWriter writer =
265+
getTestJsonStreamWriterBuilder(TEST_STREAM, TABLE_SCHEMA)
266+
.setTraceId("test:empty")
267+
.build()) {
268+
269+
testBigQueryWrite.addResponse(
270+
AppendRowsResponse.newBuilder()
271+
.setAppendResult(
272+
AppendRowsResponse.AppendResult.newBuilder().setOffset(Int64Value.of(0)).build())
273+
.build());
274+
275+
ApiFuture<AppendRowsResponse> appendFuture = writer.append(jsonArr);
276+
assertEquals(0L, appendFuture.get().getAppendResult().getOffset().getValue());
277+
appendFuture.get();
278+
assertEquals(
279+
1,
280+
testBigQueryWrite
281+
.getAppendRequests()
282+
.get(0)
283+
.getProtoRows()
284+
.getRows()
285+
.getSerializedRowsCount());
286+
assertEquals(
287+
testBigQueryWrite
288+
.getAppendRequests()
289+
.get(0)
290+
.getProtoRows()
291+
.getRows()
292+
.getSerializedRows(0),
293+
expectedProto.toByteString());
294+
assertEquals(
295+
"java-jsonwriter test:empty", testBigQueryWrite.getAppendRequests().get(0).getTraceId());
296+
}
297+
}
298+
254299
@Test
255300
public void testFlexibleColumnAppend() throws Exception {
256301
TableFieldSchema field =

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryBigDecimalByteStringEncoderTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ public static void afterClass() {
102102

103103
@Test
104104
public void TestBigDecimalEncoding()
105-
throws IOException,
106-
InterruptedException,
107-
ExecutionException,
105+
throws IOException, InterruptedException, ExecutionException,
108106
Descriptors.DescriptorValidationException {
109107
TableName parent = TableName.of(ServiceOptions.getDefaultProjectId(), DATASET, TABLE);
110108
TableFieldSchema TEST_NUMERIC_ZERO =

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryTimeEncoderTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ public static void afterClass() {
109109

110110
@Test
111111
public void TestTimeEncoding()
112-
throws IOException,
113-
InterruptedException,
114-
ExecutionException,
112+
throws IOException, InterruptedException, ExecutionException,
115113
Descriptors.DescriptorValidationException {
116114
TableName parent = TableName.of(ServiceOptions.getDefaultProjectId(), DATASET, TABLE);
117115
TableFieldSchema TEST_STRING =

0 commit comments

Comments
 (0)