DynamoDB中的分页 [英] Pagination in DynamoDB

查看:383
本文介绍了DynamoDB中的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求在jsp上显示搜索结果,且maxcount为10,并且它应具有作为分页功能来回移动的分页。

I have a requirement for to show the search result on the jsp with maxcount of 10 and it should have a pagination to traverse back and forward as pagination functionality.

Dynamodb有一个 lastevaluatedkey ,但是尽管我可以通过 lastevaluatedKey 移到下一个结果集,但返回上一页没有帮助。

Dynamodb has a lastevaluatedkey, but it doesn't help to go back to the previous page, though I can move to the next result set by the lastevaluatedKey.

有人可以帮忙吗?

我使用Java SPRING和DynamoDB作为堆栈。

I am using Java SPRING and DynamoDB as the stack.

感谢
Satya

Thanks Satya

推荐答案

基于Ray的答案,这就是我所做的。 sortId 是排序键。

Building on Ray's answer, here's what I did. sortId is the sort key.

// query a page of items and create prev and next cursor
// cursor idea from this article: https://hackernoon.com/guys-were-doing-pagination-wrong-f6c18a91b232
async function queryCursor( cursor) {
  const cursor1 = JSON.parse(JSON.stringify(cursor));
  const pageResult = await queryPage( cursor1.params, cursor1.pageItems);

  const result = {
    Items: pageResult.Items,
    Count: pageResult.Count
  };

  if ( cursor.params.ScanIndexForward) {
    if (pageResult.LastEvaluatedKey) {
      result.nextCursor = JSON.parse(JSON.stringify(cursor));
      result.nextCursor.params.ExclusiveStartKey = pageResult.LastEvaluatedKey;
    }
    if ( cursor.params.ExclusiveStartKey) {
      result.prevCursor = JSON.parse(JSON.stringify(cursor));
      result.prevCursor.params.ScanIndexForward = !cursor.params.ScanIndexForward;
      result.prevCursor.params.ExclusiveStartKey.sortId = pageResult.Items[0].sortId;
    }
  } else {
    if (pageResult.LastEvaluatedKey) {
      result.prevCursor = JSON.parse(JSON.stringify(cursor));
      result.prevCursor.params.ExclusiveStartKey = pageResult.LastEvaluatedKey;
    }
    if ( cursor.params.ExclusiveStartKey) {
      result.nextCursor = JSON.parse(JSON.stringify(cursor));
      result.nextCursor.params.ScanIndexForward = !cursor.params.ScanIndexForward;
      result.nextCursor.params.ExclusiveStartKey.sortId = pageResult.Items[0].sortId;
    }
  }

  return result;
}

这篇关于DynamoDB中的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆