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