首页 > 解决方案 > 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哪个按降序对日期进行排序。

非常欢迎您的想法。

标签: pythonamazon-web-servicesboto3

解决方案


我认为不可能订购scan. 此外,scan不会检索您的所有记录,最多可以获取 1MB 的数据。您需要使用重复该过程LastEvaluatedKey,然后在代码中执行排序。


推荐阅读