DynamoDB:提供的键元素与架构不匹配 [英] DynamoDB : The provided key element does not match the schema
问题描述
是否有一种方法可以根据不是哈希键的字段获取项目?
Is there a way to get an item depending on a field that is not the hashkey?
示例
我的表格用户: id(哈希键),名称,电子邮件
我想检索具有以下内容的用户电子邮件为 test@mail.com
And I want to retrieve the user having email as 'test@mail.com'
该怎么做?
我尝试使用boto :
I try this with boto:
user = users.get_item(email='john.doe@gmail.com')
我收到以下错误:
'The provided key element does not match the schema'
推荐答案
要查询不是哈希键的字段,您需要使用全局二级索引(GSI)。看看此AWS Post 有关GSI的更多详细信息。
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 Post for more details on GSI's.
更新2015年2月:现在可以将GSI添加到现有表中了。请参阅 Amazon文档更多细节。
UPDATE Feb 2015: It is now possible to add a GSI to an existing table. See the Amazon Docs for more details.
可悲的是您无法将GSI添加到现有DynamoDB表,因此您需要创建一个新表并移植数据
Sadly you cannot add a GSI to an existing DynamoDB table so you'll need to create a new table and port your data if this is something you really need to query on.
问:如何为DynamoDB表创建全局二级索引?
与表关联的所有GSI必须在创建表
时指定。目前,在创建表
之后无法添加GSI。有关创建表及其
索引的详细步骤,请参见此处。您最多可以创建 5个全局二级
索引每桌。
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 indexes per table.
如果您不想移植数据,可以考虑创建第二个DynamoDB电子邮件作为哈希键的表,父记录的哈希表用作对主数据表的查找,但是您可以想象这并不是一个最佳的解决方案,并且它自己也很难保持同步与您的主表。
这篇关于DynamoDB:提供的键元素与架构不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!