2014-01-16 22 views
2

當且僅當內存計數器的值大於存儲在DynamoDB中的的時,我需要執行條件更新。DynamoDB是否允許對「=」以外的操作符進行條件更新?

示例 - 假設內存中計數器的值爲30.存儲在DynamoDB中的計數器值爲25.在1個條件操作中,我想將DynamoDB值設置爲30(因爲25是舊值)

文檔具有以下 -

Expected allows you to provide an attribute name, and whether or not Amazon DynamoDB should check to see if the attribute value already exists; or if the attribute value exists and has a particular value before changing it. 

它明確指出,除非你知道舊的價值,你無法進行有條件的操作。

是否有解決方法,我可以在'大於'上執行條件更新?

任何幫助表示讚賞。

謝謝。

回答

0

你不能用documentation中的「原子操作」來做到這一點。

如果你不需要滿原子性,你可能會實現自己的「跨國」的方法或使用one from amazon(Java)的

+0

感謝您將我指向DynamoDB事務。在未來,我希望DynamoDB也支持其他運營商的條件更新。 – Vijay

0

DynamoDB不支持其他運營商的條件更新。對於您在此處討論的情況,您可以在要評估的屬性字段上指定條件運算符,並在條件「LT」(小於)內存計數器的值上指定條件運算符。在你的情況,屬性「myCounterName」小於30

在Java(僞):

ExpectedAttributeValue expectedValue = new ExpectedAttributeValue() 
    .withComparisonOperator(ComparisonOperator.LT) 
    .withAttributeValue(new AttributeValue().withN("30"); 

UpdateItemRequest request = new UpdateItemRequest(...); 
request.addExpectedEntry("myCounterName", expectedValue); 

dbClient.updateItem(request); 

如果DynamoDB當前計數器小於您指定的值這個代碼將只更新計數器。

相關問題