Ruby-on-rails 在rails中使用amazon Dynamo DB时如何实现分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9097708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement pagination when using amazon Dynamo DB in rails
提问by sunil
I want to use amazon Dynamo DB with rails.But I have not found a way to implement pagination.
我想将 amazon Dynamo DB 与 rails 一起使用。但我还没有找到实现分页的方法。
I will use AWS::Record::HashModelas ORM.
我将AWS::Record::HashModel用作 ORM。
This ORM supports limits like this:
这个 ORM 支持这样的限制:
People.limit(10).each {|person| ... }
But I could not figured out how to implement following MySql query in Dynamo DB.
但是我不知道如何在 Dynamo DB 中实现以下 MySql 查询。
SELECT *
FROM `People`
LIMIT 1 , 30
回答by codeprimate
You issue queries using LIMIT. If the subset returned does not contain the full table, a LastEvaluatedKeyvalue is returned. You use this value as the ExclusiveStartKeyin the next query. And so on...
您使用 LIMIT 发出查询。如果返回的子集不包含完整表,LastEvaluatedKey则返回一个值。您可以将此值用作ExclusiveStartKey下一个查询中的 。等等...
From the DynamoDB Developer Guide.
回答by Pranav Patil
You can provide 'page-size' in you query to set the result set size. The response of DynamoDB contains 'LastEvaluatedKey' which will indicate the last key as per the page size. If response does't contain 'LastEvaluatedKey' it means there are no results left to fetch. Use the 'LastEvaluatedKey' as 'ExclusiveStartKey' while fetching next time.
您可以在查询中提供“页面大小”以设置结果集大小。DynamoDB 的响应包含“LastEvaluatedKey”,它将根据页面大小指示最后一个键。如果响应不包含“LastEvaluatedKey”,则意味着没有剩余的结果可供获取。下次获取时使用“LastEvaluatedKey”作为“ExclusiveStartKey”。
I hope this helps.
我希望这有帮助。
回答by Dattatray
I faced a similar problem.
我遇到了类似的问题。
The generic pagination approach is, use "start index" or "start page" and the "page length".?
通用的分页方法是,使用“起始索引”或“起始页”和“页长”。?
The "ExclusiveStartKey" and "LastEvaluatedKey" based approach is very DynamoDB specific.
基于“ExclusiveStartKey”和“LastEvaluatedKey”的方法非常特定于DynamoDB。
I feel this DynamoDB specific implementation of pagination should be hidden from the API client/UI.
我觉得这个 DynamoDB 特定的分页实现应该对 API 客户端/UI 隐藏。
Also in case, the application is serverless, using service like Lambda, it will be not be possible to maintain the state on the server. The other side is the client implementation will become very complex.
此外,如果应用程序是无服务器的,使用 Lambda 之类的服务,将无法在服务器上维护状态。另一方面是客户端的实现会变得非常复杂。
I came with a different approach, which I think is generic ( and not specific to DynamoDB)
我提出了一种不同的方法,我认为这是通用的(而不是特定于 DynamoDB)
When the API client specifies the start index, fetch all the keys from the table and store it into an array.
Find out the key for the start index from the array, which is specified by the client.
Make use of the ExclusiveStartKey and fetch the number of records, as specified in the page length.
If the start index parameter is not present, the above steps are not needed, we don't need to specify the ExclusiveStartKey in the scan operation.
当 API 客户端指定起始索引时,从表中获取所有键并将其存储到数组中。
从客户端指定的数组中找出起始索引的键。
使用 ExclusiveStartKey 并获取页面长度中指定的记录数。
如果不存在起始索引参数,则不需要上述步骤,我们不需要在扫描操作中指定 ExclusiveStartKey。
This solution has some drawbacks -
此解决方案有一些缺点 -
We will need to fetch all the keys when the user needs pagination with start index.
We will need additional memory to store the Ids and the indexes. Additional database scan operations ( one or multiple to fetch the keys )
当用户需要使用起始索引进行分页时,我们将需要获取所有键。
我们将需要额外的内存来存储 Id 和索引。额外的数据库扫描操作(一个或多个获取密钥)
But I feel this will be very easy approach for the clients, which are using our APIs. The backward scan will work seamlessly. If the user wants to see "nth" page, this will be possible.
但我觉得对于使用我们 API 的客户来说,这将是一种非常简单的方法。向后扫描将无缝工作。如果用户想看到“第n”页,这将是可能的。

