使用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笔记本电脑
- 下一篇: MQTT和Java入门