Cassandra UPDATE 主键值 [英] Cassandra UPDATE primary key value

查看:12
本文介绍了Cassandra UPDATE 主键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解使用 UPDATE 无法做到这一点.

I understand that this is not possible using an UPDATE.

我想做的是将所有带有 PK=0 的行迁移到 PK=1 的新行.有什么简单的方法可以实现吗?

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?

推荐答案

对于一个相对简单的方法,你总是可以在 cqlsh 中快速COPY TO/FROM.

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

假设我有一个名为emp"的列族(表)供员工使用.

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

如果我想用新的 id 重新创建 Angel,我可以COPY 表格的内容 TO .csv 文件:

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.

现在,我将使用我最喜欢的编辑器将 emp.csv 中的 Angel 的 id 更改为 2.请注意,如果您的文件中有多行(不需要更新),这是您删除它们的机会:

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

我将保存文件,然后将更新后的行COPY 返回到Cassandra FROM 文件中:

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.

现在 Angel 在emp"表中有两行.

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)

有关详细信息,请查看 关于 COPY 的 DataStax 文档.

For more information, check the DataStax doc on COPY.

这篇关于Cassandra UPDATE 主键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆