代码之家  ›  专栏  ›  技术社区  ›  Kiran

DynamoDB异常元素与架构不匹配

  •  0
  • Kiran  · 技术社区  · 7 年前

    我想用更新现有令牌项 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 回复  |  直到 7 年前
        1
  •  0
  •   Matthew Pope    7 年前

    您缺少您的计算机中的范围键 UpdateItemSpec . 你需要使用 withPrimaryKey(String hashKeyName, Object hashKeyValue, String rangeKeyName, Object rangeKeyValue) 而不是只接受哈希键的方法。也, type 是一个 reserved word in DynamoDB ,因此需要将其添加到名称映射中。

    修正的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 */));
    
        2
  •  0
  •   tsingh    7 年前

    DynamoDBMapper dyanmoDbMapper=新的DynamoDBMapper(客户端,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);