1616package com .google .cloud .bigquery .storage .v1beta2 ;
1717
1818import com .google .api .core .ApiFuture ;
19- import com .google .api .gax .batching .BatchingSettings ;
2019import com .google .api .gax .batching .FlowControlSettings ;
2120import com .google .api .gax .core .CredentialsProvider ;
2221import com .google .api .gax .rpc .TransportChannelProvider ;
2322import com .google .cloud .bigquery .Schema ;
2423import com .google .common .base .Preconditions ;
2524import com .google .protobuf .Descriptors ;
2625import com .google .protobuf .Descriptors .Descriptor ;
27- import com .google .protobuf .Int64Value ;
2826import com .google .protobuf .Message ;
2927import java .io .IOException ;
3028import java .util .logging .Logger ;
29+ import java .util .regex .Matcher ;
3130import java .util .regex .Pattern ;
3231import javax .annotation .Nullable ;
3332import org .json .JSONArray ;
@@ -46,8 +45,8 @@ public class JsonStreamWriter implements AutoCloseable {
4645
4746 private BigQueryWriteClient client ;
4847 private String streamName ;
49- private StreamWriter streamWriter ;
50- private StreamWriter .Builder streamWriterBuilder ;
48+ private StreamWriterV2 streamWriter ;
49+ private StreamWriterV2 .Builder streamWriterBuilder ;
5150 private Descriptor descriptor ;
5251 private TableSchema tableSchema ;
5352
@@ -64,18 +63,18 @@ private JsonStreamWriter(Builder builder)
6463 BQTableSchemaToProtoDescriptor .convertBQTableSchemaToProtoDescriptor (builder .tableSchema );
6564
6665 if (this .client == null ) {
67- streamWriterBuilder = StreamWriter .newBuilder (builder .streamOrTableName );
66+ streamWriterBuilder = StreamWriterV2 .newBuilder (builder .streamName );
6867 } else {
69- streamWriterBuilder = StreamWriter .newBuilder (builder .streamOrTableName , builder .client );
68+ streamWriterBuilder = StreamWriterV2 .newBuilder (builder .streamName , builder .client );
7069 }
70+ streamWriterBuilder .setWriterSchema (ProtoSchemaConverter .convert (this .descriptor ));
7171 setStreamWriterSettings (
7272 builder .channelProvider ,
7373 builder .credentialsProvider ,
7474 builder .endpoint ,
75- builder .flowControlSettings ,
76- builder .createDefaultStream );
75+ builder .flowControlSettings );
7776 this .streamWriter = streamWriterBuilder .build ();
78- this .streamName = this . streamWriter . getStreamNameString () ;
77+ this .streamName = builder . streamName ;
7978 }
8079
8180 /**
@@ -109,17 +108,10 @@ public ApiFuture<AppendRowsResponse> append(JSONArray jsonArr, long offset) {
109108 Message protoMessage = JsonToProtoMessage .convertJsonToProtoMessage (this .descriptor , json );
110109 rowsBuilder .addSerializedRows (protoMessage .toByteString ());
111110 }
112- AppendRowsRequest .ProtoData .Builder data = AppendRowsRequest .ProtoData .newBuilder ();
113111 // Need to make sure refreshAppendAndSetDescriptor finish first before this can run
114112 synchronized (this ) {
115- data .setWriterSchema (ProtoSchemaConverter .convert (this .descriptor ));
116- data .setRows (rowsBuilder .build ());
117- AppendRowsRequest .Builder request = AppendRowsRequest .newBuilder ().setProtoRows (data .build ());
118- if (offset >= 0 ) {
119- request .setOffset (Int64Value .of (offset ));
120- }
121113 final ApiFuture <AppendRowsResponse > appendResponseFuture =
122- this .streamWriter .append (request .build ());
114+ this .streamWriter .append (rowsBuilder .build (), offset );
123115 return appendResponseFuture ;
124116 }
125117 }
@@ -134,7 +126,7 @@ public ApiFuture<AppendRowsResponse> append(JSONArray jsonArr, long offset) {
134126 void refreshConnection ()
135127 throws IOException , InterruptedException , Descriptors .DescriptorValidationException {
136128 synchronized (this ) {
137- this .streamWriter .shutdown ();
129+ this .streamWriter .close ();
138130 this .streamWriter = streamWriterBuilder .build ();
139131 this .descriptor =
140132 BQTableSchemaToProtoDescriptor .convertBQTableSchemaToProtoDescriptor (this .tableSchema );
@@ -164,41 +156,28 @@ private void setStreamWriterSettings(
164156 @ Nullable TransportChannelProvider channelProvider ,
165157 @ Nullable CredentialsProvider credentialsProvider ,
166158 @ Nullable String endpoint ,
167- @ Nullable FlowControlSettings flowControlSettings ,
168- Boolean createDefaultStream ) {
159+ @ Nullable FlowControlSettings flowControlSettings ) {
169160 if (channelProvider != null ) {
170161 streamWriterBuilder .setChannelProvider (channelProvider );
171162 }
172163 if (credentialsProvider != null ) {
173164 streamWriterBuilder .setCredentialsProvider (credentialsProvider );
174165 }
175- BatchingSettings .Builder batchSettingBuilder =
176- BatchingSettings .newBuilder ()
177- .setElementCountThreshold (1L )
178- .setRequestByteThreshold (4 * 1024 * 1024L );
179- if (flowControlSettings != null ) {
180- streamWriterBuilder .setBatchingSettings (
181- batchSettingBuilder .setFlowControlSettings (flowControlSettings ).build ());
182- } else {
183- streamWriterBuilder .setBatchingSettings (batchSettingBuilder .build ());
184- }
185166 if (endpoint != null ) {
186167 streamWriterBuilder .setEndpoint (endpoint );
187168 }
188- if (createDefaultStream ) {
189- streamWriterBuilder .createDefaultStream ();
169+ if (flowControlSettings != null ) {
170+ if (flowControlSettings .getMaxOutstandingRequestBytes () != null ) {
171+ streamWriterBuilder .setMaxInflightBytes (
172+ flowControlSettings .getMaxOutstandingRequestBytes ());
173+ }
174+ if (flowControlSettings .getMaxOutstandingElementCount () != null ) {
175+ streamWriterBuilder .setMaxInflightRequests (
176+ flowControlSettings .getMaxOutstandingElementCount ());
177+ }
190178 }
191179 }
192180
193- /**
194- * Setter for table schema. Used for schema updates.
195- *
196- * @param tableSchema
197- */
198- void setTableSchema (TableSchema tableSchema ) {
199- this .tableSchema = tableSchema ;
200- }
201-
202181 /**
203182 * newBuilder that constructs a JsonStreamWriter builder with BigQuery client being initialized by
204183 * StreamWriter by default.
@@ -259,7 +238,7 @@ public void close() {
259238 }
260239
261240 public static final class Builder {
262- private String streamOrTableName ;
241+ private String streamName ;
263242 private BigQueryWriteClient client ;
264243 private TableSchema tableSchema ;
265244
@@ -269,6 +248,13 @@ public static final class Builder {
269248 private String endpoint ;
270249 private boolean createDefaultStream = false ;
271250
251+ private static String streamPatternString =
252+ "(projects/[^/]+/datasets/[^/]+/tables/[^/]+)/streams/[^/]+" ;
253+ private static String tablePatternString = "(projects/[^/]+/datasets/[^/]+/tables/[^/]+)" ;
254+
255+ private static Pattern streamPattern = Pattern .compile (streamPatternString );
256+ private static Pattern tablePattern = Pattern .compile (tablePatternString );
257+
272258 /**
273259 * Constructor for JsonStreamWriter's Builder
274260 *
@@ -279,7 +265,17 @@ public static final class Builder {
279265 * @param client
280266 */
281267 private Builder (String streamOrTableName , TableSchema tableSchema , BigQueryWriteClient client ) {
282- this .streamOrTableName = streamOrTableName ;
268+ Matcher streamMatcher = streamPattern .matcher (streamOrTableName );
269+ if (!streamMatcher .matches ()) {
270+ Matcher tableMatcher = tablePattern .matcher (streamOrTableName );
271+ if (!tableMatcher .matches ()) {
272+ throw new IllegalArgumentException ("Invalid name: " + streamOrTableName );
273+ } else {
274+ this .streamName = streamOrTableName + "/_default" ;
275+ }
276+ } else {
277+ this .streamName = streamOrTableName ;
278+ }
283279 this .tableSchema = tableSchema ;
284280 this .client = client ;
285281 }
@@ -322,13 +318,12 @@ public Builder setFlowControlSettings(FlowControlSettings flowControlSettings) {
322318 }
323319
324320 /**
325- * If it is writing to a default stream .
321+ * Stream name on the builder .
326322 *
327323 * @return Builder
328324 */
329- public Builder createDefaultStream () {
330- this .createDefaultStream = true ;
331- return this ;
325+ public String getStreamName () {
326+ return streamName ;
332327 }
333328
334329 /**
0 commit comments