Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS DynamoDB ExclusiveStartKey default value

I'm trying to make a query to DynamoDB, and if a LastEvaluatedKey is returned (meaning the query exceeds 1 MB) I want to make other queries in order to fetch all the required data from the table, using LastEvaluatedKey as ExclusiveStartKey for the next query. This is the code I have for now:

query_response = table.query(
    KeyConditionExpression=Key('brand').eq(brand)
)

pagination_key = None

if 'LastEvaluatedKey' in query_response:
    pagination_key = query_response['LastEvaluatedKey']

    while pagination_key:
        next_query_response = table.query(
            KeyConditionExpression=Key('brand').eq(brand),
            ExclusiveStartKey=pagination_key
        )

However, I'd like to refacto this code by extracting the query into a method, passing it pagination_key as an argument. To do this, I'd have to be able to either set ExclusiveStartKey to False, None or some other default value for the first call but I didn't find anything on this, or I'd have to be able to exclude the ExclusiveStartKey alltogether, but I don't know how to do this either.

like image 903
nicojonck Avatar asked Oct 29 '25 10:10

nicojonck


1 Answers

Using keyword Arguments **kwargs it might look like this. Also, I am setting up the query before and only updating the ExclusiveStartKeyevery time.

query = { "KeyConditionExpression": Key('brand').eq(brand) }


ExclusiveStartKey = None
while True:
    if ExclusiveStartKey is not None:
        query['ExclusiveStartKey'] = ExclusiveStartKey
    query_response = table.query(**query)

    if 'LastEvaluatedKey' in query_response:
        ExclusiveStartKey = query_response['LastEvaluatedKey']
    else:
        break
like image 85
user 923227 Avatar answered Oct 31 '25 00:10

user 923227



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!