简体   繁体   English

REST API - 在 dynamoDB 中检索以前的查询

[英]REST API - Retrieve previous query in dynamoDB

I have 100 rows of data in DynamoDB and a api with path api/get/{number}我在 DynamoDB 中有 100 行数据和一个带有路径api/get/{number}的 api

Now when I say number=1 api should return me first 10 values.现在当我说 number=1 api 应该返回前 10 个值。 when I say number=2 it should return next 10 values.当我说 number=2 时,它应该返回接下来的 10 个值。 I did something like this with query, lastEvaluatedKey and sort by on createdOn .我用查询、 lastEvaluatedKey 做了类似的事情,并在createdOn上排序。 Now the use case is if the user passes number=10 after number=2 the lastEvaluatedKey is still that of page 2 and the result would be data of page 3. How can I get data directly.现在的用例是,如果用户在 number=2 之后传递 number=10,lastEvaluatedKey 仍然是第 2 页的那个,结果将是第 3 页的数据。我如何直接获取数据。 Also if the user goes from number=3 to number=1 still the data will not be of page 1.此外,如果用户从 number=3 转到 number=1,数据仍然不会在第 1 页。

I am using this to make API call based of pagination on HTML.我正在使用它根据 HTML 上的分页进行 API 调用。

I am using java 1.8 and aws-java-sdk-dynamodb .我正在使用 java 1.8 和aws-java-sdk-dynamodb

Non-sequential pagination in DynamoDB is tough - you have to design your data model around it, if it's an operation that needs to be efficient at all times. DynamoDB 中的非顺序分页很难 - 如果需要始终保持高效的操作,则必须围绕它设计数据 model 。 For a recommendation in your specific case I'd need more details about the data and access patterns.对于您的特定案例的建议,我需要有关数据和访问模式的更多详细信息。

In general you have the option of setting the ExclusiveStartKey attribute in the query call, which is similar to an offset in relational databases, but only similar and not identical.通常,您可以选择在查询调用中设置ExclusiveStartKey属性,这类似于关系数据库中的偏移量,但只是相似而不相同。 The ExclusiveStartKey is the key after which the query will continue, meaning data from your table and not just a number. ExclusiveStartKey是查询将在其后继续的键,这意味着您的表中的数据而不仅仅是一个数字。

That means you usually can't guess it, unless it's a sequential number - which isn't ideal.这意味着您通常猜不到它,除非它是一个序列号——这并不理想。

For sequential pagination, ie the user goes from page 1 to page 2, page 2 to page 3 etc. you can pass that along in the request as a token, but that won't work if the user moves in the other direction page 3 to page 2 or just randomly navigates to page 14.对于顺序分页,即用户从第 1 页转到第 2 页,从第 2 页转到第 3 页等。您可以在请求中将其作为令牌传递,但如果用户向另一个方向移动第 3 页,那将不起作用到第 2 页或只是随机导航到第 14 页。

In your case you only have a limited amount of data - 100 items, so my solution for your specific case would be to query all items and limit the amount of items in the response to n * 10 , where n is the result page.在您的情况下,您只有有限数量的数据 - 100 个项目,因此我针对您的特定情况的解决方案是查询所有项目并将响应中的项目数量限制为n * 10 ,其中n是结果页面。 Then you return the last 10 items from that result to your client.然后将该结果中的最后 10 项返回给您的客户。

This is a solution that would get expensive at scale (time + cost) though, fortunately not many people will use the pagination to go to page 7 or 8 though ( you could bury a body on page 2 of the google search results ).这是一个在规模上(时间 + 成本)会变得昂贵的解决方案,幸运的是,没有多少人会使用分页到 go 到第 7 或 8 页(你可以在谷歌搜索结果的第 2 页埋一个身体)。

Yan Cui has written an interesting post on this problem on Hackernoon , you might want to check it out. Yan Cui 在 Hackernoon 上就这个问题写了一篇有趣的帖子,你可能想看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM