开发者

Cassandra BETWEEN & ORDER BY operations

I wanted to perform SQL operations such as BETWEEN, ORDER BY with ASC/DSC order on Cassandra-0.7.8.

As I know, Cassandra-0.7.8 does not have direct support to these operations. Kindly let me know is there a way to accomplish the开发者_JAVA技巧se by tweaking on secondary index?

Below is my Data model design.

Emp(KS){
    User(CF):{
         bsanderson(RowKey): { eno, name, dept, dob, email }
     prothfuss(RowKey): { eno, name, dept, dob, email }
}
}

Queries:

 - Select * from emp where dept='IT' ORDER BY dob ASC. 
 - Select * from emp where eno BETWEEN ? AND ? ORDER BY dob ASC.

Thanks in advance.

Regards,

Thamizhananl


Select * from emp where dept='IT' ORDER BY dob ASC.

You can select rows where the 'dept' column has a certain value, by using the built-in secondary indexes. However, the rows will be returned in the order determined by the partitioner (RandomPartitioner or OrderPreservingPartitioner). To order by arbitrary values such as DOB, you would need to sort at the client.

Or, you could support this query directly by having a row for each dept, and a column for each employee, keyed (and therefore sorted) by DOB. But be careful of shared birthdays! And you'd still need subsequent queries to retrieve other data (the results of your SELECT *) for the employees selected, unless you denormalise so that the desired data is stored in the index too.

Select * from emp where eno BETWEEN ? AND ? ORDER BY dob ASC.

The secondary index querying in Cassandra requires at least one equality term, so I think you can do dept='IT' AND eno >=X AND eno <=y, but not just a BETWEEN-style query.

You could do this by creating your own index row, with a column for each employee, keyed on the employee number, with an appropriate comparator so all the columns are automatically sorted in employee-number order. You could then do a range query on that row to get a list of matching employees - but you would need further queries to retrieve other data for each employee (dob etc), unless you denormalise so that the desired data is stored in the index too. You would still need to do the dob ordering at the client.


As I know the columns will be sorted by comparator when you create column family and you can use clustring key for sorting on your opinion and row in column family will be sorted by partitioner I suggest you read this paper
Cassandra The Definitive Guide Chapter 6

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜