使用Java更新DynamoDB项
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                使用Java更新DynamoDB项
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                在上一篇文章中,我們繼續使用Java將項目插入DynamoDB。 DynamoDB還支持更新項目。
 我們將使用Login表獲取更新示例。 
 發布更新時,必須指定要更新的項目的主鍵。 
我們可以使用條件更新來處理更高級的語句。 有條件的更新可以在很多情況下為我們提供幫助,例如處理并發更新。
我們可以通過使用普通表達式來實現。
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);}另一個功能是原子計數器。 我們可以發布DynamoDB項目的更新并增加屬性值。 我們將添加一個額外的字段,稱為count。 另外,我們將添加另一個更新功能。 一旦調用,該函數將更新指定的字段,但也會增加計數器屬性。 因此,counter屬性將表示對特定項目執行了多少次更新。
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
總結
以上是生活随笔為你收集整理的使用Java更新DynamoDB项的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 教你DIY一台笔记本如何diy笔记本电脑
- 下一篇: 如何在Mac电脑中压缩图片苹果电脑如何压
