DynamoDB异常元素与架构不匹配

时间:2018-12-12 03:34:00

标签: java amazon-dynamodb

我想用last_accessed_on列(新的)更新现有令牌项目。我尝试了以下失败的代码。
请指出我这里有什么问题。

//dynamoDB is initialized with config and it is good.
Table  table = dynamoDB.getTable("Token");
UpdateItemSpec  updateItemSpec = new UpdateItemSpec()
                .withPrimaryKey("GUID", "guidHashed")
                .withUpdateExpression("set #name = :val1")
                .withNameMap(new NameMap().with("#name", "last_accessed_on"))
                .withValueMap(new ValueMap().withLong(":val1", System.currentTimeMillis()));

下面是DTO对象。

@DynamoDBTable(tableName = "Token")
public class TokenItem {

    @DynamoDBHashKey(attributeName = "GUID")
    private String guid;

    @DynamoDBRangeKey(attributeName = "type")
    private String type;

    @DynamoDBAttribute(attributeName = "last_accessed_on")
    private long lastAccessedOn;

    //getter and setters
}

错误:

com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: FNEPHSAEMVJF66Q9ASUAAJG)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1660)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1324)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1074)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:745)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:719)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:701)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:669)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:651)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:515)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:3768)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3737)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeUpdateItem(AmazonDynamoDBClient.java:3448)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:3416)
    at com.amazonaws.services.dynamodbv2.document.internal.UpdateItemImpl.doUpdateItem(UpdateItemImpl.java:102)
    at com.amazonaws.services.dynamodbv2.document.internal.UpdateItemImpl.updateItem(UpdateItemImpl.java:86)
    at com.amazonaws.services.dynamodbv2.document.Table.updateItem(Table.java:225)

2 个答案:

答案 0 :(得分:0)

您尝试过吗 DynamoDBMapper dyanmoDbMapper =新的DynamoDBMapper(client,yourConfig);

DynamoDBQueryExpression<Token> qExp = new DynamoDBQueryExpression<Timesheet>()
            .withKeyConditionExpression(" GUID =:GUID  and type =:type")
            .withExpressionAttributeValues(mapOfGuiAndTypeValues).withScanIndexForward(false)
            .withConsistentRead(false);


QueryResultPage< Token> queryPage= dyanmoDbMapper.queryPage(Token.class, qExp);

一旦检索到对象,然后使用上次访问值更新并保存

dyanmoDbMapper.save(updatedObject);

答案 1 :(得分:0)

您缺少UpdateItemSpec中的范围键。您需要使用withPrimaryKey(String hashKeyName, Object hashKeyValue, String rangeKeyName, Object rangeKeyValue)来代替仅接受哈希键的方法。另外,typereserved word in DynamoDB,因此您需要将其添加到NameMap。

固定的UpdateItemSpec就是这样。

Table  table = dynamoDB.getTable("Token");
UpdateItemSpec  updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("GUID", ":guid", "#type", ":type")
            .withUpdateExpression("set #name = :val1")
            .withNameMap(new NameMap()
                    .with("#name", "last_accessed_on")
                    .with("#type", "type"))
            .withValueMap(new ValueMap()
                    .withLong(":val1", System.currentTimeMillis())
                    .withString(":guid", /* put the guid here */)
                    .withString(":type", /* put the type here */));