使用映射器保存DynamoDB时的ValidationException

时间:2016-04-05 13:45:19

标签: java amazon-web-services amazon-dynamodb

使用WCode将新项目(DynamoDBMapper)保存到DynamoDB实例时出现此错误:

Exception in thread "Thread-1" com.amazonaws.AmazonServiceException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 4OOMKU7NS1VD19EN988SUUM3U7VV4KQNSO5AEMVJF66Q9ASUAAJG)
  at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1369)
  at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:913)
  at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:631)
  at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:400)
  at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:362)
  at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:311)
  at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1966)
  at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:1780)
  at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.doUpdateItem(DynamoDBMapper.java:1110)
  at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$2.executeLowLevelRequest(DynamoDBMapper.java:806)
  at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:989)
  at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:835)
  at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:669)
  at com.wkt.cdmng.MessageAnalyzer.storeCode(MessageAnalyzer.java:220)
  at com.wkt.cdmng.MessageAnalyzer.run(MessageAnalyzer.java:156)
  at java.lang.Thread.run(Thread.java:745)

我访问它是与WCode类的关键属性相关的内容,但我无法真正了解哪一个以及如何搜索其他详细信息问题。

以下是WCode模型类,并且正确报告了所有属性名称:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import java.util.Date;

@DynamoDBTable(tableName = "CODES_DEV")
public class WCode {
  private String id;
  private String code;
  private String extra;

  @DynamoDBHashKey(attributeName = "ID")
  public String getId() {return id;}
  public void setId(String code) {id = "0|" + code + "|" + String.valueOf(new Date().getTime());}

  @DynamoDBIndexHashKey(globalSecondaryIndexName = "CD-index", attributeName = "CD")
  public String getCode() {return code;}
  public void setCode(String code) {this.code = code;}

  @DynamoDBAttribute(attributeName = "EX")
  public String getExtra() {return extra;}
  public void setExtra(String extra) {this.extra = extra;}
}

这是我用来存储项目的方法(storeCode):

private void storeCode(String code) {
  DynamoDBMapper mapper = awsDynamo.getMapper(); // singleton class I use to connect to DynamoDB
  WCode tmpCode = new WCode();
  tmpCode.setId(code);
  tmpCode.setCode(code);
  tmpCode.setExtra("");
  mapper.save(tmpCode);
}

我正在使用aws-java-sdk-dynamodbaws-java-sdk-sqs(后者用于项目的其他部分)版本1.10.58

有人可以指出我可能的解决方案或建议吗?

非常感谢!

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。 我使用batchSave函数而不是保存

解决了问题
mapper.batchSave(Arrays.asList(tmpCode));

它为我工作