2014-05-07 44 views
0

我有以下java代碼片段,演示了這個問題。我收到的錯誤也包含在下面。 它正確地拉正確的設置,但我無法打印。 我正在使用org.neo4j.graphdb.Node節點。這是錯誤的課程嗎? 如果不是,我如何從ExecutionEngine獲取結果movieid,avgrating和movie_title?如何從ExecutionResult中分別提取結果?

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) 

回答

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"); 

等等