When a query returns many rows, it would be inefficient to return them as a single response message. Instead, the driver breaks the results into pages which get returned as they are needed.
The page size specifies how many rows the server will return in each network frame. You can set it in the configuration:
datastax-java-driver.basic.request.page-size = 5000
It can be changed at runtime (the new value will be used for requests issued after the change). If you have categories of queries that require different page sizes, use configuration profiles.
Note that the page size is merely a hint; the server will not always return the exact number of rows, it might decide to return slightly more or less.
The fetch size limits the number of results that are returned in one page; if you iterate past that, the driver uses background queries to fetch subsequent pages. Here's an example with a fetch size of 20:
ResultSet rs = session.execute("SELECT * FROM my_table WHERE k = 1"); for (Row row : rs) { // process the row }
client Session Cassandra --+--------------+---------------------------------+----- |execute(query)| | |------------->| | | | query rows 1 to 20 | | |-------------------------------->| | | | | |create | | |------>ResultSet | | | | +-----+--------+-----------------+-+ | |For i in 1..20| | | | +--------------+ | | | | | get next row | | | | |------------------------->| | | | | row i | | | | |<-------------------------| | | | | | | | +-----+--------------------------+-+ | | | | | | | | get next row | | |------------------------->| | | | query rows 21 to 40 | | |-------------------->| | row 21 | | |<------------------------ | |
In previous versions of the driver, the synchronous and asynchronous APIs returned the same ResultSet
type. This made asynchronous paging very tricky, because it was very easy to accidentally trigger background synchronous queries (which would defeat the whole purpose of async, or potentially introduce deadlocks).
To avoid this problem, the driver‘s asynchronous API now returns a dedicated AsyncResultSet; iteration only yields the current page, and the next page must be explicitly fetched. Here’s the idiomatic way to process a result set asynchronously:
CompletionStage<AsyncResultSet> futureRs = session.executeAsync("SELECT * FROM myTable WHERE id = 1"); futureRs.whenComplete(this::processRows); void processRows(AsyncResultSet rs, Throwable error) { if (error != null) { // The query failed, process the error } else { for (Row row : rs.currentPage()) { // Process the row... } if (rs.hasMorePages()) { rs.fetchNextPage().whenComplete(this::processRows); } } }
See Asynchronous programming for more tips about the async API.
Sometimes it is convenient to interrupt paging and resume it later. For example, this could be used for a stateless web service that displays a list of results with a link to the next page. When the user clicks that link, we want to run the exact same query, except that the iteration should start where we stopped the last time.
The driver exposes a paging state for that:
ResultSet rs = session.execute("your query"); ByteBuffer pagingState = rs.getExecutionInfo().getPagingState(); // Finish processing the current page while (rs.getAvailableWithoutFetching() > 0) { Row row = rs.one(); // process the row } // Later: SimpleStatement statement = SimpleStatement.builder("your query").setPagingState(pagingState).build(); session.execute(statement);
Note the loop to finish the current page after we extract the state. The new statement will start at the beginning of the next page, so we want to make sure we don't leave a gap of unprocessed rows.
The paging state can only be reused with the exact same statement (same query string, same parameters). It is an opaque value that is only meant to be collected, stored and re-used. If you try to modify its contents or reuse it with a different statement, the results are unpredictable.
Saving the paging state works well when you only let the user move from one page to the next. But it doesn‘t allow random jumps (like “go directly to page 10”), because you can’t fetch a page unless you have the paging state of the previous one. Such a feature would require offset queries, but they are not natively supported by Cassandra (see CASSANDRA-6511). The rationale is that offset queries are inherently inefficient (the performance will always be linear in the number of rows skipped), so the Cassandra team doesn't want to encourage their use.
If you really want offset queries, you can emulate them client-side. You‘ll still get linear performance, but maybe that’s acceptable for your use case. For example, if each page holds 10 rows and you show at most 20 pages, it means that in the worst case you'll fetch 190 extra rows, which is probably not a big performance hit.
For example, if the page size is 10, the fetch size is 50, and the user asks for page 12 (rows 110 to 119):
You‘ll want to experiment with the fetch size to find the best balance: too small means many background queries; too big means bigger messages and too many unneeded rows returned (we picked 50 above for the sake of example, but it’s probably too small -- the default is 5000).
Again, offset queries are inefficient by nature. Emulating them client-side is a compromise when you think you can get away with the performance hit. We recommend that you:
The driver examples include two complete web service implementations demonstrating forward-only and random (offset-based) paging.