3

I have this error while saving a new item (WCode) into a DynamoDB instance by using DynamoDBMapper:

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)

I guest it's something related to the key attributes on the WCode class but I can't really understand which one and how to search for additional details problem.

The following is the WCode model class and all the attributes names are correctly reported:

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;}
}

This is the method I use to store the item (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);
}

I'm using aws-java-sdk-dynamodb and aws-java-sdk-sqs (the latter for another part of the project) version 1.10.58.

Does someone can point me to a possible solution or suggestion?

Thank you very much!

6
  • Have you compared the keynames of WCode class with the keynamens in your dynamodb in aws? As I remember they are case sensitive Commented Apr 5, 2016 at 13:49
  • You're right; I did, they seem to be corrected. Commented Apr 5, 2016 at 13:51
  • No I mean you're right, it's case sensitive. I already checked the names match and there is no difference, they are correct. Commented Apr 5, 2016 at 14:05
  • I haven't used the mapper, I'm using my own way over the low level API. But I checked my DynamoDB Table definitions and saw that I use camel case and no hyphens. Possible this is your problem. Because the error is protocol related, your code already send a create request to dynamodb and the result is an error saying a key mismatch, which could only be a mismatch between your table key definitions and used keys in your entity class. Commented Apr 5, 2016 at 14:20
  • I use a hypen only in globalSecondaryIndexName but that's the name; anyway, I tried to change the specified names of the attributes into this (id, cdIndex, cd, ex) but I got the same error; I also tried to change the name of the table to codesDev but I obtained the ResourceNotFoundException. Commented Apr 5, 2016 at 14:50

1 Answer 1

4

I had the same problem. I've resolved using batchSave function instead of save:

mapper.batchSave(Arrays.asList(tmpCode));

It worked for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.