Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename table in Cassandra CQL3

Tags:

cassandra

cql3

I'm trying to rename table created via CQLSH. E.g. rename table "AAA" to "BBB". Can't find any command to do so. Any ideas?

Using [cqlsh 3.1.6 | Cassandra 1.2.8 | CQL spec 3.0.0 | Thrift protocol 19.36.0]

like image 677
Vladimir Kroz Avatar asked Aug 07 '13 19:08

Vladimir Kroz


People also ask

How do I rename a table in Cassandra?

The only way that I know of to do what you ask is to create the new table and move all the data from the old to the new, then drop the old table.

How do you alter a table in Cassandra?

Using ALTER command, you can add a column to a table. While adding columns, you have to take care that the column name is not conflicting with the existing column names and that the table is not defined with compact storage option. Given below is the syntax to add a column to a table.

Can we change the primary key of a table in Cassandra?

There is no way to change a primary key, as it defines how your data is physically stored. You can create a new table with the new primary key, copy data from the old one, and then drop the old table.


1 Answers

I don't believe you can rename tables or keyspaces, there's no CQL3 operation to do it, and nothing in the old Thirft interfaces either, if I remember correctly.

One reason why you can't is that it would be an extremely hard thing for Cassandra to do due to its distributed nature, the change can't be done atomically so the cluster would be in an inconsistent state, and most likely updates would be lost. It's similar to creating and dropping tables, but in those cases it's expected that updates will be lost if they're issued before the table is created or after it has been dropped.

The only way that I know of to do what you ask is to create the new table and move all the data from the old to the new, then drop the old table. There might be a way to do it without moving the data, but it would probably require you to stop the cluster and change the name of all directories and files belonging to the table, and also change the metadata in the system.schema_columnfamilies table (but I'm not sure you can even do that).

like image 138
Theo Avatar answered Sep 19 '22 12:09

Theo