I have a table "person" with 500 records.
int pageSize = 5;
Select selectStatement=QueryBuilder.select().from("person");
selectSt.setFetchSize(5);
ResultSet results = session.execute(selectStatement);
List<ResultSet> resultsList = new ArrayList<ResultSet>();
int pageCounter = 0;
// I need records that are in page 2 and page 3 only
while(!results.isExhausted())
{
if (results.getAvailableWithoutFetching() <= pageSize )
{
pageCounter ++;
if(pageCounter == 2 || pageCounter == 3) {
resultsList.add(results);
}
}
if(pageCounter>=3) break;
if(!results.isFullyFetched())
results.fetchMoreResults()
}
for (ResultSet r:resultsList) {
Iterator<Row> rowsIterator = r.iterator();
while (rowsIterator.hasNext()) {
Row row = rowsIterator.next();
System.out.println("row:"+row);
}
}
The issue here is when I iterate through row iterator from the resultsList; the row iterator actually starts to iterate from the first record of first page. To get the records which are in page 2 and page 3, I unnecessarily have to iterate through every row this way. Also the first element(first page) of resultsList exhuast all records giving no opportunity for the second element(second page) to iterate. When i get the results from page 2 and page 3 and add them to the resultsList, it is supposed to store only resultset from page 2 and page 3 and not from the first page to all the way to 100 pages.
Can anyone let me know what is that I am doing wrong? I am stuck with this for couple of weeks now.
Thanks. Appreciate the help
int pageSize = 5;
Select selectStatement=QueryBuilder.select().from("person");
selectSt.setFetchSize(5);
ResultSet results = session.execute(selectStatement);
int pageCounter = 0;
// I need records that are in page 2 and page 3 only
while(!results.isExhausted())
{
if (results.getAvailableWithoutFetching() <= pageSize )
{
pageCounter ++;
if(pageCounter == 2 || pageCounter == 3) {//this is the code with issue where the records are printed from first row of page 1
Iterator<Row> rowsIterator = results.iterator();
while (rowsIterator.hasNext()) {
Row row = rowsIterator.next();
System.out.println("row:"+row);
}
}
if(pageCounter>=3) break;
if(!results.isFullyFetched())
results.fetchMoreResults()
}