-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spanner): add samples for instance partitions (#3221)
* feat(spanner): add samples for instance partitions * Lint * Lint
- Loading branch information
1 parent
d889195
commit bc48bf2
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
samples/snippets/src/main/java/com/example/spanner/CreateInstancePartitionSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_create_instance_partition] | ||
|
||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; | ||
import com.google.spanner.admin.instance.v1.CreateInstancePartitionRequest; | ||
import com.google.spanner.admin.instance.v1.InstanceConfigName; | ||
import com.google.spanner.admin.instance.v1.InstanceName; | ||
import com.google.spanner.admin.instance.v1.InstancePartition; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class CreateInstancePartitionSample { | ||
|
||
static void createInstancePartition() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String instancePartitionId = "my-instance-partition"; | ||
createInstancePartition(projectId, instanceId, instancePartitionId); | ||
} | ||
|
||
static void createInstancePartition( | ||
String projectId, String instanceId, String instancePartitionId) { | ||
// Set instance partition configuration. | ||
int nodeCount = 1; | ||
String displayName = "Descriptive name"; | ||
|
||
// Create an InstancePartition object that will be used to create the instance partition. | ||
InstancePartition instancePartition = | ||
InstancePartition.newBuilder() | ||
.setDisplayName(displayName) | ||
.setNodeCount(nodeCount) | ||
.setConfig(InstanceConfigName.of(projectId, "nam3").toString()) | ||
.build(); | ||
|
||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { | ||
|
||
// Wait for the createInstancePartition operation to finish. | ||
InstancePartition createdInstancePartition = | ||
instanceAdminClient | ||
.createInstancePartitionAsync( | ||
CreateInstancePartitionRequest.newBuilder() | ||
.setParent(InstanceName.of(projectId, instanceId).toString()) | ||
.setInstancePartitionId(instancePartitionId) | ||
.setInstancePartition(instancePartition) | ||
.build()) | ||
.get(); | ||
System.out.printf( | ||
"Instance partition %s was successfully created%n", createdInstancePartition.getName()); | ||
} catch (ExecutionException e) { | ||
System.out.printf( | ||
"Error: Creating instance partition %s failed with error message %s%n", | ||
instancePartition.getName(), e.getMessage()); | ||
} catch (InterruptedException e) { | ||
System.out.println( | ||
"Error: Waiting for createInstancePartition operation to finish was interrupted"); | ||
} | ||
} | ||
} | ||
// [END spanner_create_instance_partition] |
55 changes: 55 additions & 0 deletions
55
samples/snippets/src/test/java/com/example/spanner/CreateInstancePartitionSampleIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.spanner.InstanceAdminClient; | ||
import com.google.cloud.spanner.InstanceConfigId; | ||
import com.google.cloud.spanner.InstanceId; | ||
import com.google.cloud.spanner.InstanceInfo; | ||
import com.google.spanner.admin.instance.v1.InstancePartitionName; | ||
import org.junit.Test; | ||
|
||
public class CreateInstancePartitionSampleIT extends SampleTestBaseV2 { | ||
|
||
@Test | ||
public void testCreateInstancePartition() throws Exception { | ||
String instanceId = idGenerator.generateInstanceId(); | ||
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); | ||
instanceAdminClient | ||
.createInstance( | ||
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId)) | ||
.setDisplayName("Geo-partitioning test instance") | ||
.setInstanceConfigId(InstanceConfigId.of(projectId, "regional-us-central1")) | ||
.setNodeCount(1) | ||
.build()) | ||
.get(); | ||
|
||
String instancePartitionId = "my-instance-partition"; | ||
String out = | ||
SampleRunner.runSample( | ||
() -> | ||
CreateInstancePartitionSample.createInstancePartition( | ||
projectId, instanceId, instancePartitionId)); | ||
assertThat(out) | ||
.contains( | ||
String.format( | ||
"Instance partition %s", | ||
InstancePartitionName.of(projectId, instanceId, instancePartitionId).toString())); | ||
} | ||
} |