简体   繁体   English

PutIf 与 PutIfExists 和 PutIfNotExists 有何不同

[英]How is PutIf different from PutIfExists and PutIfNotExists

I am using ScalarDB which adds ACID support in Cassandra .我正在使用ScalarDB ,它在Cassandra中添加了 ACID 支持。 Referring to the documentation, how is PutIf different from PutIfExists and PutIfNotExists ?参考文档, PutIfPutIfExistsPutIfNotExists有何不同?

I suppose PutIfExists is like an update PutIfNotExists is like a new addition我想PutIfExists就像一个更新PutIfNotExists就像一个新的添加

What is PutIf?什么是 PutIf? When to use it?什么时候使用它?

PutIf can add conditions to update a record. PutIf可以添加条件来更新记录。 You can use PutIfExists when you want to just update without any other condition.当您只想在没有任何其他条件的情况下进行更新时,可以使用PutIfExists On the other hand, You have to use PutIfNotExists when you want to insert a new record without unexpected overwriting.另一方面,当您想要插入一条新记录而不发生意外覆盖时,您必须使用PutIfNotExists

For example, a table has a record as below.例如,一个表有如下记录。 We want to update the pass of a record if the score is greater than or equal to 60. (We assume that records have been inserted beforehand, and we have calculated the threshold (mean, median, etc.), then we decide the ID can pass the threshold or not.)如果分数大于等于60,我们要更新一条记录的pass 。(我们假设已经预先插入了记录,并且我们已经计算了阈值(均值,中位数等),然后我们决定ID可以通过阈值与否。)

|pID|cID|score| pass|
|  0|  0|   80|false|
|  1|  0|   45|false|

The following put will update the first record to set true in pass .以下put将更新第一条记录以在pass中设置为true

PartitionKey pk = new Key(new IntValue("pID", 0));
ClusteringKey ck = new Key(new IntValue("cID", 0));

PutIf condition = new PutIf(new ConditionalExpression("score", new IntValue(60), Operator.GTE));

Put put = new Put(pk, ck).withValue(new BooleanValue("pass", true)).withCondition(condition);

storage.put(put);

If we try to update the second record with the same condition, it won't be updated because the score is less than 60.如果我们尝试用相同的条件更新第二条记录,它不会被更新,因为分数小于 60。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM