使用映射器在Dynamo Db中进行条件更新

时间:2017-04-16 15:29:00

标签: java amazon-dynamodb

我正在尝试根据要更新的标记大于当前分数的条件对表格中的字段分数进行条件更新。

我写了以下代码,

StudentDO temp = mapper.load(StudentDO.class, student.getId());
temp.setScore(marks);
ArrayList<AttributeValue> list = new ArrayList<AttributeValue>();
            list.add(new AttributeValue().withN(Oldmarks.toString()));

            Map<String,ExpectedAttributeValue> expectedAttributes = new 
                                                                                                                   HashMap<String,ExpectedAttributeValue>();

            ExpectedAttributeValue expectedAttributeValue = new 
                                                                                                     ExpectedAttributeValue().withAttributeValueList(list)
                                                                                                                                       .withComparisonOperator("GT");

            expectedAttributes.put("Score",expectedAttributeValue);
            mapper.save(temp, new DynamoDBSaveExpression().withExpected(expectedAttributes));

此运行时异常

失败

com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException:条件请求失败(服务:null;状态代码:400;错误代码:ConditionalCheckFailedException;请求ID:null)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.amazonaws.services.dynamodbv2.local.embedded.DDBExceptionMappingInvocationHandler.handleDynamoDBLocalServiceException(DDBExceptionMappingInvocationHandler.java:87)
at com.amazonaws.services.dynamodbv2.local.embedded.DDBExceptionMappingInvocationHandler.invoke(DDBExceptionMappingInvocationHandler.java:66)
at com.sun.proxy.$Proxy19.updateItem(Unknown Source)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.doUpdateItem(DynamoDBMapper.java:854)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$2.executeLowLevelRequest(DynamoDBMapper.java:594)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:733)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:623)
at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.save(AbstractDynamoDBMapper.java:128)

我无法调试为什么它会抛出条件检查失败的异常。 我可以看出请求没有正确形成,但不确定它在哪里格格不入?

我试图将它与这些文档对齐 http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBSaveExpression.html

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/model/ExpectedAttributeValue.html

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/model/AttributeValue.html

我的数据库架构是这个

@DynamoDBTable(tableName = "Student")

public class StudentDO {
    @DynamoDBHashKey(attributeName = "Id")
    private Integer id;

    @DynamoDBAttribute(attributeName = "Name")
    private String name;

    @DynamoDBAttribute(attributeName = "Score")
    private Integer score;

    @Override
    public String toString() {

        return "Student [Name=" + name + ", id=" + id + ", score=" + score + "]";
    }


}

1 个答案:

答案 0 :(得分:0)

每个DynamoDB属性必须具有一个公共的getter和setter,因为DynamoDBMapper在JavaBeans上工作(一个没有参数构造函数的类,每个私有var的getters + setters)。将您的课程修改为此:

tasks:
 - name: Restart apache service
   service: name=apache2 state=restarted
   become: yes
   become_user: root