Neo4j Cypher:如何遍历ExecutionResult结果 [英] Neo4j Cypher: How to iterate over ExecutionResult result

查看:554
本文介绍了Neo4j Cypher:如何遍历ExecutionResult结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码中,我如何遍历ExecutionResult结果中的所有节点?

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

CypherParser parser = new CypherParser();
ExecutionEngine engine = new ExecutionEngine( graphDb );
Query query = parser.parse( "START n=node(2) MATCH (n)<-[:IS_A]-(x) RETURN x" );
ExecutionResult result = engine.execute( query );
// iterate over nodes in result and print all properties

推荐答案

Cypher的javadoc不太清楚,可能是因为没有.

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<Node> 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));
        }
    }

result.columnAs("x")是关键.巧妙命名的String n参数在result子句中引用列名".在此示例中,我们需要"x"列,并且希望它包含Node对象,因此我们可以直接将其分配给Iterator<Node>,然后使用它.

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<Node> and then use that.

如果找不到该列,我们将得到一个org.neo4j.graphdb.NotFoundException.

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

如果我们要求分配错误的班级,我们将得到通常的java.lang.ClassCastException.

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

可以在此处找到完整的工作示例: https://github. com/akollegger/neo4j-trials/blob/master/src/test/java/org/akollegger/neo4j/trials/richardw/ExecutionResultIteratorTrial.java

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

希望有帮助.

干杯, 安德烈亚斯(Andreas)

Cheers, Andreas

这篇关于Neo4j Cypher:如何遍历ExecutionResult结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆