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: Sample should show sending multiple rows in one request #1335

Merged
merged 8 commits into from Oct 5, 2021
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