如何在 python 中查询 AWS DynamoDB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34171563/
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
How do I query AWS DynamoDB in python?
提问by altoids
I'm fairly new to NoSQL and using AWS DynamoDB. I'm calling it from AWS Lambda using python 2.7
I'm trying to retrieve a value from an 'order_number' field.
This is what my table looks like(only have one record.):
我对 NoSQL 和使用 AWS DynamoDB 还很陌生。我正在使用 python 2.7 从 AWS Lambda 调用它我试图从“order_number”字段中检索一个值。
这是我的表的样子(只有一条记录。):
primary partition key: subscription_id
and my secondary global index: order_number
主分区键:subscription_id
和我的辅助全局索引:order_number
Is my setup correct?
If so given the order_number how do I retrieve the record using python?
I can't figure out the syntax to do it.
I've tried
我的设置正确吗?如果是这样,给定 order_number 我如何使用 python 检索记录?
我无法弄清楚这样做的语法。
我试过了
response = table.get_item( Key = {'order_number': myordernumber} )
But i get:
An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema: ClientError
但我得到:
调用 GetItem 操作时发生错误(ValidationException):提供的关键元素与架构不匹配:ClientError
采纳答案by garnaat
DynamoDB does not automatically index all of the fields of your object. By default you can define a hash key (subscription_id
in your case) and, optionally, a range key and those will be indexed. So, you could do this:
DynamoDB 不会自动索引对象的所有字段。默认情况下,您可以定义一个散列键(subscription_id
在您的情况下),并且可以选择定义一个范围键,这些键将被索引。所以,你可以这样做:
response = table.get_item(Key={'subscription_id': mysubid})
and it will work as expected. However, if you want to retrieve an item based on order_number
you would have to use a scan
operation which looks through all items in your table to find the one(s) with the correct value. This is a very expensive operation. Or you could create a Global Secondary Index in your table that uses order_number
as the primary key. If you did that and called the new index order_number-index
you could then query for objects that match a specific order number like this:
它会按预期工作。但是,如果您想检索基于order_number
您的项目,则必须使用一种scan
操作来查看表中的所有项目以找到具有正确值的项目。这是一项非常昂贵的操作。或者您可以在您的表中创建一个全局二级索引order_number
作为主键。如果您这样做并调用新索引order_number-index
,则可以查询与特定订单号匹配的对象,如下所示:
from boto3.dynamodb.conditions import Key, Attr
response = table.query(
IndexName='order_number-index',
KeyConditionExpression=Key('order_number').eq(myordernumber))
DynamoDB is an very fast, scalable, and efficient database but it does require a lot of thought about what fields you might want to search on and how to do that efficiently.
DynamoDB 是一个非常快速、可扩展且高效的数据库,但它确实需要仔细考虑您可能想要搜索哪些字段以及如何有效地进行搜索。
The good news is that now you can add GSI's to an existing table. Previously you would have had to delete your table and start all over again.
好消息是现在您可以将 GSI 添加到现有表中。以前,您必须删除表并重新开始。
回答by user3303554
Make sure you've imported this:
确保你已经导入了这个:
from boto3.dynamodb.conditions import Key, Attr
If you don't have it, you'll get the error for sure. It's in the documentation examples.
如果你没有它,你肯定会得到错误。它在文档示例中。
Thanks @altoids for the comment above as this is the correct answer for me. I wanted to bring attention to it with a "formal" answer.
感谢@altoids 的上述评论,因为这对我来说是正确的答案。我想用一个“正式”的答案来引起人们的注意。