nic*_*nck 4 python amazon-dynamodb
我正在尝试对 DynamoDB 进行查询,如果LastEvaluatedKey返回 a (意味着查询超过 1 MB),我想进行其他查询,以便从表中获取所有所需的数据,并使用LastEvaluatedKeyasExclusiveStartKey进行下一个查询。这是我现在的代码:
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
)
Run Code Online (Sandbox Code Playgroud)
但是,我想通过将查询提取到方法中并将其 pagination_key 作为参数传递来重构此代码。为此,我必须能够在第一次调用时设置为ExclusiveStartKey或False其他None一些默认值,但我没有找到任何相关内容,或者我必须能够将所有这些都排除ExclusiveStartKey在外,但我也不知道该怎么做。
使用关键字参数,**kwargs它可能看起来像这样。另外,我之前设置了查询,并且ExclusiveStartKey每次都只更新。
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
Run Code Online (Sandbox Code Playgroud)