cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to parse Result object

I'm writing some custom complicated procedures that make some queries that return a single result, but I haven't figured out how to parse my query result from the Result object returned by the query.

For example, I have a Result result = db.execute(query, params). I can see from my log.info(result.resultAsString()) that the query returns the correct result.

| pf.value |
+----------+
| 29480    |
+----------+
1 row

However, Long pop = (Long) result.columnAs("pf.value").next(); returns an error java.util.NoSuchElementException: next on empty iterator

How do I get my value out of the Result object?

2 REPLIES 2

Gah! Of course, the "Suggested Topics" after I posted lead me to a post that gave me the example I needed.

The quick answer is:

Map<String, Object> map = db.execute(query, params).next();
Long pop = (Long) map.get("pf.value");

But, I'm still curious how to use the result.columnAs() method. I'm obviously reading the API docs wrong.

result.resultAsString() consumes all results, so by logging this you can no longer iterate through the result set. From the Result.java documentation:

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:

  • columns()
  • getQueryStatistics()
  • getQueryExecutionType()
  • getExecutionPlanDescription()