1717package com .example .spanner ;
1818
1919//[START spanner_create_instance_with_processing_units]
20+
2021import com .google .api .gax .longrunning .OperationFuture ;
2122import com .google .cloud .spanner .Instance ;
2223import com .google .cloud .spanner .InstanceAdminClient ;
2627import com .google .cloud .spanner .Spanner ;
2728import com .google .cloud .spanner .SpannerOptions ;
2829import com .google .spanner .admin .instance .v1 .CreateInstanceMetadata ;
29- import java .util .concurrent .ExecutionException ;
3030
3131class CreateInstanceWithProcessingUnitsExample {
3232
@@ -47,26 +47,28 @@ static void createInstance(String projectId, String instanceId) {
4747 int processingUnits = 500 ;
4848 String displayName = "Descriptive name" ;
4949
50- // Create an InstanceInfo object that will be used to create the instance.
51- InstanceInfo instanceInfo =
52- InstanceInfo .newBuilder (InstanceId .of (projectId , instanceId ))
53- .setInstanceConfigId (InstanceConfigId .of (projectId , configId ))
54- .setProcessingUnits (processingUnits )
55- .setDisplayName (displayName )
56- .build ();
57- OperationFuture <Instance , CreateInstanceMetadata > operation =
58- instanceAdminClient .createInstance (instanceInfo );
5950 try {
51+ // Creates a new instance
52+ System .out .printf ("Creating instance %s.%n" , instanceId );
53+ OperationFuture <Instance , CreateInstanceMetadata > operation =
54+ instanceAdminClient .createInstance (InstanceInfo
55+ .newBuilder (InstanceId .of (projectId , instanceId ))
56+ .setInstanceConfigId (InstanceConfigId .of (projectId , configId ))
57+ .setProcessingUnits (processingUnits )
58+ .setDisplayName (displayName )
59+ .build ());
60+
6061 // Wait for the createInstance operation to finish.
61- Instance instance = operation .get ();
62- System .out .printf ("Instance %s was successfully created with %d processing units%n" ,
63- instance .getId (), instance .getProcessingUnits ());
64- } catch (ExecutionException e ) {
65- System .out .printf (
66- "Error: Creating instance %s failed with error message %s%n" ,
67- instanceInfo .getId (), e .getMessage ());
68- } catch (InterruptedException e ) {
69- System .out .println ("Error: Waiting for createInstance operation to finish was interrupted" );
62+ System .out .printf ("Waiting for operation on %s to complete...%n" , instanceId );
63+ Instance createdInstance = operation .get ();
64+
65+ System .out .printf ("Created instance %s.%n" , createdInstance .getId ().getInstance ());
66+
67+ Instance instance = instanceAdminClient .getInstance (instanceId );
68+ System .out .printf ("Instance %s has %d processing units.%n" , instance .getId ().getInstance (),
69+ instance .getProcessingUnits ());
70+ } catch (Exception e ) {
71+ System .out .printf ("Error: %s.%n" , e .getMessage ());
7072 }
7173 spanner .close ();
7274 }
0 commit comments