Question

I have the following java code snippet that demonstrates the problem. The error I receive is also included below. It correctly pulls the correct set, but I am having trouble printing. I'm using the org.neo4j.graphdb.Node node. Is this the wrong class? If not, how do I obtain the results movieid, avgrating and movie_title from the ExecutionEngine?

Java Code

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

Error

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)
Was it helpful?

Solution

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

...looks to be the culprit.

In your Cypher statement, you return m.title as movie_title, i.e. you're returning a node property (in this case, a string), and, in the offending line, you're trying to cast that string result as a Node.

If you want Cypher to return a series of nodes you can iterate through, try returning m (the whole node) instead of just individual properties and aggregates, e.g.

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

Etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top