Skip to content

Commit

Permalink
fix: Sample should show sending multiple rows in one request (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
VeronicaWasson committed Oct 5, 2021
1 parent 5dfd523 commit 3f85a68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
Expand Up @@ -64,17 +64,19 @@ public static void writeCommittedStream(String projectId, String datasetName, St
try (JsonStreamWriter writer =
JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema())
.build()) {
// Append 10 JSON objects to the stream.
for (int i = 0; i < 10; i++) {
// Write two batches to the stream, each with 10 JSON records.
for (int i = 0; i < 2; i++) {
// Create a JSON object that is compatible with the table schema.
JSONObject record = new JSONObject();
record.put("col1", String.format("record %03d", i));
JSONArray jsonArr = new JSONArray();
jsonArr.put(record);
for (int j = 0; j < 10; j++) {
JSONObject record = new JSONObject();
record.put("col1", String.format("record %03d-%03d", i, j));
jsonArr.put(record);
}

// To detect duplicate records, pass the index as the record offset.
// To disable deduplication, omit the offset or use WriteStream.Type.DEFAULT.
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr, /*offset=*/ i);
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr, /*offset=*/ i * 10);
AppendRowsResponse response = future.get();
}
}
Expand Down
Expand Up @@ -67,14 +67,15 @@ public static void writePendingStream(String projectId, String datasetName, Stri
try (JsonStreamWriter writer =
JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema())
.build()) {
// Append 10 JSON objects to the stream.
for (int i = 0; i < 10; i++) {
// Write two batches to the stream, each with 10 JSON records.
for (int i = 0; i < 2; i++) {
// Create a JSON object that is compatible with the table schema.
JSONObject record = new JSONObject();
record.put("col1", String.format("batch-record %03d", i));
JSONArray jsonArr = new JSONArray();
jsonArr.put(record);

for (int j = 0; j < 10; j++) {
JSONObject record = new JSONObject();
record.put("col1", String.format("batch-record %03d-%03d", i, j));
jsonArr.put(record);
}
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
AppendRowsResponse response = future.get();
}
Expand Down
Expand Up @@ -56,14 +56,15 @@ public static void writeToDefaultStream(String projectId, String datasetName, St
// https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1beta2/JsonStreamWriter.html
try (JsonStreamWriter writer =
JsonStreamWriter.newBuilder(parentTable.toString(), tableSchema).build()) {
// Append 10 JSON objects to the stream.
for (int i = 0; i < 10; i++) {
// Write two batches to the stream, each with 10 JSON records.
for (int i = 0; i < 2; i++) {
// Create a JSON object that is compatible with the table schema.
JSONObject record = new JSONObject();
record.put("test_string", String.format("record %03d", i));
JSONArray jsonArr = new JSONArray();
jsonArr.put(record);

for (int j = 0; j < 10; j++) {
JSONObject record = new JSONObject();
record.put("test_string", String.format("record %03d-%03d", i, j));
jsonArr.put(record);
}
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
AppendRowsResponse response = future.get();
}
Expand Down

0 comments on commit 3f85a68

Please sign in to comment.