Skip to content

Commit bb18e21

Browse files
committed
Add java sample for the pre-splitting feature
1 parent 9940b66 commit bb18e21

File tree

2 files changed

+168
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)