Skip to content

Commit e97b92e

Browse files
authored
feat: Add java sample for the pre-splitting feature (#3713)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-spanner/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent f0f072b commit e97b92e

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
525525
| Create Sequence Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateSequenceSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateSequenceSample.java) |
526526
| Create Table With Foreign Key Delete Cascade Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateTableWithForeignKeyDeleteCascadeSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateTableWithForeignKeyDeleteCascadeSample.java) |
527527
| Custom Timeout And Retry Settings Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) |
528+
| Database Add Split Points Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DatabaseAddSplitPointsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DatabaseAddSplitPointsSample.java) |
528529
| Delete Backup Schedule Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java) |
529530
| Delete Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) |
530531
| Delete Using Dml Returning Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteUsingDmlReturningSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteUsingDmlReturningSample.java) |

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.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]
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)