我正在使用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)
两个表中至少有一个元素。区别在于第一个返回结果,而第二个没有。任何帮助都会非常感激。干杯!
问题是使用DynamoDB版本1(即弃用的一个)。一旦我切换到连接新版本的表并使用适当的方法执行扫描,它就工作了。
希望这可以帮助那些被跨版本不匹配所困扰的灵魂。