卡桑德拉结果中的排序顺序 [英] Sorting order in cassandra result

查看:98
本文介绍了卡桑德拉结果中的排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了表格

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆