DynamoDB AmazonServiceException:提供的关键元素与架构不匹配

时间:2016-11-11 12:53:26

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

所以someOne可以指导我为什么会得到这个例外。 以下是应用程序停止的例外bcz列表: -     11-11 17:54:22.724 5048-5171/com.creation.ceaggregation1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3 Process: com.creation.ceaggregation1, PID: 5048 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:304) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: com.amazonaws.AmazonServiceException: The provided key element does not match the schema (Service: AmazonDynamoDB; Status Code: 400; Error Code: ValidationException; Request ID: C8BTD5J7Q2477PSOK1F9G3FUTFVV4KQNSO5AEMVJF66Q9ASUAAJG) at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:712) at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:388) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:4181) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:1582) at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.doUpdateItem(DynamoDBMapper.java:1173) at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$2.executeLowLevelRequest(DynamoDBMapper.java:873) at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:1056) at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:904) at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:688) at com.creation.ceaggregation1.EnterUser$UpdateTable.doInBackground(EnterUser.java:64) at com.creation.ceaggregation1.EnterUser$UpdateTable.doInBackground(EnterUser.java:40) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  at java.lang.Thread.run(Thread.java:818)  11-11 17:54:24.769 5048-5171/com.creation.ceaggregation1 I/Process: Sending signal. PID: 5048 SIG: 9

继承我的映射器类: -

@DynamoDBTable(tableName = "agation1")
public class AWSDynamDBMapperClass {
String username;
String password;

@DynamoDBHashKey(attributeName="username")//username
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@DynamoDBAttribute(attributeName = "password")//password
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}
}

这是mainActivity代码: -     私有类UpdateTable扩展了AsyncTask {

    @Override
    protected String doInBackground(String... params) {
        String name = params[0];
        String password = params[1];

        try {
            AWSDynamoDBManagerClass manager = new     AWSDynamoDBManagerClass();
            CognitoCredentialsProvider credentialsProvider = manager.getCredentials(EnterUser.this);
            AWSDynamDBMapperClass mapperClass = new AWSDynamDBMapperClass();
            mapperClass.setUsername(name);
            mapperClass.setPassword(password);

           if (credentialsProvider != null && mapperClass != null) {
                DynamoDBMapper dynamoDBMapper = manager.initDynamoClient((CognitoCachingCredentialsProvider) credentialsProvider);
                dynamoDBMapper.save(mapperClass);//gives error over here

            } else {
                return "ss";
            }
            return "rr";

        } catch (DynamoDBMappingException ex) {
            ex.printStackTrace();
            return "exception";
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(result.equals("ss")){
            Toast.makeText(EnterUser.this, "Entered successfully :) ", Toast.LENGTH_LONG).show();
        }else if(result.equals("rr")){
            Toast.makeText(EnterUser.this, "Already Existing", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(EnterUser.this, "**Exception Caught**", Toast.LENGTH_SHORT).show();
        }
    }
}

2分区键,主键哈希键和排序键之间的区别是什么?

3请在此处建议是否需要AsyncTask。另请说明其优点和缺点(例如,如果可能,它会更好)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

1)这可能是因为表“agation1”没有将分区键设置为“username”或者它不是字符串

2)分区键是主键哈希+排序键的组合。两者一起唯一地标识一行。但是,您的表不必具有排序键。如果它没有排序键,则分区键与散列键相同。

3)你必须在另一个线程上进行保存调用,AsyncTask是一种方法。