我使用Neo4j图形数据库和java的Neo4j Cypher api来获取查询结果。转储到字符串方法将以字符串格式给出执行结果。
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);
输出字符串将如下所示。
+--------------------------+
| a |
+--------------------------+
| Node[12]{type:"java 1"} |
| Node[13]{type:"java 2"} |
+--------------------------+
2 rows
是否有任何默认的apis以通用的方式解析它?
根据我已经完成的答案
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);
}
它给出了引发异常的第一个值的结果。如何解决这个问题?
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)
答案 0 :(得分:3)
是的,你可以这样做:
Iterator<Node> javaNodes = execResult.columnAs( "java");
for (Node node : IteratorUtil.asIterable(javaNodes))
{
//do something with the node
}
详细介绍了包含许多列时要执行的操作的详细信息 http://neo4j.com/docs/2.1.8/tutorials-cypher-java.html
根据评论更新:
Cypher查询为MATCH (java:Java) return java
,因此列名称为java,该列中返回的值为Node
。
如果您想要type属性,那么查询将为MATCH (java:Java) return java.type as type
,然后您可以执行columnAs("type")
。此列的类型现在是String。