DEV Community

nainarmalik
nainarmalik

Posted on

2 1

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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay