例如:
我有列(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来得出这个结论,任何人都可以提供文档源吗?
解决方法
话虽如此,你是对的,因为你必须仔细查看文档中对此行为的明确引用.我在文档中找到了最接近的参考文献,并在下面列出了它们:
从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.
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.