diff --git a/samples/snippets/src/main/java/com/example/bigquerystorage/WriteCommittedStream.java b/samples/snippets/src/main/java/com/example/bigquerystorage/WriteCommittedStream.java index ea1ac9ccd1..63ff782924 100644 --- a/samples/snippets/src/main/java/com/example/bigquerystorage/WriteCommittedStream.java +++ b/samples/snippets/src/main/java/com/example/bigquerystorage/WriteCommittedStream.java @@ -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 future = writer.append(jsonArr, /*offset=*/ i); + ApiFuture future = writer.append(jsonArr, /*offset=*/ i * 10); AppendRowsResponse response = future.get(); } } diff --git a/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java b/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java index fc7b4c1af5..386f08a5d0 100644 --- a/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java +++ b/samples/snippets/src/main/java/com/example/bigquerystorage/WritePendingStream.java @@ -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 future = writer.append(jsonArr); AppendRowsResponse response = future.get(); } diff --git a/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java b/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java index 2241c91bba..a06b113b3b 100644 --- a/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java +++ b/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java @@ -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 future = writer.append(jsonArr); AppendRowsResponse response = future.get(); }