Tanya
Tanya

Reputation: 364

Retrieving results from Cassandra in order

I have stored the data in the ComparatorType.BYTESTYPE in the Cassandra database. Now I want to retrieve data in the stored order.

I used the following code in Hector to get data, but the query results seems not sorted.

Keyspace keyspace = HFactory.createKeyspace("ClusterName", cluster);

Map<String, String> resultMap = new HashMap<String, String>();

SliceQuery<String, String, String> query = HFactory.createSliceQuery(keyspace,   StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
query.setColumnFamily("ColumnFamilyName").setKey("RowKey")
.setRange("", "", false, Integer.MAX_VALUE);
QueryResult<ColumnSlice<String, String>> result = query.execute();

for (HColumn<String, String> column : result.get().getColumns()) {
        resultMap.put(column.getName(), column.getValue());
}

what am I missing?? Do I have to use RangeSliceQueries and OrderedRows??

Thanks in advance.

Upvotes: 1

Views: 1114

Answers (2)

Tanya
Tanya

Reputation: 364

Thanks douard for your answer.

But I solved it by changing the collection to a LinkedHashMap instead of a HashMap. As I noticed

    result.get().getColumns()

gives the results in order, but as HasMap doesn't preserve the insertion order, results get unordered. So the above code works fine when you change the HashMap to a LinkedHashMap.

    Map<String, String> resultMap = new LinkedHashMap<String, String>();

Upvotes: 0

le-doude
le-doude

Reputation: 3367

It all depends on the comparator you have set for the column names. If it is BytesType (cassandra's default) as well, then it won't be the order you expect.

if you want alphabetical order you should define your table with something like that in the cassandra-cli:

create column family ColumnFamilyName with comparator = UTF8Type;

or you are still in time maybe to do

update column family ColumnFamilyName with comparator = UTF8Type;

Upvotes: 1

Related Questions