Skip to content

Instantly share code, notes, and snippets.

@mgezkaya
Created February 18, 2019 11:59
Show Gist options
  • Save mgezkaya/f90e19e8e86e35cabcf22bd97325cf3a to your computer and use it in GitHub Desktop.
Save mgezkaya/f90e19e8e86e35cabcf22bd97325cf3a to your computer and use it in GitHub Desktop.
Cassandra Forward Paging With Spring 2.x
public PagedData findUsersByActivationStatusWithPaging(UserActivationStatus activationStatus, String pagingState) {
logger.info("Finding users by activation status {}...", activationStatus);
Select select = QueryBuilder
.select()
.from("user");
select.where(eq("activationStatus", activationStatus.name()));
select.setFetchSize(pagingFetchSize);
if (pagingState != null) {
select.setPagingState(PagingState.fromString(pagingState));
}
Slice<User> users = cassandraTemplate.slice(select, User.class);
logger.info("Found {} users with/by activation status {}", users.getContent().size(), activationStatus);
if(users.hasNext()) {
CassandraPageRequest next = (CassandraPageRequest) users.nextPageable();
return new PagedData(next.getPagingState().toString(), users.hasNext(), users.getContent());
}else{
return new PagedData(null,false,users.getContent());
}
}
@mgezkaya
Copy link
Author

Cassandra supports forward pagination which means you can fetch first n rows then you can fetch rows between n+1 and 2n and so on until your data ends but you can't fetch rows between n+1 and 2n directly. In this code block I have showed how to use forward pagination with spring data cassandra 2.x. I hope it helps you well.

@ramazanapa20
Copy link

thank u so much you saved my life

@bnsk
Copy link

bnsk commented Oct 3, 2019

I want to wait infinitely for new inserts into the table and as soon as the result set equals the batch size, I want to consume it. Can you please let me know how to implement this scenario?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment