[英]Dynamo DB scan query to obtain output file in JSON format
我对使用 boto3 的 Dynamo DB 很陌生。 我想:获取 Dynamo DB 中所有行的扫描并将其以JSON
格式存储在文件中,以进行额外的数据处理。
我目前正在使用下面显示的脚本来获取详细信息(将涉及分页):
from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Movies')
#fe = Key('year').between(1951, 1964)
pe = "#yr, title, info.rating"
# Expression Attribute Names for Projection Expression only.
ean = { "#yr": "year", }
esk = None
response = table.scan(
# FilterExpression=fe,
ProjectionExpression=pe,
ExpressionAttributeNames=ean
)
for i in response['Items']:
print(json.dumps(i, cls=DecimalEncoder))
while 'LastEvaluatedKey' in response:
response = table.scan(
ProjectionExpression=pe,
# FilterExpression=fe,
ExpressionAttributeNames= ean,
ExclusiveStartKey=response['LastEvaluatedKey']
)
for i in response['Items']:
print(json.dumps(i, cls=DecimalEncoder),)
这给了我 5000 行的示例输出:
{"info": {"rating": 6.5}, "year": 2004, "title": "The Polar Express"}
{"info": {"rating": 5.7}, "year": 2004, "title": "The Prince & Me"}
{"info": {"rating": 5.3}, "year": 2004, "title": "The Princess Diaries 2: Royal Engagement"}
{"info": {"rating": 6.3}, "year": 2004, "title": "The Punisher"}
{"info": {"rating": 6.8}, "year": 2004, "title": "The SpongeBob SquarePants Movie"}
我无法获得所需格式的输出(如下所示)。 我在这里期待一个文件。
[
{"info": {"rating": 6.5}, "year": 2004, "title": "The Polar Express"},
{"info": {"rating": 5.7}, "year": 2004, "title": "The Prince & Me"},
{"info": {"rating": 5.3}, "year": 2004, "title": "The Princess Diaries 2: Royal Engagement"},
{"info": {"rating": 6.3}, "year": 2004, "title": "The Punisher"},
{"info": {"rating": 6.8}, "year": 2004, "title": "The SpongeBob SquarePants Movie"}
]
任何人都可以向我提供一些有关如何进一步调查的提示或指示吗?
response['Items'] 不是您要查找的列表吗? Response 是一个包含列表的字典,它只能是一个子集,因此您需要根据需要迭代多个响应。
参考: https : //boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.scan
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.