|
21 | 21 | import com.google.api.gax.core.ExecutorProvider; |
22 | 22 | import com.google.api.gax.retrying.RetrySettings; |
23 | 23 | import com.google.api.gax.rpc.TransportChannelProvider; |
| 24 | +import com.google.gson.JsonArray; |
24 | 25 | import com.google.protobuf.Descriptors; |
25 | 26 | import java.io.IOException; |
26 | 27 | import java.time.Duration; |
@@ -83,6 +84,41 @@ public ApiFuture<AppendRowsResponse> append(JSONArray jsonArr, long offset) |
83 | 84 | return this.schemaAwareStreamWriter.append(jsonArr, offset); |
84 | 85 | } |
85 | 86 |
|
| 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 | + |
86 | 122 | public String getStreamName() { |
87 | 123 | return this.schemaAwareStreamWriter.getStreamName(); |
88 | 124 | } |
|
0 commit comments