1
votes

I want to traverse through Neo4j database nodes and relationships in java (not the embedded one. I want to use REST API). I have following method.

public void testTraverse(Node startNode) {
        for (Path position : Traversal.description()
                .depthFirst()
                .uniqueness(Uniqueness.NODE_GLOBAL)
                .evaluator(Evaluators.toDepth(10)).traverse(startNode)) 
        {
            System.out.println(position.lastRelationship().toString()+"\n--------------------------------\n");
        }
        for(Node node:Traversal.description().traverse(startNode).nodes()) {
            System.out.println(node.getProperty("name"));
        }
}

When calling this method, how I can create that startNode of type Node, which I want to pass as the parameter?

I am very new to Neo4j. Please help me. Alternative methods for Neo4j-Java-ReST is also warm welcomed...

1
Don't use traverse on the Node but create a RestTraversalDescription to describe your traversal and then pass in the nodes to traverse for it to be executed on the server. - Michael Hunger
Hi @Micheal Hunger, is this the way u mean, ..> new RestAPIFacade("localhost:7474/db/… .traverse(nodesList.toArray(new Node[0])) .. This returns a org.neo4j.graphdb.traversal.Traverser, is that right ? If not can you put the line for your above comment "create a RestTraversalDescription to describe your traversal and then pass in the nodes to traverse" . Thanks. - Ranjith

1 Answers

4
votes

Well, that starting node would have to be a node that you previously stored somehow.

Via the GraphDatabaseService instance, you could look up a node either by its ID:

Node yourNode = graphDatabaseService.getNodeById(0L);

...or through an index:

Node yourNode = graphDatabaseService.forNodes("anIndexName").get("id", 42).getSingle();