Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: Adding samples for updating & querying Proto messages & enums #2211

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.spanner;

import com.example.spanner.SingerProto.Genre;
import com.example.spanner.SingerProto.SingerInfo;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;

/**
* To query Proto column including messages and enums and array of them.
*/
public class QueryProtoColumnSample {
gauravpurohit06 marked this conversation as resolved.
Show resolved Hide resolved

static void queryProtoColumn() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
String databaseId = "my-database";

try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
queryProtoColumn(client);
}
}

/**
* Method to query Singer Table using DQL.
*
* Assuming a table Singer with columns with following DDL:
* CREATE TABLE Singer (
* singer_id INT64 NOT NULL,
* singer_info spanner.examples.music.SingerInfo,
* genre spanner.examples.music.Genre,
* singer_info_list ARRAY<spanner.examples.music.SingerInfo>,
* genre_list ARRAY<spanner.examples.music.Genre>,
* ) PRIMARY KEY (singer_id);
*/
static void queryProtoColumn(DatabaseClient client) {
Statement statement =
Statement.newBuilder(
"SELECT singer_id, singer_info, genre, singer_info_list, genre_list\n"
+ "FROM Singer")
.build();

try (ResultSet resultSet = client.singleUse().executeQuery(statement)) {
while (resultSet.next()) {
System.out.printf(
"singer_id: %s, singer_info: %s , genre: %s, "
+ "singer_info_list: %s, genre_list: %s%n ",
resultSet.getLong("singer_id"),
resultSet.getProtoMessage("singer_info", SingerInfo.getDefaultInstance()),
resultSet.getProtoEnum("genre", Genre::forNumber),
resultSet.getProtoMessageList("singer_info_list", SingerInfo.getDefaultInstance()),
resultSet.getProtoEnumList("genre_list", Genre::forNumber));
}
}
}
}
Loading