dynamodb java_使用Java更新DynamoDB项
生活随笔
收集整理的這篇文章主要介紹了
dynamodb java_使用Java更新DynamoDB项
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
dynamodb java
在上一篇文章中,我們繼續(xù)使用Java將項目插入DynamoDB。 DynamoDB還支持更新項目。
我們將使用Login表獲取更新示例。
發(fā)布更新時,必須指定要更新的項目的主鍵。
我們可以使用條件更新來處理更高級的語句。 有條件的更新可以在許多情況下為我們提供幫助,例如處理并發(fā)更新。
我們可以通過使用普通表達(dá)式來實現(xiàn)。
public void updateConditionallyWithExpression(String email,String fullName,String prefix) {Map<String, AttributeValue> key = new HashMap<>();key.put("email", new AttributeValue().withS(email));Map<String, AttributeValue> attributeValues = new HashMap<>();attributeValues.put(":prefix", new AttributeValue().withS(prefix));attributeValues.put(":fullname", new AttributeValue().withS(fullName));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).withUpdateExpression("set fullname = :fullname").withConditionExpression("begins_with(fullname,:prefix)").withExpressionAttributeValues(attributeValues);UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}或通過指定屬性。
public void updateConditionallyWithAttributeEntries(String email, String fullName, String prefix){Map<String,AttributeValue> key = new HashMap<>();key.put("email",new AttributeValue().withS(email));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).addAttributeUpdatesEntry("fullname",new AttributeValueUpdate().withValue(new AttributeValue().withS(fullName)).withAction(AttributeAction.PUT)).addExpectedEntry("fullname",new ExpectedAttributeValue().withValue(new AttributeValue().withS(prefix)).withComparisonOperator(ComparisonOperator.BEGINS_WITH));UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}另一個功能是原子計數(shù)器。 我們可以發(fā)布DynamoDB項目的更新并增加屬性值。 我們將添加一個額外的字段,稱為count。 另外,我們將添加另一個更新功能。 一旦調(diào)用,該函數(shù)將更新指定的字段,但也會增加計數(shù)器屬性。 因此,counter屬性將表示對特定項目執(zhí)行了多少次更新。
public void addUpdateCounter(String email) {Map<String,AttributeValue> key = new HashMap<>();key.put("email",new AttributeValue().withS(email));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).addAttributeUpdatesEntry("counter",new AttributeValueUpdate().withValue(new AttributeValue().withN("0")).withAction(AttributeAction.PUT));UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}public void updateAndIncreaseCounter(String email,String fullname) {Map<String,AttributeValue> key = new HashMap<>();key.put("email",new AttributeValue().withS(email));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).addAttributeUpdatesEntry("fullname",new AttributeValueUpdate().withValue(new AttributeValue().withS(fullname)).withAction(AttributeAction.PUT)).addAttributeUpdatesEntry("counter",new AttributeValueUpdate().withValue(new AttributeValue().withN("1")).withAction(AttributeAction.ADD));UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}您可以在github上找到源代碼。
翻譯自: https://www.javacodegeeks.com/2016/08/update-dynamodb-items-java.html
dynamodb java
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的dynamodb java_使用Java更新DynamoDB项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑qq分身版(qq分身版)
- 下一篇: apache camel_使用Apach