4

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

litehause
  • 303
  • 5
  • 12
  • Possible duplicate of [Difference between partition key, composite key and clustering key in Cassandra?](http://stackoverflow.com/questions/24949676/difference-between-partition-key-composite-key-and-clustering-key-in-cassandra) – Ashraful Islam Mar 07 '17 at 14:37
  • 1
    no.I want to add a key for sorting into an existing table – litehause Mar 07 '17 at 14:41

2 Answers2

6

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.

Community
  • 1
  • 1
xmas79
  • 5,060
  • 2
  • 14
  • 35
-1

All you need to do is add it as the second part of the PRIMARY KEY to make it a composite key

create table test_moments(id Text, title Text, sort int, PRIMARY KEY(id, sort));
bechbd
  • 6,206
  • 3
  • 28
  • 47