我的理解是,当插入具有相同主键的另一行时,行会被覆盖。
例如:
我有列(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 的设计运作方式。在执行 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.
现在,虽然这些摘录可能不会直接出来并说“小心不要覆盖”,但我确实设法在 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/