Skip to content

Commit 1fb6e94

Browse files
samples: Adding samples for updating & querying Proto messages & enums (#2211)
* samples: Adding samples for updating & querying Proto messages & enums * style: linting * style: linting * docs: Adding function and class doc
1 parent cbabd6c commit 1fb6e94

File tree

3 files changed

+1432
-0
lines changed

3 files changed

+1432
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2022 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+
package com.example.spanner;
17+
18+
import com.example.spanner.SingerProto.Genre;
19+
import com.example.spanner.SingerProto.SingerInfo;
20+
import com.google.cloud.spanner.DatabaseClient;
21+
import com.google.cloud.spanner.DatabaseId;
22+
import com.google.cloud.spanner.ResultSet;
23+
import com.google.cloud.spanner.Spanner;
24+
import com.google.cloud.spanner.SpannerOptions;
25+
import com.google.cloud.spanner.Statement;
26+
27+
/**
28+
* To query Proto column including messages and enums and array of them.
29+
*/
30+
public class QueryProtoColumnSample {
31+
32+
static void queryProtoColumn() {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "my-project";
35+
String instanceId = "my-instance";
36+
String databaseId = "my-database";
37+
38+
try (Spanner spanner =
39+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
40+
DatabaseClient client =
41+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
42+
queryProtoColumn(client);
43+
}
44+
}
45+
46+
/**
47+
* Method to query Singer Table using DQL.
48+
*
49+
* Assuming a table Singer with columns with following DDL:
50+
* CREATE TABLE Singer (
51+
* singer_id INT64 NOT NULL,
52+
* singer_info spanner.examples.music.SingerInfo,
53+
* genre spanner.examples.music.Genre,
54+
* singer_info_list ARRAY<spanner.examples.music.SingerInfo>,
55+
* genre_list ARRAY<spanner.examples.music.Genre>,
56+
* ) PRIMARY KEY (singer_id);
57+
*/
58+
static void queryProtoColumn(DatabaseClient client) {
59+
Statement statement =
60+
Statement.newBuilder(
61+
"SELECT singer_id, singer_info, genre, singer_info_list, genre_list\n"
62+
+ "FROM Singer")
63+
.build();
64+
65+
try (ResultSet resultSet = client.singleUse().executeQuery(statement)) {
66+
while (resultSet.next()) {
67+
System.out.printf(
68+
"singer_id: %s, singer_info: %s , genre: %s, "
69+
+ "singer_info_list: %s, genre_list: %s%n ",
70+
resultSet.getLong("singer_id"),
71+
resultSet.getProtoMessage("singer_info", SingerInfo.getDefaultInstance()),
72+
resultSet.getProtoEnum("genre", Genre::forNumber),
73+
resultSet.getProtoMessageList("singer_info_list", SingerInfo.getDefaultInstance()),
74+
resultSet.getProtoEnumList("genre_list", Genre::forNumber));
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)