Skip to content

Commit b8dff57

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

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

google-cloud-bigquerystorage/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<groupId>com.google.api.grpc</groupId>
118118
<artifactId>proto-google-cloud-bigquerystorage-v1</artifactId>
119119
</dependency>
120+
<dependency>
121+
<groupId>com.google.code.gson</groupId>
122+
<artifactId>gson</artifactId>
123+
<version>2.12.1</version>
124+
</dependency>
120125
<dependency>
121126
<groupId>com.google.guava</groupId>
122127
<artifactId>guava</artifactId>

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

Lines changed: 36 additions & 0 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;
@@ -83,6 +84,41 @@ public ApiFuture<AppendRowsResponse> append(JSONArray jsonArr, long offset)
8384
return this.schemaAwareStreamWriter.append(jsonArr, offset);
8485
}
8586

87+
private JSONArray gsonToOrgJSON(JsonArray jsonArr) {
88+
return new JSONArray(jsonArr.toString());
89+
}
90+
91+
/**
92+
* Writes a JsonArray that contains JsonObjects to the BigQuery table by first converting the JSON
93+
* data to protobuf messages, then using StreamWriter's append() to write the data at current end
94+
* of stream. If there is a schema update, the current StreamWriter is closed. A new StreamWriter
95+
* is created with the updated TableSchema.
96+
*
97+
* @param jsonArr The JSON array that contains JsonObjects to be written
98+
* @return {@code ApiFuture<AppendRowsResponse>} returns an AppendRowsResponse message wrapped in
99+
* an ApiFuture
100+
*/
101+
public ApiFuture<AppendRowsResponse> append(JsonArray jsonArr)
102+
throws IOException, Descriptors.DescriptorValidationException {
103+
return this.append(jsonArr, -1);
104+
}
105+
106+
/**
107+
* Writes a JsonArray that contains JsonObjects to the BigQuery table by first converting the JSON
108+
* data to protobuf messages, then using StreamWriter's append() to write the data at the
109+
* specified offset. If there is a schema update, the current StreamWriter is closed. A new
110+
* StreamWriter is created with the updated TableSchema.
111+
*
112+
* @param jsonArr The JSON array that contains JSONObjects to be written
113+
* @param offset Offset for deduplication
114+
* @return {@code ApiFuture<AppendRowsResponse>} returns an AppendRowsResponse message wrapped in
115+
* an ApiFuture
116+
*/
117+
public ApiFuture<AppendRowsResponse> append(JsonArray jsonArr, long offset)
118+
throws IOException, Descriptors.DescriptorValidationException {
119+
return this.append(gsonToOrgJSON(jsonArr), offset);
120+
}
121+
86122
public String getStreamName() {
87123
return this.schemaAwareStreamWriter.getStreamName();
88124
}

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 =

0 commit comments

Comments
 (0)