【问题标题】:Retrieve a single item from DynamoDB using Python使用 Python 从 DynamoDB 中检索单个项目
【发布时间】:2019-09-19 19:37:53
【问题描述】:

我想从 DynamoDB 中检索单个项目,但我无法做到。我想我错过了一些东西。我可以使用 boto 的 scan() 函数扫描表,但单个项目的获取不起作用。因此,如果有人能告诉我正确的方法,那就太好了。

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:无法识别来自“scan_filter”的运算符“scan_filter”。

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

boto.dynamodb2.exceptions.UnknownFilterTypeError:无法识别来自“电子邮件”的操作员“电子邮件”。

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

boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request {u'message': u'提供的关键元素与架构不匹配', u'__type': u'com.amazon.coral.validate#ValidationException'}

【问题讨论】:

  • 你的餐桌方案是什么? (哈希/范围键)
  • 你的哈希键是什么? (名称/类型)?

标签: python amazon-web-services amazon-dynamodb boto


【解决方案1】:

试试这个:

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

您还可以使用以下内容:

__eq , __beginswith, __gte

感谢

@ing0:dynamodb row count via python, boto query

来源:DynamoDB2

【讨论】:

    【解决方案2】:

    如果表哈希键是电子邮件,那么

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

    检查email 在表方案中是否为 STRING 类型(而不是 NUMBER)。

    【讨论】:

    • 我用过这个。你可以看看我的问题。该命令给出boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request
    • 您的电子邮件列类型是什么(如在 dynamo db 表中)?也许这就是原因。
    • 你会这样写:results = table.get_item(Key={'email': 'new'})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多