DynamoDB2和Boto 2.9.5批量查询和单项获取的错误请求

时间:2013-05-30 08:22:24

标签: boto amazon-dynamodb

使用DynamoDB2 API

使用boto 2.9.5进行任何单个或批量查询时遇到问题

我需要像这样进行批量查询:

one_org = Table('[table-name]').batch_get(keys=[
        {'key': '[user-id-hash]'},
        {'key': '[user-id-hash]'},
        {'key': '[user-id-hash]'},
        {'key': '[user-id-hash]'},
    ])

for user in one_org:
    for key, value in user.items():
        print key, value

我一直得到这个例外:

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

鉴于此消息,我认为密钥的名称存在问题,但我们的密钥称为密钥,因此对我没有任何意义。

我在下面包含了堆栈跟踪:

Traceback (most recent call last):
  File "aws/interfaces.py", line 38, in <module>
    for user in one_org:
  File "/home/kasper/Falcon/thenest/venv/local/lib/python2.7/site-packages/boto/dynamodb2/results.py", line 59, in next
    self.fetch_more()
  File "/home/kasper/Falcon/thenest/venv/local/lib/python2.7/site-packages/boto/dynamodb2/results.py", line 141, in fetch_more
    results = self.the_callable(*args, **kwargs)
  File "/home/kasper/Falcon/thenest/venv/local/lib/python2.7/site-packages/boto/dynamodb2/table.py", line 949, in _batch_get
    raw_results = self.connection.batch_get_item(request_items=items)
  File "/home/kasper/Falcon/thenest/venv/local/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 152, in batch_get_item
    body=json.dumps(params))
  File "/home/kasper/Falcon/thenest/venv/local/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 1479, in make_request
    retry_handler=self._retry_handler)
  File "/home/kasper/Falcon/thenest/venv/local/lib/python2.7/site-packages/boto/connection.py", line 852, in _mexe
    status = retry_handler(response, i, next_sleep)
  File "/home/kasper/Falcon/thenest/venv/local/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 1518, in _retry_handler
    response.status, response.reason, data)
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'}

2 个答案:

答案 0 :(得分:11)

今天早上我遇到了同样的问题。如果已在架构中定义了RangeKey,则还需要指定它。如果您不想指定RangeKey并且只使用HashKey获取项目,那么请考虑删除RangeKey。

答案 1 :(得分:3)

错误是提供的值与架构中定义的类型不匹配。我不知道你的架构是什么,但是,作为一个例子,如果架构将主键(在你的情况下称为key)定义为一个字符串,你提供了一个整数值,反之亦然,你会得到这个错误。

检查您的架构并确保为您的查询传递正确的值类型。