Quentin DESBOIS
Quentin DESBOIS

Reputation: 123

CASSANDRA CQL3 : Change primary key

Currently, i have a table with following Primary KEY with 10K rows .

PRIMARY KEY ((deviceid, time), channelname)

and I need to get :

PRIMARY KEY (deviceid, time, channelname)

I saw somewhere, that I've to rebuild my whole table. So do you have some methods / advices to export my rows and import them in my new table ?

Thank you ;)

Upvotes: 3

Views: 2620

Answers (2)

polandll
polandll

Reputation: 96

Just to expand. The primary key set in the CQL statement CREATE TABLE includes the partition key. In Cassandra, the partition key defines what node the data will be stored on, so it cannot be changed or altered, because, well, the data would now be on the wrong node. In your statement, you changed the partition key from (deviceid, time) to deviceid, and a different hash would be generated to define the data location.

Of course, in Cassandra, you can have the data in both tables, if you have a need to query differently. The joys of denormalization!

Upvotes: 1

Aaron
Aaron

Reputation: 57843

I saw somewhere, that I've to rebuild my whole table.

That is correct.

So do you have some methods / advices to export my rows and import them in my new table ?

Yes, I just had to do this the other week for the exact same reason. From within cqlsh, you can use the COPY utility.

To export my shipcrewregistry table, I will use COPY TO:

aploetz@cqlsh:presentation> COPY shipcrewregistry (shipname , lastname , firstname , 
    citizenid , aliases) TO '/home/aploetz/shipcrewreg_20150805.txt' 
    WITH HEADER=true AND DELIMITER='|';

9 rows exported in 0.026 seconds.

And to import it once I have blown-away and re-created the table, I will use COPY FROM:

aploetz@cqlsh:presentation> COPY shipcrewregistry (shipname , lastname , firstname ,
    citizenid , aliases) FROM '/home/aploetz/shipcrewreg_20150805.txt' 
    WITH HEADER=true AND DELIMITER='|';

9 rows imported in 0.636 seconds.

For more information on COPY, check out the DataStax docs.

Upvotes: 4

Related Questions