在AWS Lambda中处理分页的最佳方法是什么?

发布于 2025-02-10 17:10:08 字数 173 浏览 1 评论 0原文

我有一个简单地由lambda处理的get请求,该请求从DynamoDB表中读取所有行。因此,它返回所有记录。

但是,当我那里有大量记录时,它的时间是因为在这张桌子上进行扫描需要超过29秒。

解决这个问题的最佳方法是什么?如果是这样,最好的方法是什么?

我正在使用Nodejs V3 SDK。

I have a GET request that simply is handled by lambda which reads all the rows from dynamodb table. So it returns all records.

But when I have a very large amount of records there, it times out because doing scan on this table takes more than 29 seconds.

What is the best way to handle this, pagination ? If so, what would be the best way to do it?

Im using nodejs v3 sdk.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

触ぅ动初心 2025-02-17 17:10:08

由于Dynamo DB查询操作将数据返回至1 MB,因此分页逻辑将如下:

  1. 使用DeleverIvestArtKey查询表,并限制循环中的参数
  2. 从第二个执行开始,请从2nd执行开始seret expusivEstartKey = extusivEstartKey = lastEvaLeTekykeykey = lastEvalecekekeykeykeyke new
  3. the query necor store the every every
  4. never loop top lop lop loe loe lop loe loe lop loe loe lop loe tow长度返回0

一些其他考虑因素-1)增加lambda超时2)也增加了lambda内存。

PS我对节点JS并不熟悉,请原谅我的无知。

Since dynamo db query operation returns data up to 1 mb, the pagination logic would be as follows:

  1. query the table using ExclusiveStartKey and Limit parameters in a loop
  2. from 2nd execution onwards set ExclusiveStartKey = LastEvaluatedKey
  3. store the each query result
  4. keep on looping until query result length returns 0

some additional consideration - 1) Increase the lambda timeout 2) increase lambda memory as well.

p.s. I am not familiar much with Node JS, excuse my ignorance.

感悟人生的甜 2025-02-17 17:10:08

您可以通过传递limit以及表名来设置自己的页面大小。这是一个每页20个结果的示例:

{
  "TableName": "your_dynamodb_table",
  "Limit": 20
}

然后,您必须从结果有效载荷中传递lastEvaledekey才能获取下一页,将其更名为exclusivestartkey。此示例假设您的密钥是字符串类型:

{
  "TableName": "<your_dynamodb_table>",
  "Limit": 20,
  "ExclusiveStartKey": {
    "id": {"S": "<last_evaluated_key>"}
  }
}

另外,请注意,您可以直接从API网关访问DynamoDB,直接没有中间的lambda。像此帖子

You can set your own page size by passing Limit along with your table name. Here is an example with 20 results per page:

{
  "TableName": "your_dynamodb_table",
  "Limit": 20
}

Then, you'll have to pass LastEvaluatedKey from the resulting payload to get the next page, renamed as ExclusiveStartKey. This example assumes your key is of string type:

{
  "TableName": "<your_dynamodb_table>",
  "Limit": 20,
  "ExclusiveStartKey": {
    "id": {"S": "<last_evaluated_key>"}
  }
}

Also, mind that you can access DynamoDB from API Gateway directly not having a lambda in-between. Like in this post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文