有没有办法根据不是哈希键的字段来获取项目?
Is there a way to get an item depending on a field that is not the hashkey?
例子
我的表用户:id (HashKey)、姓名、电子邮件
我想检索电子邮件为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 帖子 有关 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 添加到现有表中.请参阅 亚马逊文档了解更多细节.
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 常见问题解答:
问:如何为 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:提供的关键元素与架构不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!