使用 Python 从 DynamoDB 检索单个项目

Retrieve a single item from DynamoDB using Python

我想从 DynamoDB 中检索单个项目,但我无法这样做。我想我错过了什么。我可以使用 boto 的 scan() 函数扫描 table,但无法获取单个项目。所以如果有人能告诉我正确的方法就太好了。

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())

我试过使用以下方法:

results = table.scan(scan_filter={'email':'new'})

但是出现这个错误

boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'scan_filter' from 'scan_filter' is not recognized.

results = table.query_2(email = 'new')

boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'email' from 'email' is not recognized.

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'}

如果 table 哈希键是电子邮件,则

results = table.get_item(email='new') 应该可以。

检查 email 是否属于 table 方案中的 STRING 类型(而不是 NUMBER)。

试试这个:

results = table.query_2(email__eq = 'new')

您还可以使用以下内容:

__eq , __beginswith, __gte

感谢

@ing0: dynamodb row count via python, boto query

来源:DynamoDB2