Skip to content

Commit 12e31e2

Browse files
committed
samples(spanner): add sample for Proto columns
1 parent e4ee19d commit 12e31e2

File tree

9 files changed

+1610
-0
lines changed

9 files changed

+1610
-0
lines changed

samples/install-without-bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@
107107

108108
<!-- compile and run all snippet tests -->
109109
<build>
110+
<resources>
111+
<resource>
112+
<directory>../snippets/src/main/resources</directory>
113+
</resource>
114+
</resources>
110115
<plugins>
111116
<plugin>
112117
<groupId>org.codehaus.mojo</groupId>

samples/snapshot/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106

107107
<!-- compile and run all snippet tests -->
108108
<build>
109+
<resources>
110+
<resource>
111+
<directory>../snippets/src/main/resources</directory>
112+
</resource>
113+
</resources>
109114
<plugins>
110115
<plugin>
111116
<groupId>org.codehaus.mojo</groupId>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2024 Google Inc.
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_add_proto_type_columns]
20+
21+
import com.google.cloud.spanner.Spanner;
22+
import com.google.cloud.spanner.SpannerOptions;
23+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
24+
import com.google.common.collect.ImmutableList;
25+
import com.google.protobuf.ByteString;
26+
import com.google.spanner.admin.database.v1.DatabaseName;
27+
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest;
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.util.concurrent.ExecutionException;
31+
32+
class AddProtoColumnSample {
33+
34+
static void addProtoColumn() throws InterruptedException, ExecutionException, IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
String databaseId = "my-database";
39+
40+
addProtoColumn(projectId, instanceId, databaseId);
41+
}
42+
43+
static void addProtoColumn(String projectId, String instanceId, String databaseId)
44+
throws InterruptedException, ExecutionException, IOException {
45+
InputStream in =
46+
AddProtoColumnSample.class
47+
.getClassLoader()
48+
.getResourceAsStream("com/example/spanner/descriptors.pb");
49+
try (Spanner spanner =
50+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
51+
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
52+
UpdateDatabaseDdlRequest request =
53+
UpdateDatabaseDdlRequest.newBuilder()
54+
.setDatabase(DatabaseName.of(projectId, instanceId, databaseId).toString())
55+
.addAllStatements(
56+
ImmutableList.of(
57+
"CREATE PROTO BUNDLE ("
58+
+ "examples.spanner.music.SingerInfo,"
59+
+ "examples.spanner.music.Genre,"
60+
+ ")",
61+
"ALTER TABLE Singers ADD COLUMN SingerInfo examples.spanner.music.SingerInfo",
62+
"ALTER TABLE Singers ADD COLUMN SingerInfoArray ARRAY<examples.spanner.music.SingerInfo>",
63+
"ALTER TABLE Singers ADD COLUMN SingerGenre examples.spanner.music.Genre",
64+
"ALTER TABLE Singers ADD COLUMN SingerGenreArray ARRAY<examples.spanner.music.Genre>"))
65+
.setProtoDescriptors(ByteString.readFrom(in))
66+
.build();
67+
// Wait for the operation to finish.
68+
// This will throw an ExecutionException if the operation fails.
69+
databaseAdminClient.updateDatabaseDdlAsync(request).get();
70+
System.out.printf("Added Proto columns %n");
71+
}
72+
}
73+
}
74+
// [END spanner_add_proto_type_columns]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2024 Google Inc.
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_query_with_proto_types_parameter]
20+
import com.example.spanner.SingerProto.Genre;
21+
import com.example.spanner.SingerProto.SingerInfo;
22+
import com.google.cloud.spanner.DatabaseClient;
23+
import com.google.cloud.spanner.DatabaseId;
24+
import com.google.cloud.spanner.ResultSet;
25+
import com.google.cloud.spanner.Spanner;
26+
import com.google.cloud.spanner.SpannerOptions;
27+
import com.google.cloud.spanner.Statement;
28+
29+
class QueryWithProtoParameterSample {
30+
31+
static void queryWithProtoParameter() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "my-project";
34+
String instanceId = "my-instance";
35+
String databaseId = "my-database";
36+
37+
try (Spanner spanner =
38+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
39+
DatabaseClient client =
40+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
41+
queryWithProtoParameter(client);
42+
}
43+
}
44+
45+
static void queryWithProtoParameter(DatabaseClient client) {
46+
Statement statement =
47+
Statement.newBuilder(
48+
"SELECT SingerId, SingerInfo, SingerInfo.nationality, SingerInfoArray, SingerGenre, SingerGenreArray FROM Singers WHERE SingerInfo.nationality=@country and SingerGenre=@singerGenre")
49+
.bind("country")
50+
.to("Country2")
51+
.bind("singerGenre")
52+
.to(Genre.FOLK)
53+
.build();
54+
try (ResultSet resultSet = client.singleUse().executeQuery(statement)) {
55+
while (resultSet.next()) {
56+
System.out.printf(
57+
"%d %s %s %s %s %s%n",
58+
resultSet.getLong("SingerId"),
59+
resultSet.getProtoMessage("SingerInfo", SingerInfo.getDefaultInstance()),
60+
resultSet.getString("nationality"),
61+
resultSet.getProtoMessageList("SingerInfoArray", SingerInfo.getDefaultInstance()),
62+
resultSet.getProtoEnum("SingerGenre", Genre::forNumber),
63+
resultSet.getProtoEnumList("SingerGenreArray", Genre::forNumber));
64+
}
65+
}
66+
}
67+
}
68+
// [END spanner_query_with_proto_types_parameter]

0 commit comments

Comments
 (0)