【发布时间】:2019-04-10 20:46:05
【问题描述】:
我正在尝试为我的应用实现某种分页功能,在后端使用 cassandra。
CREATE TABLE sample (
some_pk int,
some_id int,
name1 txt,
name2 text,
value text,
PRIMARY KEY (some_pk, some_id, name1, name2)
)
WITH CLUSTERING ORDER BY(some_id DESC)
我想查询 100 条记录,然后将最后的记录键存储在内存中以供以后使用。
+---------+---------+-------+-------+-------+
| sample_pk| some_id | name1 | name2 | value |
+---------+---------+-------+-------+-------+
| 1 | 125 | x | '' | '' |
+---------+---------+-------+-------+-------+
| 1 | 124 | a | '' | '' |
+---------+---------+-------+-------+-------+
| 1 | 124 | b | '' | '' |
+---------+---------+-------+-------+-------+
| 1 | 123 | y | '' | '' |
+---------+---------+-------+-------+-------+
(为简单起见,我将一些列留空。分区键(sample_pk)并不重要)
假设我的页面大小为 2。
select * from sample where sample_pk=1 limit 2;
返回前 2 行。现在我将最后一条记录存储在查询结果中并再次运行查询以获取接下来的 2 行;
这是由于单个非 EQ 关系的限制而无法工作的查询
select * from where sample_pk=1 and some_id <= 124 and name1>='a' and name2>='' limit 2;
这个返回错误的结果,因为 some_id 是降序的,而 name 列是升序的。
select * from where sample_pk=1 and (some_id, name1, name2) <= (124, 'a', '') limit 2;
所以我被困住了。如何实现分页?
【问题讨论】:
标签: cassandra datastax-java-driver cqlsh spring-data-cassandra