cassandra - cassandra 中的行何时被覆盖

标签 cassandra insert cql

我的理解是,当插入具有相同主键的另一行时,行会被覆盖。

例如:

我有列(user_id int, item_id int, site_id int)和我的主键(user_id, item_id)

如果我有下表:

user_id, item_id, site_id
   2       3        4

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

user_id, item_id, site_id
   2       3        10

不是

user_id, item_id, site_id
   2       3        4
   2       3        10

这个简单的情况是否适用于所有情况?有什么我可能没有意识到的微妙之处吗?另外,我在文档中找不到这一点,并通过使用 cassandra 得出了这个结论,任何人都可以提供文档源吗?

最佳答案

是的,这就是 Cassandra 的设计运作方式。在执行 UPDATEINSERT 的所有情况下,如果数据存在,则数据将被更新(基于键),如果不存在,则将其插入。要记住的重要一点是,在底层,UPDATEINSERT 是同义词。如果您认为这两者是相同的,那么您就可以开始理解为什么它会以这种方式工作。

话虽如此,您是正确的,因为您必须仔细查看才能在文档中找到对此行为的明确引用。我在文档中找到了最接近的引用文献并将其列出如下:

来自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.

现在,虽然这些摘录可能不会直接出来并说“小心不要覆盖”,但我确实设法在 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.

关于cassandra - cassandra 中的行何时被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28350630/

相关文章:

python - "insert or ignore"之后lastrowid的值

cassandra - 无法更改/更改 cql cassandra 中列的数据类型

python - Pycassa col_fam.get_indexed_slices(clause) 有什么问题

java - com.datastax.driver.core.exceptions.InvalidQueryException 使用 Datastax Java 驱动程序

Java CQL驱动程序查询时间戳时出现无效查询异常

python - Cassandra 驱动程序 : ImportError: No module named queue

php - 由于语法不正确,INSERT INTO php mysql 语句无法正常工作。

sql - 从另一个不存在记录的表插入到表中

scala - 设置 Cassandra 表扫描上的 Spark 任务数

mysql - 如何设计用户操作日志的Cassandra方案?