Pagination with AWS DynamoDB with PHP

Does anyone have any ideas on paginating records from a table. Actually I want to create a paginate component in php with DynamoDb.

It seems that it is impossible to specify the pagination as 1,2,3, 4 , 5 ....

Because Dyanmodb just provides us with a LIMIT clause, with which we can read some not. records, and we can process the next n records by LastEvaluatedKey. Therefore, if I want to go to page 5, how is this possible?

According to my understanding, we cannot display page numbers in pagination. We can just read the specific record limit and provide a NEXT link to get the next n records.

Pagination is the main function of any web application. How can we realize whether moving a cloud database like DynamoDb?

Please provide your opinions and suggestions. Thanks

+4
source share
2 answers

Yes, you are right, in DynamoDB no OFFSET. But using only Limitand 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->_clientrefers to a DynamoDb client object.
Basically, I look through all the entries using LastEvaluatedKeyuntil I get to the page I want.
To get the summary entries in the table, call $this->scan($this->tableName(), [], null, null)['Count'];(that is, without any search criteria and without pagination, just like in the regular pagination function).

0

@Justinas, Dynamo , (.. ). , , LastEvaluatedKey - .

, . , LastEvaluatedKey , , . , :

, , CommentID - -.

 CommentID | Author | Comment | ...
-----------+--------+---------+------------
    1      | Joe    | Foo     | ...
    2      | Joe    | Bar     | ...
    3      | John   | Baz     | ...
    4      | Joe    | FooBar  | ...
    5      | Jane   | BooBaz  | ...
    6      | Joesie | Blah    | ...
    7      | Johnny | Blahaha | ...

, , 3 , LastEvaluatedKey = 3; , , 2, ExclusiveStartKey=3, LastEvaluatedKey = 6; 3, LastEvaluatedKey = 6.. ..

, - ( , 1 2 3). , - . ​​:

 Page | Hash-Key
------+----------
   1  |   null
   2  |     3
   3  |     6
  ..  |    ... 

, . , 3, , , , 6 ExclusiveStartKey.

, , , ( ). , , , , . , , , ( ) .

0

Source: https://habr.com/ru/post/1539102/


All Articles