|
| 1 | +/* |
| 2 | + * Copyright 2024 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; |
| 18 | + |
| 19 | +// [START spanner_create_instance_partition] |
| 20 | + |
| 21 | +import com.google.cloud.spanner.Spanner; |
| 22 | +import com.google.cloud.spanner.SpannerOptions; |
| 23 | +import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; |
| 24 | +import com.google.spanner.admin.instance.v1.CreateInstancePartitionRequest; |
| 25 | +import com.google.spanner.admin.instance.v1.InstanceConfigName; |
| 26 | +import com.google.spanner.admin.instance.v1.InstanceName; |
| 27 | +import com.google.spanner.admin.instance.v1.InstancePartition; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | + |
| 30 | +class CreateInstancePartitionSample { |
| 31 | + |
| 32 | + static void createInstancePartition() { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + String projectId = "my-project"; |
| 35 | + String instanceId = "my-instance"; |
| 36 | + String instancePartitionId = "my-instance-partition"; |
| 37 | + createInstancePartition(projectId, instanceId, instancePartitionId); |
| 38 | + } |
| 39 | + |
| 40 | + static void createInstancePartition( |
| 41 | + String projectId, String instanceId, String instancePartitionId) { |
| 42 | + // Set instance partition configuration. |
| 43 | + int nodeCount = 1; |
| 44 | + String displayName = "Descriptive name"; |
| 45 | + |
| 46 | + // Create an InstancePartition object that will be used to create the instance partition. |
| 47 | + InstancePartition instancePartition = |
| 48 | + InstancePartition.newBuilder() |
| 49 | + .setDisplayName(displayName) |
| 50 | + .setNodeCount(nodeCount) |
| 51 | + .setConfig(InstanceConfigName.of(projectId, "nam3").toString()) |
| 52 | + .build(); |
| 53 | + |
| 54 | + try (Spanner spanner = |
| 55 | + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); |
| 56 | + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { |
| 57 | + |
| 58 | + // Wait for the createInstancePartition operation to finish. |
| 59 | + InstancePartition createdInstancePartition = |
| 60 | + instanceAdminClient |
| 61 | + .createInstancePartitionAsync( |
| 62 | + CreateInstancePartitionRequest.newBuilder() |
| 63 | + .setParent(InstanceName.of(projectId, instanceId).toString()) |
| 64 | + .setInstancePartitionId(instancePartitionId) |
| 65 | + .setInstancePartition(instancePartition) |
| 66 | + .build()) |
| 67 | + .get(); |
| 68 | + System.out.printf( |
| 69 | + "Instance partition %s was successfully created%n", createdInstancePartition.getName()); |
| 70 | + } catch (ExecutionException e) { |
| 71 | + System.out.printf( |
| 72 | + "Error: Creating instance partition %s failed with error message %s%n", |
| 73 | + instancePartition.getName(), e.getMessage()); |
| 74 | + } catch (InterruptedException e) { |
| 75 | + System.out.println( |
| 76 | + "Error: Waiting for createInstancePartition operation to finish was interrupted"); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +// [END spanner_create_instance_partition] |
0 commit comments