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

fix: fix WriteToDefaultStream example code to close the client properly #2433

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Changes from 1 commit
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 @@ -138,6 +138,8 @@ private static class DataWriter {

private static final int MAX_RECREATE_COUNT = 3;

private BigQueryWriteClient client;

// 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();
Expand All @@ -163,12 +165,16 @@ public void initialize(TableName parentTable)
.setMaxRetryDelay(Duration.ofMinutes(1))
.build();

// Initialize client without settings, internally within stream writer a new client will be
// created with full settings.
client = BigQueryWriteClient.create();

// 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())
JsonStreamWriter.newBuilder(parentTable.toString(), client)
.setExecutorProvider(
FixedExecutorProvider.create(Executors.newScheduledThreadPool(100)))
.setChannelProvider(
Expand All @@ -195,7 +201,7 @@ public void append(AppendContext appendContext)
&& recreateCount.getAndIncrement() < MAX_RECREATE_COUNT) {
streamWriter =
JsonStreamWriter.newBuilder(
streamWriter.getStreamName(), BigQueryWriteClient.create())
streamWriter.getStreamName(), client)
.build();
this.error = null;
}
Expand All @@ -217,6 +223,7 @@ public void cleanup() {
// Wait for all in-flight requests to complete.
inflightRequestCount.arriveAndAwaitAdvance();

client.close();
// Close the connection to the server.
streamWriter.close();

Expand Down
Loading