I have been going through a number of articles trying to find the best way to traverse through all records in my Cassandra Database row-wise.
The requirement is to implement pagination for my application which reads records from Cassandra database based on row keys and displays links of upto 10 pages on the UI.
I am using Hector client for JAVA for my application, and the requirement can be roughly depicted as follows:
Page 1 Row key key4 to key13
Page 2 Row key keyl4 to key23
. .
. .
. .
Page last Row key key94 to key103
I found these links very useful:
http://blog.dynatrace.com/2011/12/05/pagination-with-cassandra-and-what-we-can-learn-from-it/
https://github.com/rantav/hector/wiki/User-Guide
Here is the code:
The requirement is to implement pagination for my application which reads records from Cassandra database based on row keys and displays links of upto 10 pages on the UI.
I am using Hector client for JAVA for my application, and the requirement can be roughly depicted as follows:
Page 1 Row key key4 to key13
Page 2 Row key keyl4 to key23
. .
. .
. .
Page last Row key key94 to key103
I found these links very useful:
http://blog.dynatrace.com/2011/12/05/pagination-with-cassandra-and-what-we-can-learn-from-it/
https://github.com/rantav/hector/wiki/User-Guide
Here is the code:
import java.util.List;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.ColumnSlice;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.OrderedRows;
import me.prettyprint.hector.api.beans.Row;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.RangeSlicesQuery;
import me.prettyprint.hector.api.query.QueryResult;
import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;
public class PageRecords {
private static final StringSerializer se = new StringSerializer();
private static Cluster cluster= getOrCreateCluster("Test Cluster2", "localhost:9160");
private static Keyspace keyspace = HFactory.createKeyspace("KS1", cluster);
public static void main(String args[]) throws Exception{
RangeSlicesQuery<String, String, String> rangeSlicesQuery =
HFactory.createRangeSlicesQuery(keyspace, se, se,
se);
rangeSlicesQuery.setColumnFamily("CF1");
rangeSlicesQuery.setKeys("", ""); //set blank as start and end keys
rangeSlicesQuery.setRange("", "", false, 3); //no. of columns
rangeSlicesQuery.setRowCount(11); //get 11 to get start key for next page
QueryResult<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
OrderedRows<String, String, String> orderedRows = result.get();
List<Row<String, String, String>> listRows = orderedRows.getList();
for(int i=1; i< 11; i++){
System.out.println("***************page "+ i+" ****************");
result = rangeSlicesQuery.execute();
orderedRows = result.get();
listRows = orderedRows.getList();
for(int j=0; j< listRows.size()-1;j++ ){
Row<String, String, String> row = listRows.get(j);
String key = row.getKey();
System.out.println(key);
ColumnSlice<String, String> colSlice=row.getColumnSlice();
List<HColumn<String, String>> colList= colSlice.getColumns();
for(int k=0; k<colList.size();k++){
System.out.println(colList.get(k).getName() + " : "+colList.get(k).getValue());
}
}
Row<String,String,String> lastRow = orderedRows.peekLast(); //get last key
rangeSlicesQuery.setKeys(lastRow.getKey(), ""); //set last key as start key for next page
}
}
}
PS: this is just a sample code based on my initial analysis, comments/suggestions for improvement are welcomed..import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.ColumnSlice;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.OrderedRows;
import me.prettyprint.hector.api.beans.Row;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.RangeSlicesQuery;
import me.prettyprint.hector.api.query.QueryResult;
import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;
public class PageRecords {
private static final StringSerializer se = new StringSerializer();
private static Cluster cluster= getOrCreateCluster("Test Cluster2", "localhost:9160");
private static Keyspace keyspace = HFactory.createKeyspace("KS1", cluster);
public static void main(String args[]) throws Exception{
RangeSlicesQuery<String, String, String> rangeSlicesQuery =
HFactory.createRangeSlicesQuery(keyspace, se, se,
se);
rangeSlicesQuery.setColumnFamily("CF1");
rangeSlicesQuery.setKeys("", ""); //set blank as start and end keys
rangeSlicesQuery.setRange("", "", false, 3); //no. of columns
rangeSlicesQuery.setRowCount(11); //get 11 to get start key for next page
QueryResult<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
OrderedRows<String, String, String> orderedRows = result.get();
List<Row<String, String, String>> listRows = orderedRows.getList();
for(int i=1; i< 11; i++){
System.out.println("***************page "+ i+" ****************");
result = rangeSlicesQuery.execute();
orderedRows = result.get();
listRows = orderedRows.getList();
for(int j=0; j< listRows.size()-1;j++ ){
Row<String, String, String> row = listRows.get(j);
String key = row.getKey();
System.out.println(key);
ColumnSlice<String, String> colSlice=row.getColumnSlice();
List<HColumn<String, String>> colList= colSlice.getColumns();
for(int k=0; k<colList.size();k++){
System.out.println(colList.get(k).getName() + " : "+colList.get(k).getValue());
}
}
Row<String,String,String> lastRow = orderedRows.peekLast(); //get last key
rangeSlicesQuery.setKeys(lastRow.getKey(), ""); //set last key as start key for next page
}
}
}