python - AWS DynamoDB ExclusiveStartKey 默认值
问题描述
我正在尝试对 DynamoDB 进行查询,如果LastEvaluatedKey
返回 a(意味着查询超过 1 MB),我想进行其他查询,以便从表中获取所有需要的数据,使用LastEvaluatedKey
asExclusiveStartKey
进行下一个查询。这是我现在的代码:
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
)
但是,我想通过将查询提取到一个方法中来重构这段代码,并将它作为参数传递给 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
推荐阅读
- flutter - 我正在登录/注册,现在我卡在颤动的注册页面上
- javascript - 使用 javaScript 在 IOS 的 nextjs 项目中禁用捏缩放
- convolution - 在python中编写delta函数的卷积
- specflow - 更改了文件夹结构 - 现在缺少 Specflow .feature 文件(仅看到 .feature.cs)
- android - 如何解决 Android Studio 的 AVD 终止问题?
- google-kubernetes-engine - 如何添加配置 GKE 节点池访问范围
- reactjs - 如何从父母的样式组件中获取孩子道具
- azure - 如何使用 Azure DevOps REST API 运行构建/发布管道
- cordova - 无法在 Cordova 上加载 Stripe 3ds iframe
- python - 如何获取 h3 标签下的特定链接?