|
| 1 | +/* |
| 2 | + * Copyright 2024 Google Inc. |
| 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.spanner; |
| 18 | + |
| 19 | +// [START spanner_add_proto_type_columns] |
| 20 | + |
| 21 | +import com.google.cloud.spanner.Spanner; |
| 22 | +import com.google.cloud.spanner.SpannerOptions; |
| 23 | +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.protobuf.ByteString; |
| 26 | +import com.google.spanner.admin.database.v1.DatabaseName; |
| 27 | +import com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.util.concurrent.ExecutionException; |
| 31 | + |
| 32 | +class AddProtoColumnSample { |
| 33 | + |
| 34 | + static void addProtoColumn() throws InterruptedException, ExecutionException, IOException { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + String projectId = "my-project"; |
| 37 | + String instanceId = "my-instance"; |
| 38 | + String databaseId = "my-database"; |
| 39 | + |
| 40 | + addProtoColumn(projectId, instanceId, databaseId); |
| 41 | + } |
| 42 | + |
| 43 | + static void addProtoColumn(String projectId, String instanceId, String databaseId) |
| 44 | + throws InterruptedException, ExecutionException, IOException { |
| 45 | + InputStream in = |
| 46 | + AddProtoColumnSample.class |
| 47 | + .getClassLoader() |
| 48 | + .getResourceAsStream("com/example/spanner/descriptors.pb"); |
| 49 | + try (Spanner spanner = |
| 50 | + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); |
| 51 | + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { |
| 52 | + UpdateDatabaseDdlRequest request = |
| 53 | + UpdateDatabaseDdlRequest.newBuilder() |
| 54 | + .setDatabase(DatabaseName.of(projectId, instanceId, databaseId).toString()) |
| 55 | + .addAllStatements( |
| 56 | + ImmutableList.of( |
| 57 | + "CREATE PROTO BUNDLE (" |
| 58 | + + "examples.spanner.music.SingerInfo," |
| 59 | + + "examples.spanner.music.Genre," |
| 60 | + + ")", |
| 61 | + "ALTER TABLE Singers ADD COLUMN SingerInfo examples.spanner.music.SingerInfo", |
| 62 | + "ALTER TABLE Singers ADD COLUMN SingerInfoArray ARRAY<examples.spanner.music.SingerInfo>", |
| 63 | + "ALTER TABLE Singers ADD COLUMN SingerGenre examples.spanner.music.Genre", |
| 64 | + "ALTER TABLE Singers ADD COLUMN SingerGenreArray ARRAY<examples.spanner.music.Genre>")) |
| 65 | + .setProtoDescriptors(ByteString.readFrom(in)) |
| 66 | + .build(); |
| 67 | + // Wait for the operation to finish. |
| 68 | + // This will throw an ExecutionException if the operation fails. |
| 69 | + databaseAdminClient.updateDatabaseDdlAsync(request).get(); |
| 70 | + System.out.printf("Added Proto columns %n"); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +// [END spanner_add_proto_type_columns] |
0 commit comments