Skip to content

Commit 1121364

Browse files
samples: adds LCI samples (#1251)
* feat: add support for instance processing units * docs: add sample for instance processing units * fix: remove commented code + use junit asserts * fix: remove Spanner snapshot reference * test: add tests for InstanceInfo * cleanup: fix formatting + tests * chore: fix formatting * test: fixes instanceinfo test Due to master merge the compilation was failing. Fixes the builder construction here. * refactor: adds default impl for processing units Adds default implementation for the InstanceInfo set processing units in order to avoid a breaking change. * samples: remove LCI samples for now Removes the LCI samples for now, because they won't compile. We will re-add them once the main implementation is released. * Revert "samples: remove LCI samples for now" This reverts commit dc17892. * samples: fixes processing units samples for lci Co-authored-by: Olav Loite <[email protected]>
1 parent 1fa95a9 commit 1121364

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2021 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_with_processing_units]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.spanner.Instance;
22+
import com.google.cloud.spanner.InstanceAdminClient;
23+
import com.google.cloud.spanner.InstanceConfigId;
24+
import com.google.cloud.spanner.InstanceId;
25+
import com.google.cloud.spanner.InstanceInfo;
26+
import com.google.cloud.spanner.Spanner;
27+
import com.google.cloud.spanner.SpannerOptions;
28+
import com.google.spanner.admin.instance.v1.CreateInstanceMetadata;
29+
import java.util.concurrent.ExecutionException;
30+
31+
class CreateInstanceWithProcessingUnitsExample {
32+
33+
static void createInstance() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "my-project";
36+
String instanceId = "my-instance";
37+
createInstance(projectId, instanceId);
38+
}
39+
40+
static void createInstance(String projectId, String instanceId) {
41+
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
42+
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
43+
44+
// Set Instance configuration.
45+
String configId = "regional-us-central1";
46+
// This will create an instance with the processing power of 0.2 nodes.
47+
int processingUnits = 500;
48+
String displayName = "Descriptive name";
49+
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);
59+
try {
60+
// 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");
70+
}
71+
spanner.close();
72+
}
73+
}
74+
//[END spanner_create_instance_with_processing_units]

0 commit comments

Comments
 (0)