Sort by keywords in Kassandra

Suppose I have a keyspace with a column family that stores user objects, and the key to these objects is the username.

How can I use Hector to get a list of users sorted by username?

I tried using RangeSlicesQuery, paging works fine with this query, but the results are not sorted in any way.

I'm an absolute Cassandra newbie, can someone point me to a simple example that shows how to sort a column family by key? Please ask if you need more information about my efforts.

Edit:

The result was not sorted because I used the DefaultPartitioner by default instead of OrderPreseveringPartitioner in cassandra.yaml.

It is probably best not to rely on sorting by key, but to use a secondary index.

+4
source share
2 answers

Cassandra Quote - The Ultimate Guide

Column names are stored in sorted order according to compare_with. Rows, on the other hand, are stored in the order specified by the separator (for example, with RandomPartitioner, they are in random order, etc.)

I assume you are using RandomPartitioner , which

... return data in a substantially random order.

You should OrderPreservingPartitioner (OPP) use OrderPreservingPartitioner (OPP) , where

Therefore, strings are stored in the order of the key, aligning the physical data structure with your sort order.

Remember the inefficiency of OPP.


(edit March 7, 2014)
Important:

This answer is very old.

This is a system-wide setting. You can set the value of cassandra.yaml . See this document . Again, OPP is very discouraged. This document is for version 1.1, and you can see that it is out of date. It is probably removed from the latest version. If you want to use OPP, you might want to rethink your architecture architecture.

+5
source

Or create a row called "meta: userNames" in the same column family and put all usernames as a search hash. Something like that.

 Users { key: "meta:userNames" {david:david, paolo:paolo, victor:victor}, key: "paolo" {password:"*****", locale:"it_it"}, key: "david" {password:"*****", locale:"en_us"}, key: "victor" {password:"*****", locale:"en_uk"} } 

First, query the meta:userNames columns (which are sorted) and use them to get custom rows. Do not try to get everything through a single db query, as in a SQL-based database. Use Cassandra as a huge Hash map that provides quick random access to its data.

+1
source

All Articles