使用Java更新DynamoDB项

在上一篇文章中,我们继续使用Java将项目插入DynamoDB。 DynamoDB还支持更新项目。

我们将使用Login表获取更新示例。
发布更新时,必须指定要更新的项目的主键。

public void updateName(String email,String fullName) {

        Map<String,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) {

        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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值