简体   繁体   中英

Cassandra pagination

I have a table in Cassandra with 1 million records. I want to fetch 100 records at a time, so if I fetch the first 100, the next fetch should start from the item 101. How do I get this kind of pagination? I also used PagingState but it did not work.

My code is the following:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.datastax.driver.core.PagingState;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;

/**
 * 
 * The solution of skipping rows is that use page state rather than iterator
 * rows one by one.
 *
 */
public class CassandraPaging {

    private Session session;

    public CassandraPaging(Session session) {
        this.session = session;
    }

    /**
     * Retrieve rows for the specified page offset.
     * 
     * @param statement
     * @param start
     *            starting row (>1), inclusive
     * @param size
     *            the maximum rows need to retrieve.
     * @return List<Row>
     */
    public List<Row> fetchRowsWithPage(Statement statement, int start, int size) {
        ResultSet result = skipRows(statement, start, size);
        return getRows(result, start, size);
    }

    private ResultSet skipRows(Statement statement, int start, int size) {
        ResultSet result = null;
        int skippingPages = getPageNumber(start, size);
        String savingPageState = null;
        statement.setFetchSize(size);
        boolean isEnd = false;
        for (int i = 0; i < skippingPages; i++) {
            if (null != savingPageState) {
                statement = statement.setPagingState(PagingState
                        .fromString(savingPageState));
            }
            result = session.execute(statement);
            PagingState pagingState = result.getExecutionInfo()
                    .getPagingState();
            if (null != pagingState) {
                savingPageState = result.getExecutionInfo().getPagingState()
                        .toString();
            }

            if (result.isFullyFetched() && null == pagingState) {
                // if hit the end more than once, then nothing to return,
                // otherwise, mark the isEnd to 'true'
                if (true == isEnd) {
                    return null;
                } else {
                    isEnd = true;
                }
            }
        }
        return result;
    }

    private int getPageNumber(int start, int size) {
        if (start < 1) {
            throw new IllegalArgumentException(
                    "Starting row need to be larger than 1");
        }
        int page = 1;
        if (start > size) {
            page = (start - 1) / size + 1;
        }
        return page;
    }

    private List<Row> getRows(ResultSet result, int start, int size) {
        List<Row> rows = new ArrayList<>(size);
        if (null == result) {
            return rows;
        }
        int skippingRows = (start - 1) % size;
        int index = 0;
        for (Iterator<Row> iter = result.iterator(); iter.hasNext()
                && rows.size() < size;) {
            Row row = iter.next();
            if (index >= skippingRows) {
                rows.add(row);
            }
            index++;
        }
        return rows;
    }
}

This is the main method:

public static void main(String[] args) {
    Cluster cluster = null;
    Session session = null;

    try {
        cluster = Cluster.builder().addContactPoint("localhost").withPort(9042).build();
        session = cluster.connect("mykeyspace");

        Statement select = QueryBuilder.select().all().from("mykeyspace", "Mytable");

        CassandraPaging cassandraPaging = new CassandraPaging(session);
        System.out.println("*************First Page1 **************");
        List<Row> firstPageRows = cassandraPaging.fetchRowsWithPage(select, 1, 5);
        printUser(firstPageRows);

        System.out.println("*************Second Page2 **************");
        List<Row> secondPageRows = cassandraPaging.fetchRowsWithPage(select, 6, 5);
        printUser(secondPageRows);

        System.out.println("*************Third Page3 **************");
        List<Row> thirdPageRows = cassandraPaging.fetchRowsWithPage(select, 6, 5);
        printUser(thirdPageRows);

        cluster.close();
        session.close();

    } catch(Exception exp) {
        exp.printStackTrace();
    } finally {
        cluster.close();
        session.close();
    }
}

private static void printUser(final List<Row> inRows) {
    for (Row row : inRows) {
        System.out.println("Id is:" + row.getUUID("id"));
        System.out.println("Name is:" + row.getInt("name"));
        System.out.println("account is:" + row.getString("account"));
    }
}
  /*First, get the number of page states with page limit size (in my case 25):*/

 int n=0;
 PagingState pageStates=null;
 Map<Integer, PagingState> stringMap=new HashMap<Integer, PagingState>();
 do{
    Statement select = QueryBuilder.select().all().from("keyspace", "tablename").setFetchSize(25).setPagingState(pageStates);
    ResultSet resultSet=session.execute(select);
    pageStates=resultSet.getExecutionInfo().getPagingState();
    stringMap.put(++n,pageStates);
 }while (pageStates!=null);


 /*Then, find page index -> get the exact page state -> pass it in query
 ========================================================================
 1.Get the page number
 2.calculate the offset with pagelimit(in my case 25)
 3.get the pageindex
 4. pass pagestate of appropriate page index in query */


 int pagenumber ;                      
 int offset = (pagenumber * 25) - 25;  
 int pageindex=(offset/25)-1;        
 Statement selectq = QueryBuilder.select().all().from("keyspace", "tablename").setPagingState(stringMap.get(pageindex));  
 ResultSet resultSet = session.execute(selectq);
 fourthPageRows=cassandraPaging.getRows(resultSet,offset,25);

To use the following solution you need spring-data dependencies in your class path.

Spring provides PageRequest which is an implementation of Pageable , that acceps pageNo and size (no of records to show on a page).

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

PageRequest(int page, int size)

Example

Creating Repo.
For creating repo use the org.springframework.data.repository.PagingAndSortingRepository

class CasandraRepo extends PagingAndSortingRepository{

}

//use this pageReq in repository.findAll , as shown below;

Pageable pageReq = new PageRequest(0, 10);
CasandraRepo  repo;
repo.findAll(pageReq);

To achieve this, you would need to import the spring-cassandra-data dependency in your project.

Simple PageRequest can not be used to get the pageable object as when we send a page other than 0 (or first page). It throws an exception: "Cannot create a Cassandra page request for an indexed page other than the first page (0)."

Use CassandraPageRequest like this:

private static final int PAGE = 0;
private static final String DEFAULT_CURSOR_MARK = "-1";
private static final String SORT_FIELD = "test_name";

public TestResponse getData(int pageSize, String cursorMark) {

    Pageable pageable = CassandraPageRequest.of(PageRequest.of(PAGE, pageSize, Sort.by(Sort.Direction.DESC, SORT_FIELD)), DEFAULT_CURSOR_MARK.equalsIgnoreCase(
            cursorMark) ? null : PagingState.fromString(cursorMark));

    Slice<Test> testSlice = testRepository.findAll(pageable);

    TestResponse testResponse = new TestResponse();
    testResponse.setRecords(testSlice.getContent());

    if(!testSlice.isLast()) {
        testResponse.setNextCursorMark(((CassandraPageRequest)testSlice.getPageable()).getPagingState().toString());
    } else {
        testResponse.setNextCursorMark(DEFAULT_CURSOR_MARK);
    }

    return testResponse;
}

PAGE will remain 0 for all the further requests as it has no significance when we pass cursorMark (or PagingState in Cassandra).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM