Question

I have a CF Reviews which have Item ID as row keys and reviews ID as columns and a Review object json as column value.

CF Reviews{
1234:
 234:{[1:"this is a review"],[2:23/12/14],[3:4]}
 567:{[1:"this is a review"],[2:24/12/14],[3:3]}

4567:
 345:{[1:"this is a review"],[2:23/12/14],[3:4]}
 876:{[1:"this is a review"],[2:24/12/14],[3:3]}
}

I wanted to implement pagination on each item ID but as number of columns can be large, I don't want to fetch them at once. So, I wanted to design an API which can take a number range say 200 to 250 and give me sorted reviews according to date or ratings. I found few posts on this, but unfortunately none was helpful.

Was it helpful?

Solution

Paging over columns in Cassandra is not for the weak of heart. If you want to make it possible to page forwards and backwards you need to do a few tricks, such as always reading the column just before and the column after the slice you're interested in, so that you can determine when you're at the beginning or at the end.

I wrote a blog post a few years ago outlining how to do it: http://architecturalatrocities.com/post/13918146722/implementing-column-pagination-in-cassandra

Unfortunately it is not really possible to do the traditional "show items 40 through 60" type of pagination, since Cassandra's rows aren't indexed that way. You can't ask Cassandra for the 40th column.

Instead what you need to do (and this is a high level overview of my blog post, which is too long to repost here) is start at the beginning and ask for the first N + 1 columns. If you got N + 1 columns you're not at the end, so you ask for the N columns starting with the (N + 1)th (e.g. if you got a, b, f, h, g in the first result you ask for columns starting with g). Repeat until you don't get N + 1 columns back, because that means you're at the end.

Supporting paging backwards is a bit more complicated. You need to ask for one more column, and do more bounds checking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top