如何做分页与SimpleDB的? [英] How to do paging with simpledb?

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

问题描述

我知道如何与SimpleDB的数据转发页面使用的nextToken。然而,究竟怎样完成一个把手previous页面?我在.NET,但我不认为事情。我更感兴趣的总体战略。

I know how to page forward with SimpleDB data by using NextToken. However, how exactly does one handle previous pages? I'm on .NET, but I don't think that matters. I'm more interested in the general strategy.

麦克卡尔弗的介绍到Amazon的SimpleDB 网络研讨会中提到,面包屑使用,但他并不在视频落实。

Mike Culver's An Introduction to Amazon SimpleDB webinar mentions that breadcrumbs are used, but he doesn't implement them in the video.

编辑:视频中提到该向后实现分页的示例项目,但视频结尾可以显示下载的URL之前。我发现的一个样本项目没有处理分页。

The video mentions a sample project which implements backwards paging, but the video ends before the URL for the download can be displayed. The one sample project I found didn't deal with paging.

推荐答案

在进入下一个页面中,您可以通过只允许一个下一页,而不是任意的页面很简单的使用情况。您可以使用LIMIT子句为此在SimpleDB的:

When going to the next page you may be able to simplify the use case by only allowing a "next page" and not arbitrary paging. You can do this in SimpleDB by using the LIMIT clause:

SELECT title, summary, votecount FROM posts WHERE userid = '000022656' LIMIT 25

您已经知道如何处理的nextToken,但如果你使用这个战术,你可以通过存储下一个记号的面包屑(例如在Web会话)支持previous页,并重新发出查询以previous的nextToken,而不是以后一个。

You already know how to handle the NextToken, but if you use this tactic, you can support "previous page" by storing the breadcrumb trail of next tokens (e.g. in the web session) and re-issuing the query with a previous NextToken rather than a subsequent one.

然而,对于在SimpleDB的处理任意分页一般情况下为previous和下是相同的。在一般情况下,用户可以点击任意页号,如5,而不必访问的页面4或6

However, the general case for handling arbitrary pagination in SimpleDB is the same for previous and next. In the general case, the user may click on an arbitrary page number, like 5, without ever having visited page 4 or 6.

您通过使用的nextToken只需要在WHERE子句是相同的正常工作的事实,在SimpleDB的处理这个问题。因此,而不是通过每个页面顺序查询拉低所有干预项目,通常可以做它在两个步骤。

You handle this in SimpleDB by using the fact that NextToken only requires the WHERE clause to be the same to work properly. So rather than querying through every page in sequence pulling down all the intervening items, you can usually do it in two steps.


  1. 用,其中所需的页面应该启动一个极限值,和SELECT COUNT(*),而不是你想要的实际属性发出查询。

  2. 从第一步使用的nextToken使用所需的属性和页面大小为极限来获取实际的页面数据

因此​​,在伪code:

So in pseudo code:

int targetPage, pageSize;
...
int jumpLimit = pageSize * (targetPage - 1);
String query = "SELECT %1 FROM posts WHERE userid = '000022656' LIMIT %2";
String output = "title, summary, votecount";
Result temp = sdb.select(query, "count(*)", jumpLimit);
Result data = sdb.select(query, output, pageSize, temp.getToken());

其中,%1%2的字符串替换和sdb.select()是指包括与SimpleDB的通话沿字符串替换code一个虚构的方法。

Where %1 and %2 are String substitutions and "sdb.select()" is a fictitious method that includes the String substitution code along with the SimpleDB call.

无论你是否可以在两个呼叫做到这SimpleDB的(如图所示code)将取决于复杂的WHERE子句和数据集的大小。以上code简化在临时结果可能会返回一个部分计数,如果查询时间超过5秒运行。你会真想把该行的循环,直到达到适当的计数。为了使code稍微更现实我就会把它的方法内摆脱字符串替换的:

Whether or not you can accomplish this in two calls to SimpleDB (as shown in the code) will depend on the complexity of your WHERE clause and the size of your data set. The above code is simplified in that the temp result may have returned a partial count if the query took more than 5 seconds to run. You would really want to put that line in a loop until the proper count is reached. To make the code a little more realistic I'll put it within methods and get rid of the String substitutions:

private Result fetchPage(String query, int targetPage)
{
    int pageSize = extractLimitValue(query);
    int skipLimit = pageSize * (targetPage - 1);
    String token = skipAhead(query, skipLimit);
    return sdb.select(query, token);
}

private String skipAhead(String query, int skipLimit)
{
    String tempQuery = replaceClause(query, "SELECT", "count(*)");
    int accumulatedCount = 0;
    String token = "";
    do {
        int tempLimit = skipLimit - accumulatedCount;
        tempQuery = replaceClause(tempQuery , "LIMIT", tempLimit + "");
        Result tempResult = sdb.select(query, token);
        token = tempResult.getToken();
        accumulatedCount += tempResult.getCount();
    } while (accumulatedCount < skipLimit);
    return token;
}

private int extractLimitValue(String query) {...}
private String replaceClause(String query, String clause, String value){...}

这是没有错误处理的总体思路,并适用于任意的页面,但不包括第1。

This is the general idea without error handling, and works for any arbitrary page, excluding page 1.

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

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