Cassandra 中 UPDATE 和 INSERT 的区别? [英] Difference between UPDATE and INSERT in Cassandra?

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

问题描述

在对 Cassandra 执行 CQL 时,UPDATEINSERT 有什么区别?

看起来以前没有区别,但现在 文档 表示 INSERT 不支持计数器,而 UPDATE 支持.

是否有首选"方法可以使用?或者在某些情况下应该使用一个而不是另一个?

非常感谢!

解决方案

Cassandra 中的计数器列不能设置为任意值:它们只能增加或减少任意值.

出于这个原因,INSERT 不支持计数器列,因为您无法将值插入"到计数器列中.您只能UPDATE 它们(增加或减少)某个值.以下是更新 Counter 列的方法.

 UPDATE ... SET name1 = name1 + 

你问:

<块引用><块引用>

是否有首选"方法可以使用?或者在某些情况下应该使用一个而不是另一个?

是的.如果要向数据库插入值,可以使用 INSERT.如果该列不存在,它将为您创建.否则,INSERT 的效果类似于UPDATE.INSERT 在您没有预先设计的模式(动态列族,即随时插入任何内容)时很有用.如果您事先设计架构(静态列族,类似于 RDMS)并且了解每一列,那么您可以使用 UPDATE.

What is the difference between UPDATE and INSERT when executing CQL against Cassandra?

It looks like there used to be no difference, but now the documentation says that INSERT does not support counters while UPDATE does.

Is there a "preferred" method to use? Or are there cases where one should be used over the other?

Thanks so much!

解决方案

Counter Columns in Cassandra couldn't be set to an arbitrary value: they can only be incremented or decremented by any arbitrary value.

For this reason, INSERT doesn't support Counter Column because you cannot "insert" a value into a Counter Column. You can only UPDATE them (increment or decrement) by some value. Here's how you would update a Counter column.

    UPDATE ... SET name1 = name1 + <value> 

You asked:

Is there a "preferred" method to use? Or are there cases where one should be used over the other?

Yes. If you are inserting values to the database, you can use INSERT. If the column doesn't exists, it will be created for you. Otherwise, INSERT's effect is similar to UPDATE. INSERT is useful when you don't have a pre-designed schema (Dynamic Column Family, i.e. insert anything, anytime). If you are designing the schema before hand (Static Column Family, similar to RDMS) and know each column, then you can use UPDATE.

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

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