'How do order by with one primary key cassandra?

I'm trying to use the order by feature of cassandra, but with only one primary key. But when I try to create my table, this is what cassandra returns.

CREATE TABLE user_classement
(
    user_name set<text>, 
    score float, 
    PRIMARY KEY (score) 
) WITH CLUSTERING ORDER BY (score DESC);

But cassandra throws this error:

Clustering key columns must exactly match columns in CLUSTERING ORDER BY directive

In case there are two primary keys when I create a new column, it works but with only one primary key, I get this error.

Do you know if it is possible to make an order by with only one primary key?



Solution 1:[1]

primary key in Cassandra consists of partition key and clustering key. First part in primary key represents partition key. So in your example score is the partition key and ordering can be applied on clustering keys. If you have had a primary key like PRIMARY KEY (score, rank) then you can apply ordering on rank. For partition ordering you may try ByteOrderedPartitioner. But I have not tried it so cannot comment further than this.

Edit 1: As added by Aaron in comments only Murmur3 paritioner should be used. ByteOrderPartitioner is only for backward compatibility for upgrade from old versions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1