Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Order by clause in cassandra

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?

like image 668
suresh Avatar asked Jan 29 '14 10:01

suresh


1 Answers

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:

Querying compound primary keys and sorting results ORDER BY clauses can select a single column only. That column has to be the second column in a compound PRIMARY KEY. This also applies to tables with more than two column components in the primary key. Ordering can be done in ascending or descending order, default ascending, and specified with the ASC or DESC keywords.

In the ORDER BY clause, refer to a column using the actual name, not the aliases.

For example, set up the playlists table, which uses a compound primary key, insert the example data, and use this query to get information about a particular playlist, ordered by song_order. As of Cassandra 1.2, you do not need to include the ORDER BY column in the select expression.

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.

Edit 2019

For all future Googlers, I wrote an article on this topic about a year after this post. It's older now, but still relevant to result set ordering with Cassandra: https://www.datastax.com/dev/blog/we-shall-have-order

like image 128
Aaron Avatar answered Sep 28 '22 02:09

Aaron