DynamoDB分页数据检索

huangapple go评论88阅读模式
英文:

DynamoDB paginated data retrieval

问题

我有以下列及其用途。

  1. id -> 存储修改的时间戳
  2. authorName -> 保存进行修改的用户名
  3. authorEmail -> 保存进行修改的用户电子邮件
  4. version -> 表示版本号的值
  5. Data -> 一个JSON对象

在这里,version可以被视为自动递增字段,当发生修改时会递增。

DynamoDB的分区键是时间戳的id。

我想创建一个GET API,按照版本号降序排序所有记录,并应用由pageSize常量控制的限制。

目前我考虑的架构是在响应中发送LastEvaluatedKey以及数据,并且下一个API调用将传递这个LastEvaluatedKey,它将成为扫描操作中的ExclusiveStartKey

据我了解,问题在于记录不能跨多个分区排序。

有没有办法实现这个需求?

英文:

I have the following columns and its purpose.

  1. id -> stores the timestamp of modification
  2. authorName -> saves the user name who have modified
  3. authorEmail -> saves the user email who have modified
  4. version -> value denoting the version number
  5. Data -> 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.

huangapple
  • 本文由 发表于 2023年7月3日 20:23:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定