使用 PHP 使用 AWS DynamoDB 进行分页
有人知道对表中的记录进行分页.其实我想用 DynamoDb 在 php 中创建一个分页组件.
似乎不可能像
因为 Dyanmodb 只是为我们提供了 LIMIT 子句,通过它我们可以读取某些数字.的记录,我们可以通过 LastEvaluatedKey 处理下 n 条记录.所以如果我想直接跳到第5页,怎么可能?
据我了解,我们无法在分页中显示页码.我们可以做的就是读取一定数量的记录,并提供 NEXT 链接来检索下 n 条记录.
分页是任何Web应用程序的基本功能,如果迁移到像DynamoDb这样的云数据库,我们如何实现?
请提供您的意见和建议.谢谢
解决方案是的,你是对的,DynamoDB 中没有 OFFSET
.但是只使用 Limit
和 LastEvaluatedKey
,我做了这个函数:
公共函数扫描($table, $filter = [], $select = null, $limit = 2){$page = isset($_GET['page']) ?$_GET['page'] : 0;$选项= ['表名' =>$表,'计数' =>真的,];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'];$页--;}返回$结果;}
$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).
相关文章