Search code examples
javagoogle-app-enginecursorgoogle-cloud-datastore

Setting Cursor for App Engine Datastore using Java


What should the startCursor be set to if I want to query results from my first element to 10th element?

I understand that startCursor should be a Cursor object, but what value should I set it to?

My entity ID's are primitive integers starting from 1.

Please comment if any further information is necessary.


Solution

  • You set a cursor on a query when you have it. Otherwise, you simply don't set it - this will query from the very beginning.

    For example:

    Query q = new Query("Person");
    QueryResultList<Entity> results;
    Cursor cursor = null;
    FetchOptions queryOptions = FetchOptions.Builder.withChunkSize(500);
    
    do {
        if (cursor != null) {
            queryOptions.startCursor(cursor);
        }
        results = datastore.prepare(q).asQueryResultList(queryOptions);
    
        for (Entity entity : results) {
            // do something
        }
        cursor = results.getCursor();
    
    } while (results.size() == 500);