如何在使用 Google Spanner 的 readUsingIndex 方法时指定我的 WHERE 条件
How do I specify my WHERE conditions while using readUsingIndex method of Google Spanner
我有以下来自 Google Spanner docs
的代码片段
如何在此指定 WHERE 条件?
在我看来,在迭代 ResultSet 时,我们只能根据此条件和 WHERE 条件执行常规 SELECT。
static void readStoringIndex(DatabaseClient dbClient) {
// We can read MarketingBudget also from the index since it stores a copy of MarketingBudget.
try (ResultSet resultSet = dbClient
.singleUse()
.readUsingIndex(
"Albums",
"AlbumsByAlbumTitle2",
KeySet.all(),
Arrays.asList("AlbumId", "AlbumTitle", "MarketingBudget"))) {
while (resultSet.next()) {
System.out.printf(
"%d %s %s\n",
resultSet.getLong(0),
resultSet.getString(1),
resultSet.isNull("MarketingBudget") ? "NULL" : resultSet.getLong("MarketingBudget"));
}
}
}
使用readUsingIndex
方法时,您可以通过指定一个或多个要读取的索引键值来过滤要读取的行。
假设索引 AlbumsByAlbumTitle2
包含列 AlbumTitle
并且您想阅读标题为“Great album”和“[=22”的专辑=]不是很好的专辑'。你的电话应该是这样的:
client
.singleUse()
.readUsingIndex(
"Albums",
"AlbumsByAlbumTitle2",
KeySet.newBuilder()
.addKey(Key.of("Great album"))
.addKey(Key.of("Not so great album"))
.build(),
Arrays.asList("AlbumId", "AlbumTitle", "MarketingBudget"));
A KeySet
也可以是范围 (begin/end) 或前缀。请注意,当使用方法 readUsingIndex
时,您指定要使用二级索引来过滤行。这意味着您不能过滤二级索引中列以外的任何其他列。
如果您希望能够在二级索引查询中指定 WHERE 语句,您可以切换到 SQL 语言。
SELECT AlbumId, AlbumTitle, MarketingBudget
FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle2} AS s
WHERE s.AlbumTitle = "Go, Go, Go";
在这里,我将 Cloud Spanner 文档中的代码包含到 Query Using a Parameter,我向其中添加了 WHERE
语句。
static void queryWithParameter(DatabaseClient dbClient) {
Statement statement =
Statement.newBuilder(
"SELECT AlbumId, AlbumTitle, MarketingBudget "
+ "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle2} AS s "
+ "WHERE s.AlbumTitle = @title")
// here you add more conditionals statements
.bind("title")
.to("Go, Go, Go")
.build();
try (ResultSet resultSet = dbClient.singleUse().executeQuery(statement)) {
while (resultSet.next()) {
System.out.printf(
"%d %s %s\n",
resultSet.getLong("SingerId"),
resultSet.getString("FirstName"),
resultSet.getString("LastName"));
}
}
}
其他方法包括:
我有以下来自 Google Spanner docs
的代码片段如何在此指定 WHERE 条件?
在我看来,在迭代 ResultSet 时,我们只能根据此条件和 WHERE 条件执行常规 SELECT。
static void readStoringIndex(DatabaseClient dbClient) {
// We can read MarketingBudget also from the index since it stores a copy of MarketingBudget.
try (ResultSet resultSet = dbClient
.singleUse()
.readUsingIndex(
"Albums",
"AlbumsByAlbumTitle2",
KeySet.all(),
Arrays.asList("AlbumId", "AlbumTitle", "MarketingBudget"))) {
while (resultSet.next()) {
System.out.printf(
"%d %s %s\n",
resultSet.getLong(0),
resultSet.getString(1),
resultSet.isNull("MarketingBudget") ? "NULL" : resultSet.getLong("MarketingBudget"));
}
}
}
使用readUsingIndex
方法时,您可以通过指定一个或多个要读取的索引键值来过滤要读取的行。
假设索引 AlbumsByAlbumTitle2
包含列 AlbumTitle
并且您想阅读标题为“Great album”和“[=22”的专辑=]不是很好的专辑'。你的电话应该是这样的:
client
.singleUse()
.readUsingIndex(
"Albums",
"AlbumsByAlbumTitle2",
KeySet.newBuilder()
.addKey(Key.of("Great album"))
.addKey(Key.of("Not so great album"))
.build(),
Arrays.asList("AlbumId", "AlbumTitle", "MarketingBudget"));
A KeySet
也可以是范围 (begin/end) 或前缀。请注意,当使用方法 readUsingIndex
时,您指定要使用二级索引来过滤行。这意味着您不能过滤二级索引中列以外的任何其他列。
如果您希望能够在二级索引查询中指定 WHERE 语句,您可以切换到 SQL 语言。
SELECT AlbumId, AlbumTitle, MarketingBudget
FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle2} AS s
WHERE s.AlbumTitle = "Go, Go, Go";
在这里,我将 Cloud Spanner 文档中的代码包含到 Query Using a Parameter,我向其中添加了 WHERE
语句。
static void queryWithParameter(DatabaseClient dbClient) {
Statement statement =
Statement.newBuilder(
"SELECT AlbumId, AlbumTitle, MarketingBudget "
+ "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle2} AS s "
+ "WHERE s.AlbumTitle = @title")
// here you add more conditionals statements
.bind("title")
.to("Go, Go, Go")
.build();
try (ResultSet resultSet = dbClient.singleUse().executeQuery(statement)) {
while (resultSet.next()) {
System.out.printf(
"%d %s %s\n",
resultSet.getLong("SingerId"),
resultSet.getString("FirstName"),
resultSet.getString("LastName"));
}
}
}
其他方法包括: