Cassandra pagination with asp.net core C#

This article is about the pagination with infinite scroll and will not cover the pagination with page numbers.

First, let’s start with the cqlsh command to create the required table. We will create a users table and insert a few lines of data

CREATE TABLE users
(Code text,
Name text,
Accesstime TIMESTAMP,
PRIMARY KEY(Code, Accesstime));
Insert Into users (Code, Name,Accesstime) Values( ‘1111’,’CarbonTsunami’,’2021–06–06 15:12:51');
Insert Into users (Code, Name,Accesstime) Values( ‘2222’,’Bhone’,’2021–06–06 15:13:51');
Insert Into users (Code, Name,Accesstime) Values( ‘3333’,’Myint’,’2021–06–06 15:14:51');
Insert Into users (Code, Name,Accesstime) Values( ‘4444’,’Kyaw’,’2021–06–06 15:15:51');
Insert Into users (Code, Name,Accesstime) Values( ‘5555’,’David’,’2021–06–06 15:16:51');
User list

Once we have created the table, let’s go and code some C# for the pagination.

We will use Cassandra.Mapping.Mapper to run the queries against a Cassandra cluster.

usermapper

After that, we will create getuser API to test the pagination. In this api we will accept pageinfo and pageSize from the client. Pageinfo is the token generated from the Cassandra.Mapper.IPage to retrieve the next page of results. Moreover, we will convert the token to string format to send back to the client.

getuser API with pagination

After we have finished our code, let’s test it.

In the first time request, we have no information about the pageinfo and we will request with empty data of pageinfo. And in the next requests, we need to use data from page_result to get the result of the next pages.

Request one (page 1)
https://localhost:44344/api/test/getuser?pageinfo=&pageSize=2
Response with page_result to be used in next request{"users":[{"Code":"1111","Name":"CarbonTsunami","Accesstime":"2021-06-06T15:12:51"},{"Code":"4444","Name":"Kyaw","Accesstime":"2021-06-06T15:15:51"}],"page_result":"AQIAAABlAAAAeyJ0b2tlbiI6IkNCQUFBQUFpY0Vtbnc2V2lXbGg5cmdiWDdTQmtDQWtBQUFBVEFBQUJlZUhrL0ZnSUJBQUFBQUlHQndjIiwicmFuZ2UiOnsibWluIjoiIiwibWF4IjoiRkYifX0="}Request two (page 2)
https://localhost:44344/api/test/getuser?pageinfo=AQIAAABlAAAAeyJ0b2tlbiI6IkNCQUFBQUFpY0Vtbnc2V2lXbGg5cmdiWDdTQmtDQWtBQUFBVEFBQUJlZUhrL0ZnSUJBQUFBQUlHQndjIiwicmFuZ2UiOnsibWluIjoiIiwibWF4IjoiRkYifX0=&pageSize=2
Response with page_result to be used in next request{"users":[{"Code":"2222","Name":"Bhone","Accesstime":"2021-06-06T15:13:51"},{"Code":"5555","Name":"David","Accesstime":"2021-06-06T15:16:51"}],"page_result":"AQQAAABlAAAAeyJ0b2tlbiI6IkNCQUFBQUFrZmlNNVFSblpvaTZQQ014Y0d1M1RDQWtBQUFBVEFBQUJlZUhsNXJnSUJBQUFBQUlHQndjIiwicmFuZ2UiOnsibWluIjoiIiwibWF4IjoiRkYifX0="}

Now we have successfully created the Cassandra pagination with asp.net core.