简体   繁体   中英

Scanning from dynamodb with out partition key using boto3

  • I need to extract the items from dynamodb which is matching id, name, role

  • Above 3 are just attributes of the table they are not part key or sort key

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?

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

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