public interface Result extends org.neo4j.graphdb.ResourceIterator<Map<String,Object>>
executing
a query.
The result is comprised of a number of rows, potentially computed lazily, with this result object being an iterator
over those rows. Each row is represented as a
, the
keys in this map are the names of the columns in the row, as specified by the Map
<String
, Object
>return
clause of the query,
and the values of the map is the corresponding computed value of the expression in the return
clause. Each
row will thus have the same set of keys, and these keys can be retrieved using the
columns-method.
To ensure that any resource, including transactions bound to the query, are properly freed, the result must either be fully exhausted, by means of the iterator protocol, or the result has to be explicitly closed, by invoking the close-method.
Idiomatic use of the Result object would look like this:
try ( Result result = graphDatabase.execute( query, parameters ) )
{
while ( result.hasNext() )
{
Map<String, Object> row = result.next();
for ( String key : result.columns() )
{
System.out.printf( "%s = %s%n", key, row.get( key ) );
}
}
}
If the result consists of only a single column, or if only one of the columns is of interest, a projection can be
extracted using columnAs(String)
. This produces a new iterator over the values of the named column. It
should be noted that this iterator consumes the rows of the result in the same way as invoking next()
on
this object would, and that the close-method
on either iterator has the same effect. It is thus
safe to either close the projected column iterator, or this iterator, or both if all rows have not been consumed.
In addition to the iteration methods
on this interface, close()
, and the
column projection method
, there are two methods for getting a string representation of the
result that also consumes the entire result if invoked. resultAsString()
returns a single string
representation of all (remaining) rows in the result, and writeAsStringTo(PrintWriter)
does the same, but
streams the result to the provided PrintWriter
instead, without allocating large string objects.
The methods that do not consume any rows from the result, or in other ways alter the state of the result are safe to invoke at any time, even after the result has been closed or fully exhausted. These methods are:
Not all queries produce an actual result, and some queries that do might yield an empty result set. In order to
distinguish between these cases the QueryExecutionType
of this result
can be queried.
Modifier and Type | Interface and Description |
---|---|
static interface |
Result.ResultRow
Describes a row of a result.
|
static interface |
Result.ResultVisitor<VisitationException extends Exception>
This is the visitor interface you need to implement to use the
accept(ResultVisitor) method. |
Modifier and Type | Method and Description |
---|---|
<VisitationException extends Exception> |
accept(Result.ResultVisitor<VisitationException> visitor)
Visits all rows in this Result by iterating over them.
|
void |
close()
Closes the result, freeing up any resources held by the result.
|
<T> org.neo4j.graphdb.ResourceIterator<T> |
columnAs(String name)
Returns an iterator with the result objects from a single column of the result set.
|
List<String> |
columns()
The exact names used to represent each column in the result set.
|
ExecutionPlanDescription |
getExecutionPlanDescription()
Returns a description of the query plan used to produce this result.
|
Iterable<Notification> |
getNotifications()
Provides notifications about the query producing this result.
|
QueryExecutionType |
getQueryExecutionType()
Indicates what kind of query execution produced this result.
|
QueryStatistics |
getQueryStatistics()
Statistics about the effects of the query.
|
boolean |
hasNext()
Denotes there being more rows available in this result.
|
Map<String,Object> |
next()
Returns the next row in this result.
|
void |
remove()
Removing rows from the result is not supported.
|
String |
resultAsString()
Provides a textual representation of the query result.
|
void |
writeAsStringTo(PrintWriter writer)
Provides a textual representation of the query result to the provided
PrintWriter . |
forEachRemaining
QueryExecutionType getQueryExecutionType()
List<String> columns()
<T> org.neo4j.graphdb.ResourceIterator<T> columnAs(String name)
To ensure that any resources, including transactions bound to it, are properly closed, the iterator must
either be fully exhausted, or the close()
method must be
called.
T
- desired type cast for the result objectsname
- exact name of the column, as it appeared in the original queryClassCastException
- when the result object can not be cast to the requested typeNotFoundException
- when the column name does not appear in the original queryboolean hasNext()
void close()
next-method
.close
in interface AutoCloseable
close
in interface org.neo4j.graphdb.Resource
close
in interface org.neo4j.graphdb.ResourceIterator<Map<String,Object>>
QueryStatistics getQueryStatistics()
ExecutionPlanDescription getExecutionPlanDescription()
QueryExecutionType.requestedExecutionPlanDescription()
-method is used.
Being able to invoke this method, regardless of whether the user requested the plan or not is useful for
purposes of debugging queries in applications.String resultAsString()
The execution result represented by this object will be consumed in its entirety after this method is called. Calling any of the other iterating methods on it should not be expected to return any results.
void writeAsStringTo(PrintWriter writer)
PrintWriter
.
The execution result represented by this object will be consumed in its entirety after this method is called. Calling any of the other iterating methods on it should not be expected to return any results.
writer
- the PrintWriter
to receive the textual representation of the query result.void remove()
Iterable<Notification> getNotifications()
<VisitationException extends Exception> void accept(Result.ResultVisitor<VisitationException> visitor) throws VisitationException extends Exception
VisitationException
- the type of the exception that might get thrownvisitor
- the ResultVisitor instance that will see the results of the visit.VisitationException
- if the visit(ResultRow)
method of Result.ResultVisitor
throws such an
exception.VisitationException extends Exception
Copyright © 2002–2016 The Neo4j Graph Database Project. All rights reserved.