卡桑德拉结果中的排序顺序 [英] Sorting order in cassandra result
问题描述
我创建了表格
CREATE TABLE testtab (
testtabmainid bigint,
testtabid timeuuid,
posteddate timestamp,
description text,
year bigint,
month bigint,
day bigint,
PRIMARY KEY ((year,month,day),posteddate,testtabmainid)
) WITH CLUSTERING ORDER BY (posteddate DESC,testtabmainid desc);
然后打开
SELECT testtabmainid,posteddate,year,month, day FROM testtab;
我得到了这样的结果
testtabmainid / 发布日期 / 年份 / 月 / 天
testtabmainid / posteddate / year / month / day
90 / 2016-12-01 11:19:11 + 0530 / 2016 / 11 / 30
90 / 2016-12-01 11:19:11+0530 / 2016 / 11 / 30
89 / 2016-11-30 16:21:58 + 0530 / 2016 / 11 / 30
89 / 2016-11-30 16:21:58+0530 / 2016 / 11 / 30
88 / 2016-11-30 16:13:33 + 0530 / 2016 / 11 / 30
88 / 2016-11-30 16:13:33+0530 / 2016 / 11 / 30
91 / 2016-12-01 11:20:42 + 0530 / 2016 / 12 / 1
91 / 2016-12-01 11:20:42+0530 / 2016 / 12 / 1
最后一行未排序.我需要顶部的最后一行(testtabmainid = 91)
the last row is not in sorted order. I need the last row (testtabmainid =91 ) on the top
我需要以desc方式对testtabmainid中的表进行排序
i need to sort table in testtabmainid in desc manner
推荐答案
您查询时未指定任何WHERE
子句.这会产生按TOKEN
函数排序的结果,该结果将应用于分区键数据.
You queried without specifying any WHERE
clause. This produces results ordered by the TOKEN
function applied to your partition key data.
为了满足您的查询,您需要首先将表定义更改为:
In order to satisfy your query you need to change first of all the table definition to:
CREATE TABLE testtab (
testtabmainid bigint,
testtabid timeuuid,
posteddate timestamp,
description text,
year bigint,
month bigint,
day bigint,
PRIMARY KEY ((year,month,day),testtabmainid,posteddate)
) WITH CLUSTERING ORDER BY (testtabmainid desc,posteddate DESC);
,然后将查询更改为:
SELECT testtabmainid,posteddate,year,month, day
FROM testtab
WHERE year=2016 AND
month=12 AND
day=1;
关键点在于,数据仅由CLUSTERING KEY
在分区内排序,因此这就是为什么您需要使用WHERE
子句过滤查询以获取您的订单.
The key point is that data is ordered by your CLUSTERING KEY
only inside a partition, and that's why you need to filter your queries with a WHERE
clause to obtain your order.
如果要保持 posteddata DESC 的顺序,则需要创建另一个表(您已经拥有的表就可以了)并插入/更新两个表.
If you want to keep the posteddata DESC order, you'll need to create another table (the one you already have is fine) and insert/update both tables.
这篇关于卡桑德拉结果中的排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!