Issue of casting Node neo4j

2019-08-27 13:55发布

问题:

My code is below, As per the documentation it should have given me the node values but it is throwing me exception

Exception in thread "main" java.lang.ClassCastException: scala.collection.convert.Wrappers$SeqWrapper cannot be cast to org.neo4j.graphdb.Node
	at com.neo4j.performance.FetchData.main(FetchData.java:32)
I'm using Neo4j 2.2.2.

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import java.util.Iterator; 
import org.neo4j.helpers.collection.IteratorUtil;




import com.neo4j.enitites.Global;

public class FetchData {
     public static void main(String[] args) throws Exception  
        {

         String nodeResult = null;
         try ( Transaction ignored = Global.db.beginTx();
                  Result result = Global.db.execute( "MATCH path=()-[:route_1*]-() RETURN nodes(path) AS result" ) )
            {
                // START SNIPPET: items
                Iterator<Node> n_column = result.columnAs( "result" );
                for ( Node node : IteratorUtil.asIterable( n_column ) )
                {
                    nodeResult = node + ": " + node.getProperty( "StationCode" );
                }
                // END SNIPPET: items


            }

         System.out.println(nodeResult);
        }
}

I've searched for the error but only 2 or 3 posts are there those also not working. Has anyone came across with this issue or there is something wrong in this code. Thanks

回答1:

Use this snippet instead inside the transaction try-with-resources block:

        for (Object cell : IteratorUtil.asIterable(result.columnAs("result"))) {
            Iterable<Node> nodes = (Iterable<Node>) cell;
            for (Node n : nodes) {
                System.out.println(n);
            }
        }