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

Commit 5f43103

Browse files
authored
feat: populate location info if we already called GetWriteStream (#1802)
* feat: add get location support * . * add some check on set location if there is already something set
1 parent d8aaed5 commit 5f43103

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ public Descriptor getDescriptor() {
246246
return this.descriptor;
247247
}
248248

249+
/**
250+
* Gets the location of the destination
251+
*
252+
* @return Descriptor
253+
*/
254+
public String getLocation() {
255+
return this.streamWriter.getLocation();
256+
}
257+
249258
/**
250259
* Returns the wait of a request in Client side before sending to the Server. Request could wait
251260
* in Client because it reached the client side inflight request limit (adjustable when
@@ -407,6 +416,7 @@ private Builder(String streamOrTableName, TableSchema tableSchema, BigQueryWrite
407416
TableSchema writeStreamTableSchema = writeStream.getTableSchema();
408417

409418
this.tableSchema = writeStreamTableSchema;
419+
this.location = writeStream.getLocation();
410420
} else {
411421
this.tableSchema = tableSchema;
412422
}
@@ -526,6 +536,10 @@ public Builder setEnableConnectionPool(boolean enableConnectionPool) {
526536
* @return Builder
527537
*/
528538
public Builder setLocation(String location) {
539+
if (this.location != null && !this.location.equals(location)) {
540+
throw new IllegalArgumentException(
541+
"Specified location " + location + " does not match the system value " + this.location);
542+
}
529543
this.location = location;
530544
return this;
531545
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public class StreamWriter implements AutoCloseable {
5151
/** Every writer has a fixed proto schema. */
5252
private final ProtoSchema writerSchema;
5353

54+
/*
55+
* Location of the destination.
56+
*/
57+
private final String location;
58+
5459
/*
5560
* A String that uniquely identifies this writer.
5661
*/
@@ -162,6 +167,7 @@ private StreamWriter(Builder builder) throws IOException {
162167
BigQueryWriteClient client;
163168
this.streamName = builder.streamName;
164169
this.writerSchema = builder.writerSchema;
170+
this.location = builder.location;
165171
boolean ownsBigQueryWriteClient;
166172
if (builder.client == null) {
167173
BigQueryWriteSettings stubSettings =
@@ -193,7 +199,7 @@ private StreamWriter(Builder builder) throws IOException {
193199
client,
194200
ownsBigQueryWriteClient));
195201
} else {
196-
if (builder.location == "") {
202+
if (builder.location == null || builder.location.isEmpty()) {
197203
throw new IllegalArgumentException("Location must be specified for multiplexing client!");
198204
}
199205
// Assume the connection in the same pool share the same client and trace id.
@@ -318,6 +324,11 @@ public ProtoSchema getProtoSchema() {
318324
return writerSchema;
319325
}
320326

327+
/** @return the location of the destination. */
328+
public String getLocation() {
329+
return location;
330+
}
331+
321332
/** Close the stream writer. Shut down all resources. */
322333
@Override
323334
public void close() {
@@ -379,7 +390,7 @@ public static final class Builder {
379390

380391
private TableSchema updatedTableSchema = null;
381392

382-
private String location;
393+
private String location = null;
383394

384395
private boolean enableConnectionPool = false;
385396

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,48 @@ public void testAppendOutOfRangeException() throws Exception {
393393
public void testCreateDefaultStream() throws Exception {
394394
TableSchema tableSchema =
395395
TableSchema.newBuilder().addFields(0, TEST_INT).addFields(1, TEST_STRING).build();
396+
testBigQueryWrite.addResponse(
397+
WriteStream.newBuilder()
398+
.setName(TEST_STREAM)
399+
.setLocation("aa")
400+
.setTableSchema(tableSchema)
401+
.build());
396402
try (JsonStreamWriter writer =
397-
JsonStreamWriter.newBuilder(TEST_TABLE, tableSchema)
403+
JsonStreamWriter.newBuilder(TEST_TABLE, client)
398404
.setChannelProvider(channelProvider)
399405
.setCredentialsProvider(NoCredentialsProvider.create())
400406
.build()) {
401407
assertEquals("projects/p/datasets/d/tables/t/_default", writer.getStreamName());
408+
assertEquals("aa", writer.getLocation());
402409
}
403410
}
404411

412+
@Test
413+
public void testCreateDefaultStreamWrongLocation() throws Exception {
414+
TableSchema tableSchema =
415+
TableSchema.newBuilder().addFields(0, TEST_INT).addFields(1, TEST_STRING).build();
416+
testBigQueryWrite.addResponse(
417+
WriteStream.newBuilder()
418+
.setName(TEST_STREAM)
419+
.setLocation("aa")
420+
.setTableSchema(tableSchema)
421+
.build());
422+
IllegalArgumentException ex =
423+
assertThrows(
424+
IllegalArgumentException.class,
425+
new ThrowingRunnable() {
426+
@Override
427+
public void run() throws Throwable {
428+
JsonStreamWriter.newBuilder(TEST_TABLE, client)
429+
.setChannelProvider(channelProvider)
430+
.setCredentialsProvider(NoCredentialsProvider.create())
431+
.setLocation("bb")
432+
.build();
433+
}
434+
});
435+
assertEquals("Specified location bb does not match the system value aa", ex.getMessage());
436+
}
437+
405438
@Test
406439
public void testSimpleSchemaUpdate() throws Exception {
407440
try (JsonStreamWriter writer =

0 commit comments

Comments
 (0)