简体   繁体   中英

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 ;)

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 .

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM