Reputation: 577
I am using Neo4j graph database and the Neo4j Cypher api of java to get the query result.A dump to string method will give the execution result in the string format.
GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase("C:/TPNeo4jDB");
ExecutionEngine execEngine = new ExecutionEngine(graphDb);
ExecutionResult execResult = execEngine.execute("MATCH (java:JAVA) RETURN java");
String results = execResult.dumpToString();
System.out.println(results);
The output string will look like this.
+--------------------------+
| a |
+--------------------------+
| Node[12]{type:"java 1"} |
| Node[13]{type:"java 2"} |
+--------------------------+
2 rows
Is there any default apis to parse this in a generic way?
As per the answer i have done this
ExecutionResult execResult = execEngine.execute("MATCH (java:JAVA) RETURN java");
Iterator<Node> n_column = execResult2.columnAs( "java" );
for ( Node node : IteratorUtil.asIterable( n_column ) )
{
// note: we're grabbing the name property from the node,
// not from the n.name in this case.
String nodeResult = node + ": " + node.getProperty( "type" );
System.out.println("noderesult :"+nodeResult);
}
It gives result for the first value after which it throws exception.How to solve this?
noderesult :Node[12]: java 1
Jun 12, 2015 7:21:03 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/BOSBOT] threw exception [Request processing failed; nested exception is org.neo4j.graphdb.NotInTransactionException] with root cause
org.neo4j.graphdb.NotInTransactionException
at org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.assertInUnterminatedTransaction(ThreadToStatementContextBridge.java:71)
at org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.getTopLevelTransactionBoundToThisThread(ThreadToStatementContextBridge.java:104)
at org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.getKernelTransactionBoundToThisThread(ThreadToStatementContextBridge.java:111)
at org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.get(ThreadToStatementContextBridge.java:64)
at org.neo4j.kernel.InternalAbstractGraphDatabase$8.statement(InternalAbstractGraphDatabase.java:748)
at org.neo4j.kernel.impl.core.NodeProxy.getProperty(NodeProxy.java:387)
Upvotes: 1
Views: 336
Reputation: 19373
Yes, you can do this:
Iterator<Node> javaNodes = execResult.columnAs( "java");
for (Node node : IteratorUtil.asIterable(javaNodes))
{
//do something with the node
}
More info including what to do when you have many columns is detailed in http://neo4j.com/docs/2.1.8/tutorials-cypher-java.html
Update based on comment:
The Cypher query is MATCH (java:Java) return java
so the column name will be java, and the value returned in that column is a Node
.
If you wanted the type property, then the query would be MATCH (java:Java) return java.type as type
and then you can do a columnAs("type")
. The type of this column will now be a String.
Upvotes: 3