KEMBAR78
feat: Add sample to indicate how to recreate a stream writer. by yirutang · Pull Request #1982 · googleapis/java-bigquerystorage · GitHub
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,26 @@ private static class DataWriter {
// Track the number of in-flight requests to wait for all responses before shutting down.
private final Phaser inflightRequestCount = new Phaser(1);
private final Object lock = new Object();

private JsonStreamWriter streamWriter;

@GuardedBy("lock")
private RuntimeException error = null;

public void initialize(TableName parentTable)
throws DescriptorValidationException, IOException, InterruptedException {
initialize(parentTable.toString());
}

private void initialize(String streamOrTableName)
throws DescriptorValidationException, IOException, InterruptedException {
// Use the JSON stream writer to send records in JSON format. Specify the table name to write
// to the default stream.
// For more information about JsonStreamWriter, see:
// https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.html
streamWriter =
JsonStreamWriter.newBuilder(parentTable.toString(), BigQueryWriteClient.create()).build();
JsonStreamWriter.newBuilder(streamOrTableName, BigQueryWriteClient.create())
.build();
}

public void append(AppendContext appendContext)
Expand All @@ -158,6 +165,13 @@ public void append(AppendContext appendContext)
throw this.error;
}
}
// All the retraible errors should have been retried within StreamWriter, when user saw the
// writer becomes bad, most likely it is not recoverable error. The sample code here shows
// a possibility of recreate the writer but most likely your program wouldn't want it.
// if (streamWriter.isClosed() && !streamWriter.isUserClosed()) {
// streamWriter.close();
// initialize(streamWriter.getStreamName());
// }
// Append asynchronously for increased throughput.
ApiFuture<AppendRowsResponse> future = streamWriter.append(appendContext.data);
ApiFutures.addCallback(
Expand Down