Skip to content

Commit b0f8dcc

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

File tree

3 files changed

+182
-1
lines changed

3 files changed

+182
-1
lines changed

samples/snippets/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>com.google.cloud</groupId>
3636
<artifactId>libraries-bom</artifactId>
37-
<version>26.54.0</version>
37+
<version>26.57.0</version>
3838
<type>pom</type>
3939
<scope>import</scope>
4040
</dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.SplitPoints;
28+
import java.io.IOException;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
public class DatabaseAddSplitPointsSample {
33+
34+
/***
35+
* Assume DDL for the underlying database:
36+
* <pre>{@code
37+
* CREATE TABLE Singers (
38+
* SingerId INT64 NOT NULL,
39+
* FirstName STRING(1024),
40+
* LastName STRING(1024),
41+
* SingerInfo BYTES(MAX),
42+
* ) PRIMARY KEY(SingerId);
43+
*
44+
*
45+
* CREATE INDEX SingersByFirstLastName ON Singers(FirstName, LastName);
46+
* }</pre>
47+
*/
48+
49+
static void addSplitPoints() throws IOException {
50+
// TODO(developer): Replace these variables before running the sample.
51+
String projectId = "my-project";
52+
String instanceId = "my-instance";
53+
String databaseId = "my-database";
54+
addSplitPoints(projectId, instanceId, databaseId);
55+
}
56+
57+
static void addSplitPoints(String projectId, String instanceId, String databaseId)
58+
throws IOException {
59+
try (Spanner spanner =
60+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
61+
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
62+
final String database =
63+
"projects/" + projectId + "/instances/" + instanceId + "/databases/" + databaseId;
64+
List<com.google.spanner.admin.database.v1.SplitPoints> splitPoints = new ArrayList<>();
65+
66+
// table key
67+
com.google.spanner.admin.database.v1.SplitPoints splitPointForTable =
68+
SplitPoints.newBuilder()
69+
.setTable("Singers")
70+
.setKeys(
71+
0,
72+
com.google.spanner.admin.database.v1.SplitPoints.Key.newBuilder()
73+
.setKeyParts(
74+
ListValue.newBuilder()
75+
.addValues(Value.newBuilder().setStringValue("42").build())
76+
.build()))
77+
.build();
78+
79+
// index key without table key part
80+
com.google.spanner.admin.database.v1.SplitPoints splitPointForIndex =
81+
SplitPoints.newBuilder()
82+
.setIndex("SingersByFirstLastName")
83+
.setKeys(
84+
0,
85+
com.google.spanner.admin.database.v1.SplitPoints.Key.newBuilder()
86+
.setKeyParts(
87+
ListValue.newBuilder()
88+
.addValues(Value.newBuilder().setStringValue("John").build())
89+
.addValues(Value.newBuilder().setStringValue("Doe").build())
90+
.build()))
91+
.build();
92+
93+
// index key with table key part
94+
com.google.spanner.admin.database.v1.SplitPoints splitPointForIndexWitTableKey =
95+
SplitPoints.newBuilder()
96+
.setIndex("SingersByFirstLastName")
97+
.setKeys(
98+
0,
99+
com.google.spanner.admin.database.v1.SplitPoints.Key.newBuilder()
100+
.setKeyParts(
101+
ListValue.newBuilder()
102+
.addValues(Value.newBuilder().setStringValue("38").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("Jane").build())
110+
.addValues(Value.newBuilder().setStringValue("Doe").build())
111+
.build()))
112+
.build();
113+
114+
splitPoints.add(splitPointForTable);
115+
splitPoints.add(splitPointForIndex);
116+
splitPoints.add(splitPointForIndexWitTableKey);
117+
databaseAdminClient.addSplitPoints(database, 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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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,"
45+
+ " LastName)"))
46+
.get();
47+
}
48+
49+
@Test
50+
public void testAddSplits() throws Exception {
51+
final String out =
52+
SampleRunner.runSample(
53+
() -> DatabaseAddSplitPointsSample.addSplitPoints(projectId, instanceId, databaseId));
54+
assertTrue(out.contains(""));
55+
}
56+
}

0 commit comments

Comments
 (0)