Search code examples
javaneo4jdriver

How to stream records by batch with neo4j java driver 4.0.0?


Let's say I have a query to a neo4j procedure which should return 100 000 records. What I would like is a way to get my records by batches of 1000. I don't want to wait for the 100 000 records to be all sent by Neo4j, I would like to control on client side the number of records I receive. I have noticed there is a CompletionStage<Record> nextAsync() method in ResultCursor, which is doing what I want, but only one record by one. This is not optimal, as it would need too many round trips between the client and neo4j to get all the records. Is there a way to ask for the next N records ?

I am using neo4j java driver 4.0.0 in async mode.


Solution

  • [UPDATED]

    In neo4j 4.0+, the nextAsync() method internally gets the records in batches for you.

    Its implementation returns each record from a queue that is asynchronously replenished, in batches, whenever the queue size falls below a low watermark.

    So, the nextAsync() method is pretty optimal and should normally get the next record very quickly from the queue.