0
votes

I have created a DynamoDB table with following details:

enter image description here

and I'm trying to insert items in my table:

public static void insertItems() {
    AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager
            .ddb();
    DynamoDBMapper mapper = new DynamoDBMapper(ddb);

    try {
        UserPreference userPreference = new UserPreference();
        userPreference.setNavn("SalonSol");
        for (int i = 800; i <= 1600; i = i + 50) {

            userPreference.setTid(i);                
            userPreference.setMandag("Ledig");
            userPreference.setTirsdag("Ledig");
            userPreference.setOnsdag("Ledig");
            userPreference.setTorsdag("Ledig");
            userPreference.setFredag("Ledig");
            userPreference.setLørdag("Ledig");
            userPreference.setSøndag("Ledig"); 

            Log.d(TAG, "Inserting Tid and Dage");
            mapper.save(userPreference);
            Log.d(TAG, "Tid and Dage inserted");               
        }            
    } catch (AmazonServiceException ex) {
        Log.e(TAG, "Error inserting users");
        UserPreferenceDemoActivity.clientManager
                .wipeCredentialsOnAuthError(ex);
    }
}

But AWS keeps returning following exception:

AmazonserviceException: The provided key element does not match the schema
Status Code: 400

I'm actually inserting a String value as a Hash Key and int values as range, so I don't really understand why I'm getting this exception.

My definition of UserPreference class:

@DynamoDBTable(tableName = Constants.TEST_TABLE_NAME)
public static class UserPreference {
    private String Navn;
    private int Tid;
    private String Mandag;
    private String Tirsdag;
    private String Onsdag;
    private String Torsdag;
    private String Fredag;
    private String Lørdag;
    private String Søndag;        



    @DynamoDBHashKey(attributeName = "Navn")
    public String getNavn() {
        return Navn;
    }

    public void setNavn(String Navn) {
        this.Navn = Navn;
    }

    @DynamoDBRangeKey(attributeName = "Tid")
    public int getTid() {
        return Tid;
    }

    public void setTid(int Tid) {
        this.Tid = Tid;
    }

    @DynamoDBAttribute(attributeName = "Mandag")
    public String getMandag() {
        return Mandag;
    }

    public void setMandag(String Mandag) {
        this.Mandag = Mandag;
    }

    @DynamoDBAttribute(attributeName = "Tirsdag")
    public String getTirsdag() {
        return Tirsdag;
    }

    public void setTirsdag(String Tirsdag) {
        this.Tirsdag = Tirsdag;
    }

    @DynamoDBAttribute(attributeName = "Onsdag")
    public String getOnsdag() {
        return Onsdag;
    }

    public void setOnsdag(String Onsdag) {
        this.Onsdag = Onsdag;
    }

    @DynamoDBAttribute(attributeName = "Torsdag")
    public String getTorsdag() {
        return Torsdag;
    }

    public void setTorsdag(String Torsdag) {
        this.Torsdag = Torsdag;
    }

    @DynamoDBAttribute(attributeName = "Fredag")
    public String getFredag() {
        return Fredag;
    }

    public void setFredag(String Fredag) {
        this.Fredag = Fredag;
    }

    @DynamoDBAttribute(attributeName = "Lørdag")
    public String getLørdag() {
        return Lørdag;
    }

    public void setLørdag(String Lørdag) {
        this.Lørdag = Lørdag;
    }

    @DynamoDBAttribute(attributeName = "Søndag")
    public String getSøndag() {
        return Søndag;
    }

    public void setSøndag(String Søndag) {
        this.Søndag = Søndag;
    }   

}

and the table name has following definition in the "Constants" class:

public static final String TEST_TABLE_NAME = "EkstraTable";
2
I am not sure whether it will help but please change type of the Tid from int to Integer. Are you sure that you are using proper credentials? - baju
It still returns the same exception. I have created the table using an another method inside the application, so the credentials information should be fine. - Osman Esen
Hm, are you connecting to the same region in which you created the table? Otherwise is seems that is should work - baju
When I'm dropping the range, and only using hash as "Tid" Integer, it works perfectly. - Osman Esen
What version of the SDK are you using? It is possible that this could have been a bug with the mapper and static nested classes. - mkobit

2 Answers

0
votes

I tried to reproduce the problem by copying and pasting your code then change it to write to my own test table. The problem did not occur, I successfully put all items into the table as per your code:

http://i.imgur.com/lIPgn7P.png

I would suggest checking to make sure you have the newest version of the AWS Java SDK, and that the table was created correctly (I created mine through the console to be safe, you can try that if code still doesn't work after upgrading).

0
votes

I found the issue. I did a huge mistake by importing:

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey

instead of:

com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBRangeKey

for @DynamoDBRangeKey annotation since I'm using Android mobile SDK. I found the issue by downloading and adding the DynamoDBMapper.jar for 2.1.8, and this version shows the

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey

as deprecated.