Sorting order in cassandra result

那年仲夏 提交于 2019-12-10 21:04:10

问题


I created table

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);

then on

SELECT testtabmainid,posteddate,year,month, day FROM testtab;

i got result like this

testtabmainid / posteddate / year / month / day

90 / 2016-12-01 11:19:11+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

91 / 2016-12-01 11:20:42+0530 / 2016 / 12 / 1

the last row is not in sorted order. I need the last row (testtabmainid =91 ) on the top

i need to sort table in testtabmainid in desc manner


回答1:


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);

and then change your query to:

SELECT testtabmainid,posteddate,year,month, day 
FROM testtab 
WHERE year=2016 AND 
      month=12 AND 
      day=1;

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.

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.



来源:https://stackoverflow.com/questions/40906913/sorting-order-in-cassandra-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!