Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add sample to indicate how to recreate a stream writer. #1982

Closed
wants to merge 10 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 =
yirutang marked this conversation as resolved.
Show resolved Hide resolved
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