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

Commit 47e3654

Browse files
yirutanggcf-owl-bot[bot]stephaniewang526
authored
docs(samples): add finalize call to our samples (#1471)
* fix:Add finalize call to our samples * . * . * . * . * . * . * . * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Stephanie Wang <[email protected]>
1 parent e038151 commit 47e3654

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ If you are using Maven without BOM, add this to your dependencies:
4949
If you are using Gradle 5.x or later, add this to your dependencies
5050

5151
```Groovy
52-
implementation platform('com.google.cloud:libraries-bom:24.1.2')
52+
implementation platform('com.google.cloud:libraries-bom:24.2.0')
5353
5454
implementation 'com.google.cloud:google-cloud-bigquerystorage'
5555
```

samples/snippets/src/main/java/com/example/bigquerystorage/WriteBufferedStream.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.bigquery.storage.v1.AppendRowsResponse;
2222
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
2323
import com.google.cloud.bigquery.storage.v1.CreateWriteStreamRequest;
24+
import com.google.cloud.bigquery.storage.v1.FinalizeWriteStreamRequest;
2425
import com.google.cloud.bigquery.storage.v1.FlushRowsRequest;
2526
import com.google.cloud.bigquery.storage.v1.FlushRowsResponse;
2627
import com.google.cloud.bigquery.storage.v1.JsonStreamWriter;
@@ -78,7 +79,6 @@ public static void writeBufferedStream(String projectId, String datasetName, Str
7879
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
7980
AppendRowsResponse response = future.get();
8081
}
81-
8282
// Flush the buffer.
8383
FlushRowsRequest flushRowsRequest =
8484
FlushRowsRequest.newBuilder()
@@ -88,6 +88,10 @@ public static void writeBufferedStream(String projectId, String datasetName, Str
8888
FlushRowsResponse flushRowsResponse = client.flushRows(flushRowsRequest);
8989
// You can continue to write to the stream after flushing the buffer.
9090
}
91+
// Finalize the stream after use.
92+
FinalizeWriteStreamRequest finalizeWriteStreamRequest =
93+
FinalizeWriteStreamRequest.newBuilder().setName(writeStream.getName()).build();
94+
client.finalizeWriteStream(finalizeWriteStreamRequest);
9195
System.out.println("Appended and committed records successfully.");
9296
} catch (ExecutionException e) {
9397
// If the wrapped exception is a StatusRuntimeException, check the state of the operation.

samples/snippets/src/main/java/com/example/bigquerystorage/WriteCommittedStream.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.bigquery.storage.v1.AppendRowsResponse;
2222
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
2323
import com.google.cloud.bigquery.storage.v1.CreateWriteStreamRequest;
24+
import com.google.cloud.bigquery.storage.v1.FinalizeWriteStreamRequest;
2425
import com.google.cloud.bigquery.storage.v1.JsonStreamWriter;
2526
import com.google.cloud.bigquery.storage.v1.TableName;
2627
import com.google.cloud.bigquery.storage.v1.WriteStream;
@@ -64,7 +65,9 @@ public static void writeCommittedStream(String projectId, String datasetName, St
6465
try (JsonStreamWriter writer =
6566
JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema())
6667
.build()) {
67-
// Write two batches to the stream, each with 10 JSON records.
68+
// Write two batches to the stream, each with 10 JSON records. A writer should be
69+
// used for as much writes as possible. Creating a writer for just one write is an
70+
// antipattern.
6871
for (int i = 0; i < 2; i++) {
6972
// Create a JSON object that is compatible with the table schema.
7073
JSONArray jsonArr = new JSONArray();
@@ -79,6 +82,10 @@ public static void writeCommittedStream(String projectId, String datasetName, St
7982
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr, /*offset=*/ i * 10);
8083
AppendRowsResponse response = future.get();
8184
}
85+
// Finalize the stream after use.
86+
FinalizeWriteStreamRequest finalizeWriteStreamRequest =
87+
FinalizeWriteStreamRequest.newBuilder().setName(writeStream.getName()).build();
88+
client.finalizeWriteStream(finalizeWriteStreamRequest);
8289
}
8390
System.out.println("Appended records successfully.");
8491
} catch (ExecutionException e) {

samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static void writeToDefaultStream(String projectId, String datasetName, St
5656
// https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1beta2/JsonStreamWriter.html
5757
try (JsonStreamWriter writer =
5858
JsonStreamWriter.newBuilder(parentTable.toString(), tableSchema).build()) {
59-
// Write two batches to the stream, each with 10 JSON records.
59+
// Write two batches to the stream, each with 10 JSON records. A writer should be used for as
60+
// much writes as possible. Creating a writer for just one write is an antipattern.
6061
for (int i = 0; i < 2; i++) {
6162
// Create a JSON object that is compatible with the table schema.
6263
JSONArray jsonArr = new JSONArray();

0 commit comments

Comments
 (0)