sim
sim

Reputation: 488

Scanning from dynamodb with out partition key using boto3

Below is the code

import boto3
from boto3.dynamodb.conditions import Attr
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('mytable')

def lambda_handler(event, context):
    response_list = []
    scan_response = table.scan(
        FilterExpression=Attr('id').eq(event['id']) & Attr(
        'name').eq(event['name']) & Attr(
        'role').eq('Manager')
    )
    response_list.extend(scan_response['Items'])
    while scan_response.get('LastEvaluatedKey'):
        scan_response = table.scan(
            FilterExpression=Attr('id').eq(event['id']) & Attr(
        'name').eq(event['name']) & Attr(
        'role').eq('Manager')
         )
         response_list.extend(scan_response['Items'])

    print( response_list)

My loop is running infinitely on the first item only. where is the issue?

Upvotes: 0

Views: 1398

Answers (1)

Mark B
Mark B

Reputation: 200476

You aren't passing the LastEvaluatedKey into the scan() call, so it is starting the scan over from the beginning each time.

Change it to this:

while scan_response.get('LastEvaluatedKey'):
    scan_response = table.scan(
        ExclusiveStartKey=scan_response.get('LastEvaluatedKey'),
        FilterExpression=Attr('id').eq(event['id']) & Attr(
    'name').eq(event['name']) & Attr(
    'role').eq('Manager')
     )

Upvotes: 2

Related Questions