DEV Community

nainarmalik
nainarmalik

Posted on

DynamoDB get all records

Hey,

Here is a quick hands-on of retrieving all rows from a dynamodb table.

`
from pprint import pprint
from time import sleep
import boto3
from botocore.exceptions import ClientError

def get_data(dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb',region_name='YOUR_REGION')

table = dynamodb.Table('YOUR_TABLE_NAME')

try:
    response = table.scan()
    data = response['Items']

    while 'LastEvaluatedKey' in response:
        response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
        data.extend(response['Items'])

except ClientError as e:
    print(e.response['Error']['Message'])
else:
    return response
Enter fullscreen mode Exit fullscreen mode

if name == 'main':
data = get_data()
if data:
pprint(data, sort_dicts=False)
`

P.S:
1.You need to configure the aws credentials.
2.Replace YOUR_REGION with the region name where the dynamodb table is located.
3.Replace YOUR_TABLE_NAME with the actual name of the dynamodb table.

Feel free to ask if you have any clarifications.

Top comments (0)