简体   繁体   English

在java中解析Neo4j ExecutionResult dumptoString

[英]Parsing Neo4j ExecutionResult dumptoString in java

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. 我使用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);

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? 是否有任何默认的apis以通用的方式解析它?

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)

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 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 . Cypher查询是MATCH (java:Java) return java因此列名将是java,并且该列中返回的值是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") . 如果你想要type属性,那么查询将是MATCH (java:Java) return java.type as type ,然后你可以做一个columnAs("type") The type of this column will now be a String. 此列的类型现在是String。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM