Dudimetla Mallesh
Dudimetla Mallesh

Reputation: 133

"ORDER BY is only supported when the partition key is restricted by an EQ or an IN." cassandra

I have created my table like this..

CREATE TABLE test1.mytest (
    id text,
    year text,
    time bigint,
    crtby text,
    data text,
    postid text,
    upby text,
    PRIMARY KEY ((id, year), time)
) WITH CLUSTERING ORDER BY (time DESC)


I want to sort my data based on time key.


SELECT * FROM test1.mytest WHERE id ='b' ORDER BY time asc ALLOW FILTERING ;

when i try the above command i am getting

InvalidRequest: Error from server: code=2200 [Invalid query] message="ORDER BY is only supported when the partition key is restricted by an EQ or an IN."

Upvotes: 2

Views: 4142

Answers (1)

Abhishek Garg
Abhishek Garg

Reputation: 2288

The error clearly describes the reason for the failure. Your primary key is id and year and then further clustering is done on time key.

Cassandra needs to know the id and the year and only then it can do an order by on time.

Since you are not always using the partitioning key (id, year) in queries, to do order by time, you will have to use Materialized Views. More details can be found here.

Upvotes: 1

Related Questions