Search code examples
databasecassandracql

Cassandra order by second clustering key


I have a table in cassandra and I want to order by the second clustering column and leave the first clustering column. It is the table definition:

CREATE TABLE table1 (
key int,
value1 text,
value2 text,
value3 text,
comments text,
PRIMARY KEY (key, value1, value2, value3)
)WITH CLUSTERING ORDER BY (value2 DESC);

I know that the above script is wrong and I should change it below:

CREATE TABLE table1 (
key int,
value1 text,
value2 text,
value3 text,
comments text,
PRIMARY KEY (key, value1, value2, value3)
)WITH CLUSTERING ORDER BY (value1 DESC, value2 DESC);

but I want to sort it by the only value2(and not the value1). Is it possible? Is there any way to achieve this?


Solution

  • It's not possible out of box - data is sorted hierarchically inside the partition - first they are sorted by first clustering column, then sorted inside every unique value of "parent column", etc. Something like this (CL - clustering column):

    partition key:
      CL1 value 1:
        CL2 value 1
        CL2 value 2
      CL1 value 2
        CL2 value 1
        CL2 value 3
      ...