如何从ExecutionResult中单独提取结果?

时间:2014-05-07 01:18:32

标签: neo4j cypher

我有以下用于演示此问题的java代码段。我收到的错误也包含在下面。 它正确地拉出正确的设置,但我在打印时遇到问题。 我正在使用org.neo4j.graphdb.Node节点。这是错误的班级吗? 如果没有,我如何从ExecutionEngine获取结果movieid,avgrating和movie_title?

Java代码

GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
ExecutionEngine engine = new ExecutionEngine(db);

String cypherQuery =        "MATCH (n)-[r:RATES]->(m) \n"
                          + "RETURN m.movieid as movieid, avg(toFloat(r.rating)) as avgrating, m.title as movie_title \n"
                          + "ORDER BY avgrating DESC \n"
                          + "LIMIT 20;";

ExecutionResult result = engine.execute(cypher);

for (Map<String, Object> row : result) {
    Node x = (Node) row.get("movie_title");
        for (String prop : x.getPropertyKeys()) {
            System.out.println(prop + ": " + x.getProperty(prop));
        }
    }

错误

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to org.neo4j.graphdb.Node
    at beans.RecommendationBean.queryMoviesWithCypher(RecommendationBean.java:194)
    at beans.RecommendationBean.main(RecommendationBean.java:56)

1 个答案:

答案 0 :(得分:2)

Node x = (Node) row.get("movie_title");

......看起来是罪魁祸首。

在您的Cypher语句中,您将m.title作为movie_title返回,即您返回一个节点属性(在本例中为一个字符串),并且在违规行中,您正在尝试执行该字符串结果为节点。

如果您希望Cypher返回一系列节点,您可以迭代,尝试返回m(整个节点),而不仅仅是单个属性和聚合,例如

"...RETURN m AS movie;"
...
Node x = (Node) row.get("movie");