You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 5.0 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Querying: How to stream query results?

Query results can be streamed using stream method from advanced() session operations.

Syntax

public <T> CloseableIterator<StreamResult<T>> stream(IRavenQueryable<T> query);

public <T> CloseableIterator<StreamResult<T>> stream(IRavenQueryable<T> query, Reference<QueryHeaderInformation> queryHeaderInformation);

public <T> CloseableIterator<StreamResult<T>> stream(IDocumentQuery<T> query);

public <T> CloseableIterator<StreamResult<T>> stream(IDocumentQuery<T> query, Reference<QueryHeaderInformation> queryHeaderInformation);
Parameters
query IQueryable or IDocumentQuery Query to stream results for.
queryHeaderInformation QueryHeaderInformation Information about performed query.
Return Value
CloseableIterator<StreamResult<T>> Iterator with entities.
QueryHeaderInformation Information about performed query.

Example I

QEmployee e = QEmployee.employee;
IRavenQueryable<Employee> query = session
  .query(Employee.class)
  .where(e.firstName.eq("Robert"));

try (CloseableIterator<StreamResult<Employee>> results = session.advanced().stream(query)) {
  while (results.hasNext()) {
    StreamResult<Employee> employee = results.next();
  }
}

Example II

QEmployee e = QEmployee.employee;
IDocumentQuery<Employee> query = session
  .advanced()
  .documentQuery(Employee.class)
  .whereEquals(e.firstName, "Robert");

Reference<QueryHeaderInformation> queryHeaderInfoRef = new Reference<>();
try (CloseableIterator<StreamResult<Employee>> results = session.advanced().stream(query, queryHeaderInfoRef)) {
  while (results.hasNext()) {
    StreamResult<Employee> employee = results.next();
  }
}