Cassandra - Resuming pagination

less than 1 minute read

Cassandra provides the page state information and it can be reused to get next iteration.

1
2
3
4
5
6
7
Statement stmt = QueryBuilder
                    .select()
                    .all()
                    .from("users")
                    .where(eq("email", emailUpper));
ResultSet resultSet = getSession().execute(stmt);
PagingState pagingState = resultSet.getExecutionInfo().getPagingState();

Then it can be serialized in below way

1
2
String savedStringState = pagingState.toString();
byte[] savedBytesState = pagingState.toBytes();

It can be reconstructed and resume the query as shown following example.

1
2
3
4
5
6
7
8
9
10
11
PagingState pagingState = PagingState.fromString(string);
Statement stmt = QueryBuilder
                    .select()
                    .all()
                    .from("users")
                    .where(eq("email", emailUpper));
PagingState pagingState = resultSet.getExecutionInfo().getPagingState();
// restore state from the saved string
PagingState pagingState = PagingState.fromString(savedStringState);
stmt.setPagingState(pagingState);
ResultSet resultSet = getSession().execute(stmt);