In this example we are going to create a Rest API where we will return paginated list of users. The important point here is that, the response will contain a key called next which represent the “next page”. There won’t be a “previous page” option. However, if you wish to go backwards as well, I added an example solution/suggestion at the end of the post.

Pagination requires us to expose Cassandra query’s “page state” value. When the client sends a new request this value will be used in the query to fetch the next page. Everytime we run the query, this value will be updated and returned to the client so that they can use it in next request. By the way, encrypted version of this value is being transmitted for security reasons.

Cassandra keyspace

DROP KEYSPACE IF EXISTS blog;

CREATE KEYSPACE IF NOT EXISTS blog
WITH replication = {
    'class': 'NetworkTopologyStrategy',
    'datacenter1': 1
};

DROP TABLE IF EXISTS blog.users;

CREATE TABLE IF NOT EXISTS blog.users (
    id int,
    username text,    
    created_at timestamp,
    PRIMARY KEY (username, id)
);

#cassandra #go #golang

Cassandra Pagination Example with Golang
5.10 GEEK