通过PHP与AWS DynamoDB分页 [英] Pagination with AWS DynamoDB with PHP

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

问题描述

有人对从表中分页记录有任何想法.其实我想用DynamoDb在php中创建一个分页组件.

似乎不可能像

1,2,3, 4 ,5 ...这样的分页. .

因为Dyanmodb只是提供了LIMIT子句,我们可以通过该子句读取某些编号.记录,我们可以通过LastEvaluatedKey处理下n条记录.因此,如果我想直接跳到第5页,怎么可能?

根据我的理解,我们无法在分页中显示页码.我们可以做的就是只读取一定数量的记录,并提供NEXT链接来检索下n条记录.

分页是任何Web应用程序的基本功能,如果迁移到DynamoDb这样的云数据库,我们如何实现?

请提供您的意见和建议.谢谢

解决方案

是的,是的,DynamoDB中没有OFFSET.但是我仅使用LimitLastEvaluatedKey进行了此功能:

public function scan($table, $filter = [], $select = null, $limit = 2)
{
    $page = isset($_GET['page']) ? $_GET['page'] : 0;
    $options = [
        'TableName' => $table,
        'Count' => true,
    ];

    if (!empty($limit)) {
        $options['Limit'] = $limit;
    }

    if (!is_null($select)) {
        $options['Select'] = $select;
    }

    if (!empty($filter)) {
        $options['ScanFilter'] = $filter;
    }

    $results = $results = $this->_client->scan($options);

    while ($page > 0 && isset($results['LastEvaluatedKey'])) {
        $results = $results = $this->_client->scan($options);
        $options['ExclusiveStartKey'] = $results['LastEvaluatedKey'];
        $page--;
    }

    return $results;
}

$this->_client引用DynamoDb客户端对象.
基本上我会遍历所有LastEvaluatedKey条目,直到到达所需页面.
要获取表中的总条目,请调用$this->scan($this->tableName(), [], null, null)['Count'];(即-没有任何搜索条件且没有分页,就像在常规分页功能中一样.)

Have someone any idea about Paginating the records from a table. Actually I want to create a paginate component in php with DynamoDb.

It seems like it is not possible to giving pagination like <first> <prev> 1,2,3,4,5... <next> <last>.

Because Dyanmodb just provide us LIMIT clause by which we can read certain no. of records and we can process next n records by LastEvaluatedKey. So if I want to jump directly to 5th page, How is it possible ?

As per my understanding we can't display page numbers into the pagination. The thing we can do is just read certain limit of records and provide the NEXT link to retrieve next n records.

Pagination is basic feature of any web application, How can we implement if migrating to cloud database like DynamoDb ?

Please provide your views and suggestions. Thanks

解决方案

Yes, you are right, there is no OFFSET in DynamoDB. But using only Limit and LastEvaluatedKey, i made this function:

public function scan($table, $filter = [], $select = null, $limit = 2)
{
    $page = isset($_GET['page']) ? $_GET['page'] : 0;
    $options = [
        'TableName' => $table,
        'Count' => true,
    ];

    if (!empty($limit)) {
        $options['Limit'] = $limit;
    }

    if (!is_null($select)) {
        $options['Select'] = $select;
    }

    if (!empty($filter)) {
        $options['ScanFilter'] = $filter;
    }

    $results = $results = $this->_client->scan($options);

    while ($page > 0 && isset($results['LastEvaluatedKey'])) {
        $results = $results = $this->_client->scan($options);
        $options['ExclusiveStartKey'] = $results['LastEvaluatedKey'];
        $page--;
    }

    return $results;
}

$this->_client refers to DynamoDb client object.
Basically i loop through all entries with LastEvaluatedKey till i reach needed page.
To get total entries in table, call $this->scan($this->tableName(), [], null, null)['Count']; (that is - without any search criteria and without pagination, just as in normal pagination function).

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

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