Apache Cassandra ordering bv clustering keys?

Viewed 31

I have a table with this setup:

...
    PRIMARY KEY (id, "requestId", "createdAt")
) WITH CLUSTERING ORDER BY ("requestId" ASC, "createdAt" DESC)

So when I run CQL like:

SELECT * from "Table" WHERE "requestId"='someId'

Should I get results ordered by createdAt DESC? That's what I would expect, but I'm new to Cassandra and am not sure about how this works.

What I want to achieve is to retrieve all records that belong a certain requestId, ordered by creation date DESC.

How do I do this?

1 Answers

In Cassandra, data is ordered only inside the partition, not globally. If you do SELECT * from "Table" WHERE id = .... AND "requestId"='someId', then you will get data by createdAt in descending order

Your original query won't work out of box because you don't have the partition key provided. And if you put "ALLOW FILTERING" (bad idea), then you will get data back, but ordered by createdAt inside each partition separately.

Related