加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

何时在cassandra中覆盖行

发布时间:2020-12-12 07:05:07 所属栏目:MsSql教程 来源:网络整理
导读:我的理解是,当插入具有相同主键的另一行时,将覆盖行. 例如: 我有列(user_id int,item_id int,site_id int)和我的PRIMARY KEY(user_id,item_id) 如果我有下表: user_id,item_id,site_id 2 3 4 我插入user_id:2,item_id:3,site_id:10,我的新表将是: user_
我的理解是,当插入具有相同主键的另一行时,将覆盖行.

例如:

我有列(user_id int,item_id int,site_id int)和我的PRIMARY KEY(user_id,item_id)

如果我有下表:

user_id,item_id,site_id
   2       3        4

我插入user_id:2,item_id:3,site_id:10,我的新表将是:

user_id,site_id
   2       3        10

user_id,site_id
   2       3        4
   2       3        10

这种简单的案例是否适用于所有情况?我可能没有注意到任何微妙之处吗?另外,我在文档中找不到这个并通过玩cassandra来得出这个结论,任何人都可以提供文档源吗?

解决方法

是的,这就是Cassandra的设计运作方式.在执行UPDATE或INSERT的所有情况下,如果数据存在,将更新数据(基于密钥),而不存在数据.需要记住的一点是,UPDATE和INSERT是同义词.如果你认为这两者是相同的,那么你就可以开始理解为什么它的工作方式如此.

话虽如此,你是对的,因为你必须仔细查看文档中对此行为的明确引用.我在文档中找到了最接近的参考文献,并在下面列出了它们:

从UPDATE文档:

The row is created if none existed before,and updated otherwise. Specify the row to update in the WHERE clause by including all columns composing the partition key. … The UPDATE SET operation is not valid on a primary key field.

从INSERT文件:

You do not have to define all columns,except those that make up the key. … If the column exists,it is updated. The row is created if none exists.

现在虽然这些摘录可能没有出来并且说“小心不要覆盖”,但我确实找到了一篇关于Planet Cassandra的更明确的文章:How to Do an Upsert in Cassandra

Cassandra is a distributed database that avoids reading before a write,so an INSERT or UPDATE sets the column values you specify regardless of whether the row already exists. This means inserts can update existing rows,and updates can create new rows. It also means it’s easy to accidentally overwrite existing data,so keep that in mind.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读