【问题标题】:DynamoDB scan() ResultSet has no attribute keysDynamoDB scan() ResultSet 没有属性键
【发布时间】:2015-07-13 23:37:56
【问题描述】:

我正在使用 scan 查询两个 dynamo db 表以获取其中的所有项目,将它们存储在列表中,然后返回它们(所有这些都在 Flask 应用程序中)。很简单,没什么花哨的。

问题是,对于一个它可以正常工作,但对于另一个我得到这个:

for entry in squads_info:
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/layer2.py", line 125, in __iter__
response = self.response
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/layer2.py", line 92, in response
return self.next_response() if self._response is None else self._response
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/layer2.py", line 104, in next_response
self._response = self.callable(**self.kwargs)
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/layer1.py", line 577, in scan
return self.make_request('Scan', json_input, object_hook=object_hook)
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/layer1.py", line 127, in make_request
return json.loads(response_body, object_hook=object_hook)
File "/usr/local/lib/python2.7/json/__init__.py", line 351, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/usr/local/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/types.py", line 347, in decode
return decoder(attr[dynamodb_type])
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/types.py", line 377, in _decode_l
return [self.decode(i) for i in attr]
File "/usr/local/lib/python2.7/site-packages/boto/dynamodb/types.py", line 338, in decode
dynamodb_type = list(attr.keys())[0]
AttributeError: 'unicode' object has no attribute 'keys'

这是第一次扫描的代码:

def get_browsers():
    # the database table instance that can be scanned
    browsers = get_table_connection('Browser')
    # a list of the environments in the table
    browser_list = []
    # getting the full list of environments in the table
    results = browsers.scan()

   # add a dictionary entry for each result we get
   for result in results:
       entry = {
           "name": result['name'],
           "versions": result['versions']
        }

        browser_list.append(entry)

   # return the env_list 
   return jsonify(result=browser_list)

这是失败的扫描代码。

@logger_app.route("/squad-information", methods=['GET'])
def get_squad_info():
   # get connection to db
   squads = get_table_connection('Squad')

   # we need to return all the information we have stored in this table, so a    scan should do
   squads_info = squads.scan()


   # start building up the response
   # response is an array of dictionaries
   response = []

   for entry in squads_info:
      response_entry = {
          "name": entry['name'],
          "squad_id": entry['squad_id'],
          "test_suites": entry['test_suites']
      }

      response.append(response_entry)

return jsonify(squads=response)

两个表格中至少有一个元素。不同之处在于第一个返回结果,而第二个不返回。任何帮助都将不胜感激。干杯!

【问题讨论】:

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


    【解决方案1】:

    问题在于使用 DynamoDB 版本 1(即已弃用的版本)。一旦我切换到连接新版本的表并使用适当的方法执行扫描,它就起作用了。

    希望这可以帮助其他因跨版本不匹配而感到困惑的受折磨的灵魂。

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多