|
| 1 | +/* |
| 2 | + * Copyright 2025 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_writenestedproto] |
| 20 | +import com.google.api.core.ApiFuture; |
| 21 | +import com.google.cloud.bigquery.storage.v1.AppendRowsResponse; |
| 22 | +import com.google.cloud.bigquery.storage.v1.ProtoRows; |
| 23 | +import com.google.cloud.bigquery.storage.v1.ProtoSchemaConverter; |
| 24 | +import com.google.cloud.bigquery.storage.v1.StreamWriter; |
| 25 | +import com.google.protobuf.Descriptors.DescriptorValidationException; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | + |
| 29 | +public class WriteNestedProto { |
| 30 | + |
| 31 | + public static void runWriteNestedProto(String projectId, String datasetName, String tableName) |
| 32 | + throws DescriptorValidationException, InterruptedException, IOException { |
| 33 | + StreamWriter streamWriter = |
| 34 | + StreamWriter.newBuilder( |
| 35 | + "projects/" |
| 36 | + + projectId |
| 37 | + + "/datasets/" |
| 38 | + + datasetName |
| 39 | + + "/tables/" |
| 40 | + + tableName |
| 41 | + + "/_default") |
| 42 | + .setWriterSchema(ProtoSchemaConverter.convert(HasNestedMessage.getDescriptor())) |
| 43 | + .build(); |
| 44 | + ProtoRows protoRows = |
| 45 | + ProtoRows.newBuilder() |
| 46 | + .addSerializedRows( |
| 47 | + HasNestedMessage.newBuilder() |
| 48 | + .setFoo("foo") |
| 49 | + .setBar( |
| 50 | + HasNestedMessage.InnerMessage.newBuilder() |
| 51 | + .setMyInt(12345) |
| 52 | + .setMyString("bar") |
| 53 | + .build()) |
| 54 | + .build() |
| 55 | + .toByteString()) |
| 56 | + .addSerializedRows( |
| 57 | + HasSeparateNestedMessage.newBuilder() |
| 58 | + .setFoo("foo2") |
| 59 | + .setBar( |
| 60 | + SeparateMessage.newBuilder().setMyInt(123456).setMyString("bar2").build()) |
| 61 | + .build() |
| 62 | + .toByteString()) |
| 63 | + .build(); |
| 64 | + ApiFuture<AppendRowsResponse> future = streamWriter.append(protoRows); |
| 65 | + try { |
| 66 | + AppendRowsResponse response = future.get(); |
| 67 | + System.out.println("Appended records successfully."); |
| 68 | + } catch (ExecutionException e) { |
| 69 | + System.out.println(e); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +// [END bigquerystorage_writenestedproto] |
0 commit comments