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

Posted

技术标签:

【中文标题】DynamoDB AmazonServiceException:提供的关键元素与架构不匹配【英文标题】:DynamoDB AmazonServiceException: The provided key element does not match the schema 【发布时间】:2016-11-11 12:53:26 【问题描述】:

所以有人可以指导我为什么会遇到此异常。 以下是应用程序已停止的异常 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】:

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

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

3) 您必须在不同的线程上进行保存调用,AsyncTask 是一种方法。

【讨论】:

以上是关于DynamoDB AmazonServiceException:提供的关键元素与架构不匹配的主要内容,如果未能解决你的问题,请参考以下文章

如何为用户 id 等于 DynamoDB 表名的 DynamoDB 创建策略?

使用 AWS Java DynamoDB 流 Kinesis 适配器处理 DynamoDB 流

AWS Appsync 一个 dynamodb 解析器中的多个 dynamodb 请求

dynamodb 查询我试图使用 laravel 从 dynamodb 获取一些数据

使用 spring-data-dynamodb 在 GSI 中查询 dynamoDB 中的 id 列表

使用 EMR 查询 DynamoDB 数据