Cassandra UPDATE primary key value

萝らか妹 提交于 2019-12-18 03:15:27

问题


I understand that this is not possible using an UPDATE.

What I would like to do instead, is migrate all rows with say PK=0 to new rows where PK=1. Are there any simple ways of achieving this?


回答1:


For a relatively simple way, you could always do a quick COPY TO/FROM in cqlsh.

Let's say that I have a column family (table) called "emp" for employees.

CREATE TABLE stackoverflow.emp (
    id int PRIMARY KEY,
    fname text,
    lname text,
    role text
)

And for the purposes of this example, I have one row in it.

aploetz@cqlsh:stackoverflow> SELECT * FROM emp;

 id | fname | lname | role
----+-------+-------+-------------
  1 | Angel |   Pay | IT Engineer

If I want to re-create Angel with a new id, I can COPY the table's contents TO a .csv file:

aploetz@cqlsh:stackoverflow> COPY stackoverflow.emp TO '/home/aploetz/emp.csv';

1 rows exported in 0.036 seconds.

Now, I'll use my favorite editor to change the id of Angel to 2 in emp.csv. Note, that if you have multiple rows in your file (that don't need to be updated) this is your opportunity to remove them:

2,Angel,Pay,IT Engineer

I'll save the file, and then COPY the updated row back into Cassandra FROM the file:

aploetz@cqlsh:stackoverflow> COPY stackoverflow.emp FROM '/home/aploetz/emp.csv';

1 rows imported in 0.038 seconds.

Now Angel has two rows in the "emp" table.

aploetz@cqlsh:stackoverflow> SELECT * FROM emp;

 id | fname | lname | role
----+-------+-------+-------------
  1 | Angel |   Pay | IT Engineer
  2 | Angel |   Pay | IT Engineer

(2 rows)

For more information, check the DataStax doc on COPY.



来源:https://stackoverflow.com/questions/27022325/cassandra-update-primary-key-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!