Search in sources :

Example 86 with Instance

use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.

the class CreateInstanceExample method createInstance.

static void createInstance(String projectId, String instanceId) {
    Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
    InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
    // Set Instance configuration.
    String configId = "regional-us-central1";
    int nodeCount = 2;
    String displayName = "Descriptive name";
    // Create an InstanceInfo object that will be used to create the instance.
    InstanceInfo instanceInfo = InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId)).setInstanceConfigId(InstanceConfigId.of(projectId, configId)).setNodeCount(nodeCount).setDisplayName(displayName).build();
    OperationFuture<Instance, CreateInstanceMetadata> operation = instanceAdminClient.createInstance(instanceInfo);
    try {
        // Wait for the createInstance operation to finish.
        Instance instance = operation.get();
        System.out.printf("Instance %s was successfully created%n", instance.getId());
    } catch (ExecutionException e) {
        System.out.printf("Error: Creating instance %s failed with error message %s%n", instanceInfo.getId(), e.getMessage());
    } catch (InterruptedException e) {
        System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
    } finally {
        spanner.close();
    }
}
Also used : Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) ExecutionException(java.util.concurrent.ExecutionException) InstanceInfo(com.google.cloud.spanner.InstanceInfo) Spanner(com.google.cloud.spanner.Spanner)

Aggregations

Test (org.junit.Test)50 AbstractMessage (com.google.protobuf.AbstractMessage)19 ByteString (com.google.protobuf.ByteString)17 ExecutionException (java.util.concurrent.ExecutionException)16 Instance (com.google.cloud.spanner.Instance)15 StatusRuntimeException (io.grpc.StatusRuntimeException)14 Instance (com.google.cloud.compute.v1.Instance)11 InstancesClient (com.google.cloud.compute.v1.InstancesClient)11 CreateInstanceMetadata (com.google.spanner.admin.instance.v1.CreateInstanceMetadata)11 Instance (com.google.spanner.admin.instance.v1.Instance)11 Instance (com.google.bigtable.admin.v2.Instance)10 InstanceAdminClient (com.google.cloud.spanner.InstanceAdminClient)10 InstanceInfo (com.google.cloud.spanner.InstanceInfo)10 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)9 Instance (com.google.cloud.notebooks.v1beta1.Instance)9 Spanner (com.google.cloud.spanner.Spanner)8 ArrayList (java.util.ArrayList)8 Operation (com.google.cloud.compute.v1.Operation)7 FieldMask (com.google.protobuf.FieldMask)7 InstanceName (com.google.spanner.admin.instance.v1.InstanceName)7