How to use Traversal Description


(Nadworny) #1

Hi guys,

I'm struggling to implement a basic java application that uses the Traversal framework (https://neo4j.com/docs/java-reference/current/tutorial-traversal/) but maybe I just misunderstood how it's meant to be used.

I'm creating an API that should talk to the existing Neo4j database and as the Traversal interface would help me a lot in navigating through the graph, I thought to use it instead of working with Cypher. But I can't seem to figure out how to actually get the Travesal interface to use the neo4j Driver. Normally I would do something like this:
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));

And create a transaction for each call to the database. But how can I use the driver with the Traversal?
Or can I use Traversal only when I'm using the embedded neo4j?

Thanks a lot!


(M. David Allen) #2

To use the traversal framework you must be "in the database". Yes, embedded mode would work, but you could also use the traversal framework within a stored procedure you were writing, if you wished -- so it's not just embedded. But all of the classes required for this only exist server side, so there's not a way to interact with this via a regular driver as far as I know.

I don't mean to try to convince you not to do it this way -- but most users find that cypher will be easier 99% of the time. There are special cases where the traversal framework will be better, but for most use cases it will be more code to write, and perhaps not even as performant. Cypher also has the advantage that it will work server side or with a driver.

So you have options -- one would be to use cypher instead of the traversal framework, which I'd recommend if your use could support it. The second option would be to write your code as a java plugin to the server. You could then for example expose your traversal code in cypher, by say making it a stored procedure, so that users from the outside could reuse or call this traversal just by doing something like CALL myCode.traversal()

Relevant documentation: https://neo4j.com/docs/java-reference/current/tutorial-traversal/


(Nadworny) #3

Thanks a lot for the answer, it makes everything clear now. I'll most probably still stick with the traversal as I've kind of hit a limit with the cypher for the requirement that I have at the moment.


(Andrew Bowman) #4

You may want to also check if the path expander procs in APOC Procedures will meet your needs, as those leverage the traversal framework:

https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_expand_paths

It may help to describe what you're attempting to accomplish.


(Jonatan Jaderberg) #5

Yes, it is an in-process/embedded API, not a remoting/driver API. The premise of the Java Developer Reference is coding directly against the database, either developing extensions (procedures, functions, HTTP endpoints / unmanaged extensions, security plugins) or embedding Neo4j with your application--with everything that entails. The traversal API predates Cypher. You can do some cool things, but it does require sticking it in an extension of some kind. Unless you are running Neo4j in-process, embedded with your application (which it sounds like you are not).