简体   繁体   English

CASSANDRA CQL3:更改主键

[英]CASSANDRA CQL3 : Change primary key

Currently, i have a table with following Primary KEY with 10K rows . 目前,我有一个表,其后有1万行主键。

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. 在cqlsh中,您可以使用COPY实用程序。

To export my shipcrewregistry table, I will use COPY TO : 要导出我的shipcrewregistry ,我将使用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 : 导入并重新创建表后,要导入它,我将使用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 . 有关COPY的更多信息,请查看DataStax文档

Just to expand. 只是为了扩大。 The primary key set in the CQL statement CREATE TABLE includes the partition key. CQL语句CREATE TABLE中设置的主键包括分区键。 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. 在Cassandra中,分区键定义了数据将存储在哪个节点上,因此不能更改或更改,因为现在数据将位于错误的节点上。 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. 当然,在Cassandra中,如果需要以其他方式查询,则可以在两个表中都有数据。 The joys of denormalization! 非规范化的乐趣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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