Cassandra BETWEEN & ORDER BY operations

て烟熏妆下的殇ゞ 提交于 2020-01-14 16:48:06

问题


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 these 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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7255734/cassandra-between-order-by-operations

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