我正在使用dynamodb的本地实例进行练习,从测试实例中获取项目,并将json对象放回本地表中。
例如,以id作为主键查询会给我一个“项目”响应,其中包含四个字典(在测试数据库中已确认四个记录)的列表:
{'Items': [{'processId': 'a1',
'recommendation': {'value': Decimal('0'),
'processor': 'John Doe'},
'createDate': '2019-02-27T18:53:16.380Z',
'status': 'rejected',
'recordId': '5',
'createdBy': 'Buddy',
'actions': [{'actor': 'Buddy',
'actionType': 'review',
'note': '444551233344',
'actorName': 'Buddy',
'createDate': '2019-02-27T18:53:16.380Z'},
{'actor': 'Johnny',
'actionType': 'reject',
'note': '234',
'actorName': 'Johnny',
'createDate': '2019-02-27T19:00:46.425Z'}],
'type': 'adjusted'},
{'processId': 'a2',
'recommendation': {'value': Decimal('0'),
'processor': 'John Doe'},
'createDate': '2019-02-27T18:53:16.380Z',
'status': 'rejected',
'recordId': '5',
'createdBy': 'Buddy',
'actions': [{'actor': 'Buddy',
'actionType': 'review',
'note': '444551233344',
'actorName': 'Buddy',
'createDate': '2019-02-27T18:53:16.380Z'},
{'actor': 'Johnny',
'actionType': 'reject',
'note': '234',
'actorName': 'Johnny',
'createDate': '2019-02-27T19:00:46.425Z'}],
'type': 'adjusted'},
{'processId': 'a3',
'recommendation': {'value': Decimal('0'),
'processor': 'John Doe'},
'createDate': '2019-02-27T18:53:16.380Z',
'status': 'rejected',
'recordId': '5',
'createdBy': 'Buddy',
'actions': [{'actor': 'Buddy',
'actionType': 'review',
'note': '444551233344',
'actorName': 'Buddy',
'createDate': '2019-02-27T18:53:16.380Z'},
{'actor': 'Johnny',
'actionType': 'reject',
'note': '234',
'actorName': 'Johnny',
'createDate': '2019-02-27T19:00:46.425Z'}],
'type': 'adjusted'},
{'processId': 'a4',
'recommendation': {'value': Decimal('0'),
'processor': 'John Doe'},
'createDate': '2019-02-27T18:53:16.380Z',
'status': 'rejected',
'recordId': '5',
'createdBy': 'Buddy',
'actions': [{'actor': 'Buddy',
'actionType': 'review',
'note': '444551233344',
'actorName': 'Buddy',
'createDate': '2019-02-27T18:53:16.380Z'},
{'actor': 'Johnny',
'actionType': 'reject',
'note': '234',
'actorName': 'Johnny',
'createDate': '2019-02-27T19:00:46.425Z'}],
'type': 'adjusted'}],
'Count': 4,
'ScannedCount': 4,
'ResponseMetadata': {'RequestId': '4LI4AVRIAQ',
'HTTPStatusCode': 200,
'HTTPHeaders': {'server': 'Server',
'date': 'Thu, 03 Oct 2019 19:45:59 GMT',
'content-type': 'application/x-amz-json-1.0',
'content-length': '13934',
'connection': 'keep-alive',
'x-amzn-requestid': '4LI4AVR',
'x-amz-crc32': '4101956423'},
'RetryAttempts': 0}}
响应:
trimmed = blurb_from_test["Items"]
然后我将其修剪成字典列表:
for i in range(len(trimmed)):
table_local.put_item(
Item=trimmed[i]
)
然后我使用put_item进行遍历:
{'Items': [{'processId': 'a4',
'recommendation': {'value': Decimal('0'),
'processor': 'John Doe'},
'createDate': '2019-02-27T18:53:16.380Z',
'status': 'rejected',
'recordId': '5',
'createdBy': 'Buddy',
'actions': [{'actor': 'Buddy',
'actionType': 'review',
'note': '444551233344',
'actorName': 'Buddy',
'createDate': '2019-02-27T18:53:16.380Z'},
{'actor': 'Johnny',
'actionType': 'reject',
'note': '234',
'actorName': 'Johnny',
'createDate': '2019-02-27T19:00:46.425Z'}],
'type': 'adjusted'}]
'Count': 1,
'ScannedCount': 1,
'ResponseMetadata': {'RequestId': 'ced4a02e-f878-4edf-843a-2aabfadb0133',
'HTTPStatusCode': 200,
'HTTPHeaders': {'content-type': 'application/x-amz-json-1.0',
'x-amz-crc32': '4260213483',
'x-amzn-requestid': 'ced4a02e-f878-4edf-843a-2aabfadb0133',
'content-length': '3360',
'server': 'Jetty(8.1.12.v20130726)'},
'RetryAttempts': 0}}
但是我只能得到一条记录(使用与上面相同的getRecordsById方法):
{{1}}
我的假设是我没有正确地使用put_item进行迭代,但是我找不到任何原因。
答案 0 :(得分:0)
您可以迭代如下:
for item in response.get('Items'):
print(item)
可以出于您的目的更改打印语句的地方,即
for item in response.get('Items'):
table_local.put_item(
Item=item
)