英文:
DynamoDB paginated data retrieval
问题
我有以下列及其用途。
id
-> 存储修改的时间戳authorName
-> 保存进行修改的用户名authorEmail
-> 保存进行修改的用户电子邮件version
-> 表示版本号的值Data
-> 一个JSON对象
在这里,version
可以被视为自动递增字段,当发生修改时会递增。
DynamoDB的分区键是时间戳的id。
我想创建一个GET API,按照版本号降序排序所有记录,并应用由pageSize常量控制的限制。
目前我考虑的架构是在响应中发送LastEvaluatedKey
以及数据,并且下一个API调用将传递这个LastEvaluatedKey
,它将成为扫描操作中的ExclusiveStartKey
。
据我了解,问题在于记录不能跨多个分区排序。
有没有办法实现这个需求?
英文:
I have the following columns and its purpose.
id
-> stores the timestamp of modificationauthorName
-> saves the user name who have modifiedauthorEmail
-> saves the user email who have modifiedversion
-> value denoting the version numberData
-> a JSON object
Here, version
can be considered as auto increment field which is incremented when modification happens.
The DynamoDB has partition key as id which is timestamp.
I want to create a GET API which orders all the records by version in descending order and applies a limit governed by pageSize constant.
Currently the architecture I thought of was by sending LastEvaluatedKey
in response along with the data and next api call would pass this LastEvaluatedKey
which will be my ExclusiveStartKey
in the scan operation.
The issue as per my understanding is that the records cannot be ordered across multiple partitions.
Is there a way through which this can be achieved?
答案1
得分: 1
如果您想按照version
降序获取给定id
的所有项,那么您必须使用分区键:id
和排序键:version
SELECT * FROM myTable WHERE id=123 LIMIT 5 DESC
如果您想通过版本来维护全局顺序(跨所有项的顺序),那么您将需要创建一个全局二级索引并使用静态分区键:
gsipk | version | id | other |
---|---|---|---|
1 | 0 | 123 | data |
1 | 1 | 376 | data |
1 | 2 | 292 | data |
1 | 5 | 101 | data |
1 | 6 | 123 | data |
1 | 10 | 403 | data |
1 | 13 | 191 | data |
1 | 17 | 403 | data |
SELECT * FROM myTable.myIndex WHERE gsipk=1 LIMIT 5 DESC
现在,由于我们对GSI分区键(1
)使用了静态值,我想提醒您,这将限制您的吞吐量为每秒1000个写吞吐量单位(WCU),因为这是单个分区的限制。如果您需要更高的吞吐量,那么您将需要使用GSI分区键分片。
英文:
If you want to get all items for a given id
and sorted in desc order by version
then you must use partition key: id
and sort key: version
SELECT * FROM myTable WHERE id=123 LIMIT 5 DESC
If you would like to maintain global order (order across all items) by version, then you will have to create a Global Secondary index and use a static partition key:
gsipk | version | id | other |
---|---|---|---|
1 | 0 | 123 | data |
1 | 1 | 376 | data |
1 | 2 | 292 | data |
1 | 5 | 101 | data |
1 | 6 | 123 | data |
1 | 10 | 403 | data |
1 | 13 | 191 | data |
1 | 17 | 403 | data |
SELECT * FROM myTable.myIndex WHERE gsipk=1 LIMIT 5 DESC
Now that we use a static value for GSI partition key (1
) I want to make you aware that this will limit your throughput to 1000 WCU per second, as that is the limit for a single partition. If you require more throughput than that, then you will have to make use of GSI partition key sharding.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论