Python DynamoDB:提供的关键元素与架构不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25886403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DynamoDB : The provided key element does not match the schema
提问by
Is there a way to get an item depending on a field that is not the hashkey?
有没有办法根据不是哈希键的字段获取项目?
Example
例子
My Table Users: id (HashKey), name, email
我的表用户: id (HashKey), name, email
And I want to retrieve the user having email as '[email protected]'
我想检索电子邮件为“[email protected]”的用户
How this can be done?
如何做到这一点?
I try this with boto:
我用 boto 试试这个:
user = users.get_item(email='[email protected]')
I get the following error:
我收到以下错误:
'The provided key element does not match the schema'
采纳答案by Wolfwyrd
To query on fields which are not the hash key you need to use a Global Secondary Index (GSI). Take a look at this AWS Postfor more details on GSI's.
要查询不是哈希键的字段,您需要使用全局二级索引 (GSI)。有关 GSI 的更多详细信息,请查看此 AWS 帖子。
UPDATE Feb 2015:It is now possible to add a GSI to an existing table. See the Amazon Docsfor more details.
2015 年 2 月更新:现在可以将 GSI 添加到现有表中。有关更多详细信息,请参阅亚马逊文档。
Sadly you cannot add a GSI to an existing DynamoDB tableso you'll need to create a new table and port your data if this is something you really need to query on.
遗憾的是,您无法将 GSI 添加到现有的 DynamoDB 表中,因此如果您确实需要查询数据,则需要创建一个新表并移植数据。
From the DynamoDB FAQ:
Q: How do I create a global secondary index for a DynamoDB table?
All GSIs associated with a table must be specified at table creation time. At this time, it is not possible to add a GSI after the table has been created. For detailed steps on creating a Table and its indexes, see here. You can create a maximum of 5 global secondary indexesper table.
问:如何为 DynamoDB 表创建全局二级索引?
必须在创建表时指定与表关联的所有 GSI。此时,无法在创建表后添加 GSI。有关创建表及其索引的详细步骤,请参见此处。每个表最多可以创建5 个全局二级索引。
If you do not want to port your data you could consider creating a second DynamoDB table with the email as a hash key and the hash of the parent record to use as a lookup into your main data table but as you can imagine this isn't exactly an optimal solution and it comes with its own headaches of keeping it in synch with your master table.
如果您不想移植数据,您可以考虑创建第二个 DynamoDB 表,其中电子邮件作为哈希键,父记录的哈希用作主数据表的查找,但您可以想象这不是完全是一个最佳解决方案,它带来了与主表保持同步的问题。
回答by NotoriousWebmaster
I also got this error when I was sending a string instead of an integer.
当我发送一个字符串而不是一个整数时,我也遇到了这个错误。
Of course, this was when I was writing to the database, rather than reading from.
当然,这是我写入数据库而不是读取的时候。
回答by Sean
The following applies to the Node.js AWS SDK in the AWS Lambda environment:
以下适用于 AWS Lambda 环境中的 Node.js AWS 开发工具包:
This was a rough one for me. I ran into this problem when trying to use the getItem method. No matter what I tried I would continue to receive this error. I finally found a solution on the AWS forum: https://forums.aws.amazon.com/thread.jspa?threadID=208820
这对我来说是艰难的。我在尝试使用 getItem 方法时遇到了这个问题。无论我尝试什么,我都会继续收到此错误。终于在AWS论坛上找到了解决办法:https: //forums.aws.amazon.com/thread.jspa?threadID =208820
Inexplicably, the apparent solution conflicts with all AWS documentation that I can find.
令人费解的是,明显的解决方案与我能找到的所有 AWS 文档冲突。
Here is the code which worked for me:
这是对我有用的代码:
var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();
var params = { }
params.TableName = "ExampleTable";
var key = { "ExampleHashKey": "1" };
params.Key = key;
dynamo.getItem(params, function(err, data) {
if (err)
console.log(err);
else
console.log(data)
});
回答by Ryan Shillington
I got this error in Java because I had used the @DynamoDBHashKeyannotation for a RANGE key. I had to use the @DynamoDBRangeKeyannotation for my object's id instead.
我在 Java 中遇到此错误,因为我使用@DynamoDBHashKey了 RANGE 键的注释。我不得不@DynamoDBRangeKey为我的对象的 id使用注释。
回答by Josh
I got this error in my Java application, because I accidentally had two Range keys annotated (@DynamoDBRangeKey) in my DAO class, when there should only be one. I changed the new addition's annotation to @DynamoDBAttribute, and that solved it.
我在 Java 应用程序中遇到此错误,因为我不小心@DynamoDBRangeKey在 DAO 类中注释了两个 Range 键 ( ),而本应只有一个。我将新添加的注释更改为 @ DynamoDBAttribute,并解决了它。

