user5513268
user5513268

Reputation: 41

How do I use Gremlin/Tinkerpop with Java?

I'm working on a project which involves the use of knowledge representation in Java, and I got the impression that some kind of semantic network is the way to go about it.

Gremlin/Tinkerpop seems to have very nice syntax for graph generation and traversal, but I can only get it to work in an independent shell. It's a JVM language, so presumably it has some kind of Java API? I tried adding the source folder to an Eclipse project and it just comes up filled with errors and refuses to work.

Is there some better way of doing this? A compiled library perhaps, akin to the Stanford CoreNLP library that I'm using to process the user input?

Upvotes: 4

Views: 4145

Answers (2)

Joey Baruch
Joey Baruch

Reputation: 5229

I know this isn't directly answering your question, but I was where you are now, and this info might come in handy.

We're currently using tinkerpop 3.0.1-incubating with titan 1.0.0 for a project at work. I highly recommend going over the tinkerpop documentation extensively.

I'm currently trying out a design pattern where i have a basic graph utilities class in groovy (gremlin groovy is very similar to the independent shell that you mentioned). The point of the utils class is to wrap all the traversal, access, and retrieval functionalities (from node/s x, go through edges y and get prop z, and so on). This is important due to the fact that tinkerpop tends to have some changes from version to version, and this serves as a single point of change. Then i use regular java with this utils class to implement my own ORM kinda thing.

here's some groovy sources i found helpful (about 4 mins each video):

https://www.youtube.com/watch?v=1Trx7cKwMOQ

https://www.youtube.com/watch?v=u7NWMOL5aUo

PS: here's some useful groovy snippet that'll save u time down the line:

note the .fill(result) as a method of obtaining the info you'd get from the shell

def result = []
g.V().values().fill(result)
return result.first()

Upvotes: 0

rmuller
rmuller

Reputation: 12859

Tinkerpop 3 offers an API (like JDBC does for RDBMS) and (vendor specific) implementations. An in-memory reference implementation is also available. So first you need to decide upon the implementation you need. For learning purposes I recommend using the reference implementation (TinkerGraph) first.

Easiest way to start is to use maven. For this, add the following dependency:

<dependency>
    <groupId>org.apache.tinkerpop</groupId>
    <artifactId>tinkergraph-gremlin</artifactId>
    <version>${tinkergraph.version}</version>
</dependency>

If not using maven, you need to add the following jar-files to your class path (I am not aware of an uber-jar for TinkerGraph):

+- org.apache.tinkerpop:tinkergraph-gremlin:jar:3.0.1-incubating:compile
|  \- org.apache.tinkerpop:gremlin-core:jar:3.0.1-incubating:compile
|     +- org.apache.tinkerpop:gremlin-shaded:jar:3.0.1-incubating:compile
|     +- commons-configuration:commons-configuration:jar:1.10:compile
|     |  \- commons-lang:commons-lang:jar:2.6:compile
|     +- org.yaml:snakeyaml:jar:1.15:compile
|     +- org.javatuples:javatuples:jar:1.2:compile
|     +- com.carrotsearch:hppc:jar:0.7.1:compile
|     +- com.fasterxml.jackson.core:jackson-databind:jar:2.5.3:compile
|     |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.5.0:compile
|     |  \- com.fasterxml.jackson.core:jackson-core:jar:2.5.3:compile
|     +- com.jcabi:jcabi-manifests:jar:1.1:compile
|     |  \- com.jcabi:jcabi-log:jar:0.14:compile
|     +- org.slf4j:slf4j-log4j12:jar:1.7.12:compile
|     |  +- org.slf4j:slf4j-api:jar:1.7.12:compile
|     |  \- log4j:log4j:jar:1.2.17:compile
|     \- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile

Now you can use the API from within your Java (or other JVM base) language.

Graph g = TinkerGraph.open(); // open in-memory Graph

Note: Tinkerpop3 needs Java 8 (it offers a very nice API based on Java 8 streams and lambdas!).

Upvotes: 5

Related Questions