Search code examples
cassandracql3

Cassandra how to add clustering key in table?


There is a table in cassandra

create table test_moments(id Text, title Text, sort int, PRIMARY KEY(id));

How add clustering key in column "sort". Not re-creating the table


Solution

  • The main problem is the on-disk data structure. Clustering key directly dictates how data is sorted and serialized to disk (and then searched), so what you're asking is not possible.

    The only way is to "migrate" the data to another table. Depending on your data, if you have a lot of records you could encounter some timeout error during the queries, so be prepared to tweak your migration with some useful techniques such as the COPY command or the TOKEN function.

    Have a look at this SO question also.