How to use pagination in dynamodb in boto3 using the SCAN method?

79 Views Asked by At

I'm trying to use 'LastEvaluatedKey' with a scan method in dynamo, but I'm not able to pull data from other pages, just one.

I also asked to print the value of 'LastEvaluatedKey' and the values ​​always remain the same, it's as if it doesn't leave the first pagination and I don't understand why,

resp = table.scan(AttributesToGet=['name','email'])`

while 'LastEvaluatedKey' resp:     

    count=0     
    while count < len(resp.get('Items')):
       print(resp.get('Items')[count].get('name'), ",",
       resp.get('Items')[count].get('email'))  
       count +=1
1

There are 1 best solutions below

6
Leeroy Hannigan On
import boto3
dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('mytable')

response = table.scan(AttributesToGet=['name','email'])

data = response['Items']

while 'LastEvaluatedKey' in response:
    response = table.scan(AttributesToGet=['name','email'],Exclusive Start Key=response['LastEvaluatedKey'])
    data.extend(response['Items'])