Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner): add samples for instance partitions #3221

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
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()));
}
}
Loading