|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.bigquerystorage; |
| 18 | + |
| 19 | +// [START bigquerystorage_jsonstreamwriter_buffered] |
| 20 | +import com.google.api.core.ApiFuture; |
| 21 | +import com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse; |
| 22 | +import com.google.cloud.bigquery.storage.v1beta2.BigQueryWriteClient; |
| 23 | +import com.google.cloud.bigquery.storage.v1beta2.CreateWriteStreamRequest; |
| 24 | +import com.google.cloud.bigquery.storage.v1beta2.FlushRowsRequest; |
| 25 | +import com.google.cloud.bigquery.storage.v1beta2.FlushRowsResponse; |
| 26 | +import com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter; |
| 27 | +import com.google.cloud.bigquery.storage.v1beta2.TableName; |
| 28 | +import com.google.cloud.bigquery.storage.v1beta2.WriteStream; |
| 29 | +import com.google.protobuf.Descriptors.DescriptorValidationException; |
| 30 | +import com.google.protobuf.Int64Value; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | +import org.json.JSONArray; |
| 34 | +import org.json.JSONObject; |
| 35 | + |
| 36 | +public class WriteBufferedStream { |
| 37 | + |
| 38 | + public static void runWriteBufferedStream() |
| 39 | + throws DescriptorValidationException, InterruptedException, IOException { |
| 40 | + // TODO(developer): Replace these variables before running the sample. |
| 41 | + String projectId = "MY_PROJECT_ID"; |
| 42 | + String datasetName = "MY_DATASET_NAME"; |
| 43 | + String tableName = "MY_TABLE_NAME"; |
| 44 | + |
| 45 | + writeBufferedStream(projectId, datasetName, tableName); |
| 46 | + } |
| 47 | + |
| 48 | + public static void writeBufferedStream(String projectId, String datasetName, String tableName) |
| 49 | + throws DescriptorValidationException, InterruptedException, IOException { |
| 50 | + try (BigQueryWriteClient client = BigQueryWriteClient.create()) { |
| 51 | + // Initialize a write stream for the specified table. |
| 52 | + // For more information on WriteStream.Type, see: |
| 53 | + // https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1beta2/WriteStream.Type.html |
| 54 | + WriteStream stream = WriteStream.newBuilder().setType(WriteStream.Type.BUFFERED).build(); |
| 55 | + TableName parentTable = TableName.of(projectId, datasetName, tableName); |
| 56 | + CreateWriteStreamRequest createWriteStreamRequest = |
| 57 | + CreateWriteStreamRequest.newBuilder() |
| 58 | + .setParent(parentTable.toString()) |
| 59 | + .setWriteStream(stream) |
| 60 | + .build(); |
| 61 | + WriteStream writeStream = client.createWriteStream(createWriteStreamRequest); |
| 62 | + |
| 63 | + // Use the JSON stream writer to send records in JSON format. |
| 64 | + // For more information about JsonStreamWriter, see: |
| 65 | + // https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1beta2/JsonStreamWriter.html |
| 66 | + try (JsonStreamWriter writer = |
| 67 | + JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema()) |
| 68 | + .build()) { |
| 69 | + // Write two batches to the stream, each with 10 JSON records. |
| 70 | + for (int i = 0; i < 2; i++) { |
| 71 | + JSONArray jsonArr = new JSONArray(); |
| 72 | + for (int j = 0; j < 10; j++) { |
| 73 | + // Create a JSON object that is compatible with the table schema. |
| 74 | + JSONObject record = new JSONObject(); |
| 75 | + record.put("col1", String.format("buffered-record %03d", i)); |
| 76 | + jsonArr.put(record); |
| 77 | + } |
| 78 | + ApiFuture<AppendRowsResponse> future = writer.append(jsonArr); |
| 79 | + AppendRowsResponse response = future.get(); |
| 80 | + } |
| 81 | + |
| 82 | + // Flush the buffer. |
| 83 | + FlushRowsRequest flushRowsRequest = |
| 84 | + FlushRowsRequest.newBuilder() |
| 85 | + .setWriteStream(writeStream.getName()) |
| 86 | + .setOffset(Int64Value.of(10 * 2 - 1)) // Advance the cursor to the latest record. |
| 87 | + .build(); |
| 88 | + FlushRowsResponse flushRowsResponse = client.flushRows(flushRowsRequest); |
| 89 | + // You can continue to write to the stream after flushing the buffer. |
| 90 | + } |
| 91 | + System.out.println("Appended and committed records successfully."); |
| 92 | + } catch (ExecutionException e) { |
| 93 | + // If the wrapped exception is a StatusRuntimeException, check the state of the operation. |
| 94 | + // If the state is INTERNAL, CANCELLED, or ABORTED, you can retry. For more information, see: |
| 95 | + // https://grpc.github.io/grpc-java/javadoc/io/grpc/StatusRuntimeException.html |
| 96 | + System.out.println(e); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +// [END bigquerystorage_jsonstreamwriter_buffered] |
0 commit comments