[英]Retrieve a single item from DynamoDB using Python
I want to retrieve single item from DynamoDB but I'm unable to do that. 我想从DynamoDB检索单个项目,但是我无法做到这一点。 I think I am missing something. 我想我缺少了一些东西。 I am able to scan the table using scan() function of boto but the fetching of a single item is not working. 我可以使用boto的scan()函数扫描表,但无法获取单个项目。 So it would be great if someone can tell me the correct way to do this. 因此,如果有人可以告诉我正确的方法,那就太好了。
models.py file: models.py文件:
import os
import boto
from boto import dynamodb2
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, RangeKey, KeysOnlyIndex, GlobalAllIndex
AWS_ACCESS_KEY_ID = '****************'
AWS_SECRET_ACCESS_KEY = '***************************'
REGION = 'us-east-1'
TABLE_NAME = 'Users'
conn = dynamodb2.connect_to_region(REGION, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
table = Table(
TABLE_NAME,
connection=conn
)
results = table.scan()
for dynamo_item in results:
print dict(dynamo_item.items())
I've tried using following: 我尝试使用以下方法:
results = table.scan(scan_filter={'email':'new'})
but got this error 但是得到这个错误
boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'scan_filter' from 'scan_filter' is not recognized. boto.dynamodb2.exceptions.UnknownFilterTypeError:无法识别来自“ scan_filter”的运算符“ scan_filter”。
results = table.query_2(email = 'new')
boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'email' from 'email' is not recognized. boto.dynamodb2.exceptions.UnknownFilterTypeError:无法识别来自“电子邮件”的运算符“电子邮件”。
results = table.get_item(email='new')
boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request {u'message': u'The provided key element does not match the schema', u'__type': u'com.amazon.coral.validate#ValidationException'} boto.dynamodb2.exceptions.ValidationException:ValidationException:400错误的请求{u'message':u'提供的键元素与架构不匹配,u'__ type':u'com.amazon.coral.validate#ValidationException'}
Try this: 尝试这个:
results = table.query_2(email__eq = 'new')
You can also use the following: 您还可以使用以下内容:
__eq , __beginswith, __gte
Thanks to 谢谢
@ing0: dynamodb row count via python, boto query @ ing0: 通过python,boto查询的dynamodb行计数
If the table hash key is email then 如果表哈希键是电子邮件,则
results = table.get_item(email='new')
should work. results = table.get_item(email='new')
应该可以工作。
Check if email
is in type STRING (and not NUMBER) in the table scheme. 检查表方案中email
是否为STRING类型(而不是NUMBER)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.