Neo4j Cypher: How to iterate over ExecutionResult result

前端 未结 4 1490
深忆病人
深忆病人 2020-12-16 22:02

In this code, how could I iterate over all the nodes in the ExecutionResult result?

CypherParser parser = new CypherParser();
ExecutionEngine engine = new Ex         


        
4条回答
  •  情书的邮戳
    2020-12-16 22:34

    The javadoc for Cypher isn't very clear about this, possibly because there isn't any.

    So I re-created your code in a "trial" that demonstrates how to iterate over the properties of nodes in the match. The domain is kinds of fruit, where each kind is linked to the "fruit" node. The relevant snippet is this, after running the query:

        Iterator kindsOfFruit = result.columnAs("x");
        while (kindsOfFruit.hasNext()) {
            Node kindOfFruit = kindsOfFruit.next();
            System.out.println("Kind #" + kindOfFruit.getId());
            for (String propertyKey : kindOfFruit.getPropertyKeys()) {
                System.out.println("\t" + propertyKey + " : " +
                   kindOfFruit.getProperty(propertyKey));
            }
        }
    

    It's the result.columnAs("x") that is the key. The cleverly named String n parameter refers to a "column name" in the result clause. In this example we want the "x" column and we expect it to contain Node objects, so we can assign straight to an Iterator and then use that.

    If the column can't be found, we'll get an org.neo4j.graphdb.NotFoundException.

    If we ask for assignment to the wrong class, we'll get the usual java.lang.ClassCastException.

    The full working example is available here: https://github.com/akollegger/neo4j-trials/blob/master/src/test/java/org/akollegger/neo4j/trials/richardw/ExecutionResultIteratorTrial.java

    Hope that helps.

    Cheers, Andreas

提交回复
热议问题