Boto DynamoDB2迭代查询结果集不起作用

时间:2013-11-20 07:18:41

标签: python boto amazon-dynamodb

我正在使用boto v2.16和DynamodDB2 API。按照boto,dynamodb2的官方文档中的教程进行操作:

from boto.dynamodb2.table import Table
photos = Table('photos') # photos is a table
def get_image_urls():
    # photo_id is hashkey and type is rangekey
    allphotos = photos.query(photo_id__gte = 0, type__eq = 'Homedesign')

    # This prints <boto.dynamodb2.results.ResultSet object at 0x02981CF0>
    print allphotos

    #When I try to iterate the resultset, I get an error        
    for eachphoto in allphotos:
        print eachphoto['photo']

当我尝试迭代结果集allphotos

时,这是我得到的错误
Traceback (most recent call last):
  File "E:\coding\FL\ongoing jobs\django - FB\facebook-app-rating\rating\insert_data.py", line 75, in <module>
    get_image_urls()
  File "E:\coding\FL\ongoing jobs\django - FB\facebook-app-rating\rating\insert_data.py", line 59, in get_image_urls
    for eachphoto in allphotos:
  File "C:\Python27\lib\site-packages\boto\dynamodb2\results.py", line 59, in next
    self.fetch_more()
  File "C:\Python27\lib\site-packages\boto\dynamodb2\results.py", line 114, in fetch_more
    results = self.the_callable(*args, **kwargs)
  File "C:\Python27\lib\site-packages\boto\dynamodb2\table.py", line 868, in _query
    **kwargs
  File "C:\Python27\lib\site-packages\boto\dynamodb2\layer1.py", line 975, in query
    body=json.dumps(params))
  File "C:\Python27\lib\site-packages\boto\dynamodb2\layer1.py", line 1487, in make_request
    retry_handler=self._retry_handler)
  File "C:\Python27\lib\site-packages\boto\connection.py", line 898, in _mexe
    status = retry_handler(response, i, next_sleep)
  File "C:\Python27\lib\site-packages\boto\dynamodb2\layer1.py", line 1527, in _retry_handler
    response.status, response.reason, data)
boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request
{u'message': u'Query key condition not supported', u'__type': u'com.amazon.coral.validate#ValidationException'}
[Finished in 17.1s with exit code 1]

不知道发生了什么事!有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

这实际上是一些愚蠢的错误。 hashkey上不允许使用过滤器gte(大于等于)。 hashkey上只允许一个过滤器,即eq(相等)。所以当我删除它,它工作正常。实际上我删除了表格照片,通过交换hashkey和rangekey重新创建了它,并将查询代码更新为:

from boto.dynamodb2.table import Table
photos = Table('photos') # photos is a table
def get_image_urls():
    # photo_id is rangekey and type is hashkey
    allphotos = photos.query(type__eq = 'Homedesign', photo_id__gte = 1)

    print allphotos

    for eachphoto in allphotos:
        print eachphoto['photo']

这很好用!