|
| 1 | +/* |
| 2 | + * Copyright 2025 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_database_add_split_points] |
| 20 | + |
| 21 | +import com.google.cloud.spanner.Spanner; |
| 22 | +import com.google.cloud.spanner.SpannerException; |
| 23 | +import com.google.cloud.spanner.SpannerOptions; |
| 24 | +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
| 25 | +import com.google.protobuf.ListValue; |
| 26 | +import com.google.protobuf.Value; |
| 27 | +import com.google.spanner.admin.database.v1.DatabaseName; |
| 28 | +import com.google.spanner.admin.database.v1.SplitPoints; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class DatabaseAddSplitPointsSample { |
| 34 | + |
| 35 | + /*** |
| 36 | + * Assume DDL for the underlying database: |
| 37 | + * <pre>{@code |
| 38 | + * CREATE TABLE Singers ( |
| 39 | + * SingerId INT64 NOT NULL, |
| 40 | + * FirstName STRING(1024), |
| 41 | + * LastName STRING(1024), |
| 42 | + * SingerInfo BYTES(MAX), |
| 43 | + * ) PRIMARY KEY(SingerId); |
| 44 | + * |
| 45 | + * |
| 46 | + * CREATE INDEX SingersByFirstLastName ON Singers(FirstName, LastName); |
| 47 | + * }</pre> |
| 48 | + */ |
| 49 | + |
| 50 | + static void addSplitPoints() throws IOException { |
| 51 | + // TODO(developer): Replace these variables before running the sample. |
| 52 | + String projectId = "my-project"; |
| 53 | + String instanceId = "my-instance"; |
| 54 | + String databaseId = "my-database"; |
| 55 | + addSplitPoints(projectId, instanceId, databaseId); |
| 56 | + } |
| 57 | + |
| 58 | + static void addSplitPoints(String projectId, String instanceId, String databaseId) |
| 59 | + throws IOException { |
| 60 | + try (Spanner spanner = |
| 61 | + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); |
| 62 | + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { |
| 63 | + List<com.google.spanner.admin.database.v1.SplitPoints> splitPoints = new ArrayList<>(); |
| 64 | + |
| 65 | + // table key |
| 66 | + com.google.spanner.admin.database.v1.SplitPoints splitPointForTable = |
| 67 | + SplitPoints.newBuilder() |
| 68 | + .setTable("Singers") |
| 69 | + .setKeys( |
| 70 | + 0, |
| 71 | + com.google.spanner.admin.database.v1.SplitPoints.Key.newBuilder() |
| 72 | + .setKeyParts( |
| 73 | + ListValue.newBuilder() |
| 74 | + .addValues(Value.newBuilder().setStringValue("42").build()) |
| 75 | + .build())) |
| 76 | + .build(); |
| 77 | + |
| 78 | + // index key without table key part |
| 79 | + com.google.spanner.admin.database.v1.SplitPoints splitPointForIndex = |
| 80 | + SplitPoints.newBuilder() |
| 81 | + .setIndex("SingersByFirstLastName") |
| 82 | + .setKeys( |
| 83 | + 0, |
| 84 | + com.google.spanner.admin.database.v1.SplitPoints.Key.newBuilder() |
| 85 | + .setKeyParts( |
| 86 | + ListValue.newBuilder() |
| 87 | + .addValues(Value.newBuilder().setStringValue("John").build()) |
| 88 | + .addValues(Value.newBuilder().setStringValue("Doe").build()) |
| 89 | + .build())) |
| 90 | + .build(); |
| 91 | + |
| 92 | + // index key with table key part, first key is the index key and second is the table key |
| 93 | + com.google.spanner.admin.database.v1.SplitPoints splitPointForIndexWitTableKey = |
| 94 | + SplitPoints.newBuilder() |
| 95 | + .setIndex("SingersByFirstLastName") |
| 96 | + .setKeys( |
| 97 | + 0, |
| 98 | + com.google.spanner.admin.database.v1.SplitPoints.Key.newBuilder() |
| 99 | + .setKeyParts( |
| 100 | + ListValue.newBuilder() |
| 101 | + .addValues(Value.newBuilder().setStringValue("Jane").build()) |
| 102 | + .addValues(Value.newBuilder().setStringValue("Doe").build()) |
| 103 | + .build())) |
| 104 | + .setKeys( |
| 105 | + 1, |
| 106 | + com.google.spanner.admin.database.v1.SplitPoints.Key.newBuilder() |
| 107 | + .setKeyParts( |
| 108 | + ListValue.newBuilder() |
| 109 | + .addValues(Value.newBuilder().setStringValue("38").build()) |
| 110 | + .build())) |
| 111 | + .build(); |
| 112 | + |
| 113 | + splitPoints.add(splitPointForTable); |
| 114 | + splitPoints.add(splitPointForIndex); |
| 115 | + splitPoints.add(splitPointForIndexWitTableKey); |
| 116 | + databaseAdminClient.addSplitPoints( |
| 117 | + DatabaseName.of(projectId, instanceId, databaseId), splitPoints); |
| 118 | + |
| 119 | + } catch (Exception e) { |
| 120 | + // If the operation failed during execution, expose the cause. |
| 121 | + throw (SpannerException) e.getCause(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | +// [END spanner_database_add_split_points] |
0 commit comments