上海网站怎么备案,做赚钱问卷调查的网站好,wordpress 相关文章 插件,全球采购在上一篇文章中#xff0c;我们继续使用Java将项目插入DynamoDB。 DynamoDB还支持更新项目。 我们将使用Login表获取更新示例。 发布更新时#xff0c;必须指定要更新的项目的主键。 public void updateName(String email,String fullName) {MapString,AttributeValue… 在上一篇文章中我们继续使用Java将项目插入DynamoDB。 DynamoDB还支持更新项目。 我们将使用Login表获取更新示例。 发布更新时必须指定要更新的项目的主键。 public void updateName(String email,String fullName) {MapString,AttributeValue attributeValues new HashMap();attributeValues.put(email,new AttributeValue().withS(email));attributeValues.put(fullname,new AttributeValue().withS(fullName));UpdateItemRequest updateItemRequest new UpdateItemRequest().withTableName(TABLE_NAME).addKeyEntry(email,new AttributeValue().withS(email)).addAttributeUpdatesEntry(fullname,new AttributeValueUpdate().withValue(new AttributeValue().withS(fullName)));UpdateItemResult updateItemResult amazonDynamoDB.updateItem(updateItemRequest);} 我们可以使用条件更新来处理更高级的语句。 有条件的更新可以在很多情况下为我们提供帮助例如处理并发更新。 我们可以通过使用普通表达式来实现。 public void updateConditionallyWithExpression(String email,String fullName,String prefix) {MapString, AttributeValue key new HashMap();key.put(email, new AttributeValue().withS(email));MapString, 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){MapString,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) {MapString,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) {MapString,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