1

I'm new to DynamoDB, I'm trying to insert a new item. However, I'm getting the following exception:

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: XXX)

This is how my table is described:

{
    "Table": {
        "TableArn": "arn:aws:dynamodb:us-east-1:111:table/table-XXX", 
        "AttributeDefinitions": [
            {
                "AttributeName": "timestamp", 
                "AttributeType": "S"
            }, 
            {
                "AttributeName": "title", 
                "AttributeType": "S"
            }
        ], 
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0, 
            "WriteCapacityUnits": 5, 
            "ReadCapacityUnits": 5
        }, 
        "TableSizeBytes": 0, 
        "TableName": "ddb-table-sst67gy", 
        "TableStatus": "ACTIVE", 
        "KeySchema": [
            {
                "KeyType": "HASH", 
                "AttributeName": "title"
            }, 
            {
                "KeyType": "RANGE", 
                "AttributeName": "timestamp"
            }
        ], 
        "ItemCount": 0, 
        "CreationDateTime": 1489090172.658
    }
}

And this is my Java class:

@DynamoDBTable(tableName = "table-XXX")
public class Movie {

    private String title;
    private String timeStamp;

    @DynamoDBHashKey(attributeName = "title")
    @NotNull(message = "Title must not be empty")
    public String getTitle() {
        return title;
    }

    public Movie withTitle(String name) {

        setTitle(name);
        return this;
    }

    @DynamoDBAttribute(attributeName = "timestamp")
    public String getTimeStamp() {
        return timeStamp;
    }

    public Movie withTimeStamp(String address) {
        setTimeStamp(address);
        return this;
    }


    public void setTitle(String title) {
        this.title = title;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Movie movie = (Movie) o;

        if (title != null ? !title.equals(movie.title) : movie.title != null) return false;
        return timeStamp != null ? timeStamp.equals(movie.timeStamp) : movie.timeStamp == null;
    }

    @Override
    public int hashCode() {
        int result = title != null ? title.hashCode() : 0;
        result = 31 * result + (timeStamp != null ? timeStamp.hashCode() : 0);
        return result;
    }
}

How should I map my Java class into DynamoDB correctly ?

1
  • 1
    could you add @DynamoDBRangeKey(attributeName="timestamp") on your * timeStamp* field getter and try again.In your case unique primary key made up of both the hash and the range key but you missed telling which is your range key in your code. Commented Mar 14, 2017 at 3:19

1 Answer 1

1

Please use annotation @DynamoDBRangeKey to define the range or sort key attribute.

@DynamoDBRangeKey(attributeName = "timestamp")
public String getTimeStamp() {
    return timeStamp;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I've just added solution in my comment :)

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.