Skip to content

Commit ffc190c

Browse files
authored
samples: updates lci samples (#1266)
Updates the LCI samples to be accordingly to the spec. Instead of printing out the processing units directly from the instance that is created, another getInstance is done in order to retrieve the information.
1 parent 2ec8315 commit ffc190c

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

samples/snippets/src/main/java/com/example/spanner/CreateInstanceWithProcessingUnitsExample.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.spanner;
1818

1919
//[START spanner_create_instance_with_processing_units]
20+
2021
import com.google.api.gax.longrunning.OperationFuture;
2122
import com.google.cloud.spanner.Instance;
2223
import com.google.cloud.spanner.InstanceAdminClient;
@@ -26,7 +27,6 @@
2627
import com.google.cloud.spanner.Spanner;
2728
import com.google.cloud.spanner.SpannerOptions;
2829
import com.google.spanner.admin.instance.v1.CreateInstanceMetadata;
29-
import java.util.concurrent.ExecutionException;
3030

3131
class 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

Comments
 (0)