The version of cassandra used in this article is version 2.0.6
1. Prerequisites and Constraints
CREATE TABLE test(a INT,b INT,c INT, d string,e string,PRIMARY KEY(a,b,c)); CREATE INDEX ON test(d);
cassandra's query has the following constraints:
The first primary key can only be queried with the = sign.
The second primary key supports = > < >= <= but must be followed by an ALLOW FILTERING
index column that only supports the = sign
2. Paging query
First use the limit keyword to limit the number of query results for paging.
Turning pages is a complex process.
It needs to be clear that the results of the query are sorted by token(a), followed by (b,c).
Assuming a query without any query conditions, the first query statement is
SELECT * FROM test LIMIT 10;
The primary key of the 10th record that comes out needs to be recorded, assuming it is
a10 b10 c10
Then when the second query is performed, the statement should be written like this:
SELECT * FROM test WHERE token(a)=token(a10) AND (b,c)>(b10,c10) LIMIT 10;
Assuming that there are only 6 found,
Then you need to continue to find out 4, the statement should be written like this:
select * from test where token(a)>token(a10) limit 4;
If there is data behind, the primary key of the 20th record should also be recorded, assuming it is
a20 b20 c20
Assuming that you want to continue turning pages, you can repeat the process of the second query.
3、总结
cassandra 的分页查询,主要是通过查询结果的默认的排列顺序来实现的,本文的例子是没有查询条件的情况,
有查询条件的情况,也是一样的。你只要知道了cassandra的默认查询结果的排序规则,就知道如何具体的分页查询了。