python - Boto3 + DynamoDB:查询所有有限制和排序的数据
问题描述
我想要实现的是能够使用限制为每页获取 15 条记录,并按范围键排序。这是我的表格格式。
Primary partition key: id (string) -> in GUID format
Primary sort key: created_on (number) -> date stored in epoch format
我的问题是我无法做到这一点
client = boto3.resource('dynamodb')
table = client.Table('my_table')
result = table.query(Limit=15, ScanIndexForward=False)
我收到此错误,这是可以理解的
An error occurred (ValidationException) when calling the Query operation:
Either the KeyConditions or KeyConditionExpression parameter must be specified in the request
现在我的问题是我不想过滤掉结果id
,只是让所有数据排序created_on
并获取前 15 条记录,这是不可能的,因为它对主分区键query()
具有强制性。equals to
使用scan()
我无法使用ScanIndexForward
哪个按降序对日期进行排序。
非常欢迎您的想法。
解决方案
我认为不可能订购scan
. 此外,scan
不会检索您的所有记录,最多可以获取 1MB 的数据。您需要使用重复该过程LastEvaluatedKey
,然后在代码中执行排序。
推荐阅读
- c# - 必须先关闭的 Open DataReader 问题
- c# - Efficient way to pass Bitmap into registered COM component
- html - 难以使 Strava Iframe 响应式 (CSS/HTML)
- php - 根据会话变量显示按钮不起作用
- node.js - 查询从主集合的一个数组字段中加入 mongo 中的两个集合
- json - 将 JSON 转换为字符串错误字符串文字未终止
- laravel - 如何在 laravel 之外的 bash 脚本中运行 artisan 命令?
- python - 未正确从文件中读取 unicode 文本、缺少空格、错误字符
- python - pip install ortools:没有匹配的分布 - Alpine
- r - ggsave 不会将所有元素保存为 pdf