本文介绍了在cassandra中使用order by子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cassandra中创建表时,我们可以给聚类键如下所示的排序。

 创建表用户(partitionkey int,id int,name varchar,age int,address text,
insrt_ts timestamp,
主键(partitionkey,name,insrt_ts,id)
与聚簇顺序(name asc,insrt_ts desc,id asc);


当我们将数据插入该表时,p>

检索具有CQL1和CQL2的记录,我按照相同的排序顺序。



CQL1:

 从partitionkey = 101的用户中选择*; 

CQL2:

 从名称为insrt desc,id的用户选择* partitionkey = 101的用户; 
/ pre>

CQL1和CQL2之间有什么区别?

解决方案

DataStax推荐了一种面向查询的数据建模方法,其中结果按查询时所需的顺序存储。此顺序由[复合]主键的分区键和第一个聚簇列确定。 解释了这一点:



您还可以使用Apache Cassandra在线课程(。我相信第3节提供了一个关于 ORDER BY 子句的模块。


When creating table in cassandra, we can give clustering keys with ordering like below.

Create table user(partitionkey int, id int, name varchar, age int, address text,
   insrt_ts timestamp,
 Primary key(partitionkey, name, insrt_ts, id)
 with clustering order by (name asc, insrt_ts desc, id asc);

when we insert data into that table, As per cassandra documentation records are sorted based on clustering keys.

When i retrieve records with CQL1 and CQL2, I am getting in the same sorted order.

CQL1:

Select * from user where partitionkey=101;

CQL2:

Select * from user where partitionkey=101 order by name, insrt desc, id;

what is the difference between CQL1 and CQL2?

解决方案

DataStax recommends a query-oriented data modeling approach, where the results are stored in the order in which they are required at query-time. This order is determined by both the partition key and first clustering column of a [compound] primary key. The DataStax doc on the ORDER BY clause explains this:

So basically you can really only alter it by asking for ASCending or DESCending order.

You also might want to check out the (free) Java development with Apache Cassandra online class at DataStax Ac*ademy. I believe session 3 offers a module on the ORDER BY clause.

这篇关于在cassandra中使用order by子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 15:57