简体   繁体   中英

Dynamo DB scan query to obtain output file in JSON format

I am quite new to Dynamo DB using boto3. I would like to: obtain a scan of all the rows in Dynamo DB and store it in JSON format, in a file, for additional data processing.

I am presently using the script shown below to fetch the details (pagination will be involved) :

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),)

This gives me sample output of 5000 rows:

{"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"}

I am unable to get the output in the desired format (as shown below). I am expecting a file here.

[
    {"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"}
]

Could anyone please provide me some hints or pointers on how to investigate this further?

Isn't response['Items'] not the list you are looking for? Response is a dictionnary with the list inside, it can be only a subset so you need to iterate over multiple responses if needed.

Reference: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.scan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM