Skip to content

Commit bc48bf2

Browse files
authored
feat(spanner): add samples for instance partitions (#3221)
* feat(spanner): add samples for instance partitions * Lint * Lint
1 parent d889195 commit bc48bf2

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.spanner.InstanceAdminClient;
22+
import com.google.cloud.spanner.InstanceConfigId;
23+
import com.google.cloud.spanner.InstanceId;
24+
import com.google.cloud.spanner.InstanceInfo;
25+
import com.google.spanner.admin.instance.v1.InstancePartitionName;
26+
import org.junit.Test;
27+
28+
public class CreateInstancePartitionSampleIT extends SampleTestBaseV2 {
29+
30+
@Test
31+
public void testCreateInstancePartition() throws Exception {
32+
String instanceId = idGenerator.generateInstanceId();
33+
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
34+
instanceAdminClient
35+
.createInstance(
36+
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId))
37+
.setDisplayName("Geo-partitioning test instance")
38+
.setInstanceConfigId(InstanceConfigId.of(projectId, "regional-us-central1"))
39+
.setNodeCount(1)
40+
.build())
41+
.get();
42+
43+
String instancePartitionId = "my-instance-partition";
44+
String out =
45+
SampleRunner.runSample(
46+
() ->
47+
CreateInstancePartitionSample.createInstancePartition(
48+
projectId, instanceId, instancePartitionId));
49+
assertThat(out)
50+
.contains(
51+
String.format(
52+
"Instance partition %s",
53+
InstancePartitionName.of(projectId, instanceId, instancePartitionId).toString()));
54+
}
55+
}

0 commit comments

Comments
 (0)