We are tryint to use cassandra to store database. We are not able to page backward/forward in c# datastax driver. Can anyone suggest a methodology to page results in MVC project.
1 Answers
You can use the manual paging feature of the C# driver to page through results from Cassandra. Basically it works like this:
- Before querying the first page, call
SetAutoPage(false)
andSetPageSize(pageSize)
on your statement that's going to get the first page. - Select the first page of results from Cassandra. The
RowSet
that's returned from the query will have aPagingState
property. Save that paging state somewhere (for example, in Session storage or a Cookie) - Later, when you want to get the next page, retrieve the
PagingState
from where you stored it. - Before you query for the next page, call
SetAutoPage(false)
andSetPagingState(yourPagingStateFromStorage)
on the statement to get the next page. - Repeat steps 2-4 for subsequent pages.
The tricky part is paging backwards. If you're caching the results of your paging in your UI (i.e. not doing a full page refresh every time some clicks a button to go to the next/previous page), this isn't an issue since paging backwards is really just moving backwards through the data you've already queried and have cached (probably in an array in your JavaScript code).
If you are doing a full page refresh every time someone clicks to page through results, then you'll need to keep the PagingState
from all pages around until you're sure they're done paging (i.e. store multiple paging state values in something like Session or a cookie). That way, if someone pages backwards you can just lookup the paging state token for that previous page and use it in your query.
Note: If you're in the full page refresh situation, you might also be able to pass the paging state around as a querystring parameter. For example, make the link to the next page "/some/page?pagingState=PAGING_STATE_FROM_ROWSET_CURRENTLY_DISPLAYED" and then having backwards paging just do the same thing as the browser's back button.

- 1,365
- 11
- 8
-
Luke, Thanks for the answer. The document says dont expose the paging state to the end-user. How can we handle this. Will try to implement your suggestion – Nikhil Mysore Apr 30 '16 at 09:02
-
1It's OK to expose the paging state to the end user, just be aware that when you upgrade from one version of Cassandra to another, a paging state that worked in an older version may not work in the newer version. So basically, you don't want paging states persisted somewhere for a long period of time (i.e. one that might span across an upgrade). – Luke Tillman May 01 '16 at 19:36