Results pagination in Cassandra (CQL)

后端 未结 6 1783
一生所求
一生所求 2020-12-08 04:33

I am wondering how can I achieve pagination using Cassandra.

Let us say that I have a blog. The blog lists max 10 posts per page. To access next posts a user must cl

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 04:54

    If you read this doc "Use paging state token to get next result",

    https://datastax.github.io/php-driver/features/result_paging/

    We can use "paging state token" to paginate at application level. So PHP logic should look like,

    withContactPoints('127.0.0.1')->build();
    $session   = $cluster->connect("simplex");
    $statement = new Cassandra\SimpleStatement("SELECT * FROM paging_entries Limit ".($limit+$offset));
    
    $result = $session->execute($statement, new Cassandra\ExecutionOptions(array('page_size' => $offset)));
    // Now $result has all rows till "$offset" which we can skip and jump to next page to fetch "$limit" rows.
    
    while ($result->pagingStateToken()) {
        $result = $session->execute($statement, new Cassandra\ExecutionOptions($options = array('page_size' => $limit,'paging_state_token' => $result->pagingStateToken())));
        foreach ($result as $row) {
          printf("key: '%s' value: %d\n", $row['key'], $row['value']);
        }
    }
    ?>
    

提交回复
热议问题