|
| 1 | +/* |
| 2 | + * Copyright 2023 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.spanner.admin.generated; |
| 18 | + |
| 19 | +//[START spanner_create_database_with_default_leader] |
| 20 | + |
| 21 | +import com.google.cloud.spanner.SpannerException; |
| 22 | +import com.google.cloud.spanner.SpannerExceptionFactory; |
| 23 | +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.spanner.admin.database.v1.CreateDatabaseRequest; |
| 26 | +import com.google.spanner.admin.database.v1.Database; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | + |
| 30 | +public class CreateDatabaseWithDefaultLeaderSample { |
| 31 | + |
| 32 | + static void createDatabaseWithDefaultLeader() throws IOException { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + final String instanceName = "projects/my-project/instances/my-instance-id"; |
| 35 | + final String databaseId = "my-database-name"; |
| 36 | + final String defaultLeader = "my-default-leader"; |
| 37 | + createDatabaseWithDefaultLeader(instanceName, databaseId, defaultLeader); |
| 38 | + } |
| 39 | + |
| 40 | + static void createDatabaseWithDefaultLeader(String instanceName, String databaseId, |
| 41 | + String defaultLeader) throws IOException { |
| 42 | + DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); |
| 43 | + |
| 44 | + try { |
| 45 | + Database createdDatabase = |
| 46 | + databaseAdminClient.createDatabaseAsync( |
| 47 | + CreateDatabaseRequest.newBuilder() |
| 48 | + .setParent(instanceName) |
| 49 | + .setCreateStatement("CREATE DATABASE `" + databaseId + "`") |
| 50 | + .addAllExtraStatements( |
| 51 | + ImmutableList.of("CREATE TABLE Singers (" |
| 52 | + + " SingerId INT64 NOT NULL," |
| 53 | + + " FirstName STRING(1024)," |
| 54 | + + " LastName STRING(1024)," |
| 55 | + + " SingerInfo BYTES(MAX)" |
| 56 | + + ") PRIMARY KEY (SingerId)", |
| 57 | + "CREATE TABLE Albums (" |
| 58 | + + " SingerId INT64 NOT NULL," |
| 59 | + + " AlbumId INT64 NOT NULL," |
| 60 | + + " AlbumTitle STRING(MAX)" |
| 61 | + + ") PRIMARY KEY (SingerId, AlbumId)," |
| 62 | + + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE", |
| 63 | + "ALTER DATABASE " + "`" + databaseId + "`" |
| 64 | + + " SET OPTIONS ( default_leader = '" + defaultLeader + "' )")) |
| 65 | + .build()).get(); |
| 66 | + System.out.println("Created database [" + createdDatabase.getName() + "]"); |
| 67 | + System.out.println("\tDefault leader: " + createdDatabase.getDefaultLeader()); |
| 68 | + } catch (ExecutionException e) { |
| 69 | + // If the operation failed during execution, expose the cause. |
| 70 | + throw (SpannerException) e.getCause(); |
| 71 | + } catch (InterruptedException e) { |
| 72 | + // Throw when a thread is waiting, sleeping, or otherwise occupied, |
| 73 | + // and the thread is interrupted, either before or during the activity. |
| 74 | + throw SpannerExceptionFactory.propagateInterrupt(e); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +//[END spanner_create_database_with_default_leader] |
0 commit comments