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

Commit 3f85a68

Browse files
fix: Sample should show sending multiple rows in one request (#1335)
1 parent 5dfd523 commit 3f85a68

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ public static void writeCommittedStream(String projectId, String datasetName, St
6464
try (JsonStreamWriter writer =
6565
JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema())
6666
.build()) {
67-
// Append 10 JSON objects to the stream.
68-
for (int i = 0; i < 10; i++) {
67+
// Write two batches to the stream, each with 10 JSON records.
68+
for (int i = 0; i < 2; i++) {
6969
// Create a JSON object that is compatible with the table schema.
70-
JSONObject record = new JSONObject();
71-
record.put("col1", String.format("record %03d", i));
7270
JSONArray jsonArr = new JSONArray();
73-
jsonArr.put(record);
71+
for (int j = 0; j < 10; j++) {
72+
JSONObject record = new JSONObject();
73+
record.put("col1", String.format("record %03d-%03d", i, j));
74+
jsonArr.put(record);
75+
}
7476

7577
// To detect duplicate records, pass the index as the record offset.
7678
// To disable deduplication, omit the offset or use WriteStream.Type.DEFAULT.
77-
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr, /*offset=*/ i);
79+
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr, /*offset=*/ i * 10);
7880
AppendRowsResponse response = future.get();
7981
}
8082
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ public static void writePendingStream(String projectId, String datasetName, Stri
6767
try (JsonStreamWriter writer =
6868
JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema())
6969
.build()) {
70-
// Append 10 JSON objects to the stream.
71-
for (int i = 0; i < 10; i++) {
70+
// Write two batches to the stream, each with 10 JSON records.
71+
for (int i = 0; i < 2; i++) {
7272
// Create a JSON object that is compatible with the table schema.
73-
JSONObject record = new JSONObject();
74-
record.put("col1", String.format("batch-record %03d", i));
7573
JSONArray jsonArr = new JSONArray();
76-
jsonArr.put(record);
77-
74+
for (int j = 0; j < 10; j++) {
75+
JSONObject record = new JSONObject();
76+
record.put("col1", String.format("batch-record %03d-%03d", i, j));
77+
jsonArr.put(record);
78+
}
7879
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
7980
AppendRowsResponse response = future.get();
8081
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ 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-
// Append 10 JSON objects to the stream.
60-
for (int i = 0; i < 10; i++) {
59+
// Write two batches to the stream, each with 10 JSON records.
60+
for (int i = 0; i < 2; i++) {
6161
// Create a JSON object that is compatible with the table schema.
62-
JSONObject record = new JSONObject();
63-
record.put("test_string", String.format("record %03d", i));
6462
JSONArray jsonArr = new JSONArray();
65-
jsonArr.put(record);
66-
63+
for (int j = 0; j < 10; j++) {
64+
JSONObject record = new JSONObject();
65+
record.put("test_string", String.format("record %03d-%03d", i, j));
66+
jsonArr.put(record);
67+
}
6768
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
6869
AppendRowsResponse response = future.get();
6970
}

0 commit comments

Comments
 (0)