Bib*_*wal 5 query-builder cassandra cql3 datastax-java-driver
在我的cassandra表中,我有一个Map的集合,我也已经为地图键编制了索引.
CREATE TABLE IF NOT EXISTS test.collection_test(
name text,
year text,
attributeMap map<text,text>,
PRIMARY KEY ((name, year))
);
CREATE INDEX ON collection_test (attributeMap);
Run Code Online (Sandbox Code Playgroud)
QueryBuilder语法如下:
select().all().from("test", "collection_test")
.where(eq("name", name)).and(eq("year", year));
Run Code Online (Sandbox Code Playgroud)
我应该如何放置条件attributeMap?
首先,您需要在映射中的键上创建索引。默认情况下,在映射上创建的索引对映射的值进行索引,而不是对键进行索引。有特殊的语法来索引键:
CREATE INDEX attributeKeyIndex ON collection_test (KEYS(attributeMap));
Run Code Online (Sandbox Code Playgroud)
接下来,要SELECT创建带有索引键的映射,您将需要关键字CONTAINS KEY。但目前,查询生成器 API 中没有此功能的定义。然而,有一个开放的票据来支持它:JAVA-677
目前,要使用 Java 驱动程序完成此操作,您需要构建自己的查询或使用准备好的语句:
PreparedStatement statement = _session.prepare("SELECT * " +
"FROM test.collection_test " +
"WHERE attributeMap CONTAINS KEY ?");
BoundStatement boundStatement = statement.bind(yourKeyValue);
ResultSet results = _session.execute(boundStatement);
Run Code Online (Sandbox Code Playgroud)
最后,您应该仔细阅读有关何时使用索引的DataStax 文档。众所周知,二级索引表现不佳。我无法想象集合上的二级索引会有什么不同。
| 归档时间: |
|
| 查看次数: |
923 次 |
| 最近记录: |