Skip to content

JVM Language Implementations

okram edited this page Nov 18, 2011 · 22 revisions

Gremlin is a style of graph traversing that can be hosted in any number of languages. The benefit of this is that users can make use of the programming language they are most comfortable with and still be able to evaluate Gremlin-style traversals. This model is different than, lets say, using SQL in Java where the query is evaluated by passing a string representation of the query to the SQL engine. On the contrary, with Gremlin native support for other JVM languages, there is no string passing, simply method execution in Gremlin’s fluent style. However, the drawback of this model is that for each JVM language, there are syntactic variations that must be accounted for.

The examples below demonstrate the same traversal in Groovy, Scala, and Java, respectively.

Iterate through all the vertices in the graph

g.V
g.V
new GremlinPipeline(g).V()

Iterate through all the vertices that vertex 1 knows

g.v(1).out('knows')
g.v(1).out("knows")
new GremlinPipeline(g.getVertex(1)).out("knows")

Iterate through the names of all the vertices that vertex 1 knows

g.v(1).out('knows').name
g.v(1).out("knows").property("name")
new GremlinPipeline(g.getVertex(1)).out("knows").property("name")

Iterate though all vertices that vertex 1 knows named josh

g.v(1).out.filter{it.name=='josh'}
g.v(1).out.filter{v:Vertex => v("name").equals("josh")}

new GremlinPipeline(g.getVertex(1)).out().filter(new PipeFunction<Vertex,Boolean>() { 
  public Boolean compute(Vertex v) {
    return v.getProperty("name").equals("josh");
  }
}
x = []; g.v(1).outE.aggregate(x){it.weight}
val x = List; g.v(1).outE.aggregate(x){Edge e => e("weight")}

List<Double> x = new ArrayList<Double>();
new GremlinPipeline(g.getVertex(1)).outE().aggregate(x, new PipeFunction<Edge,Double>() {
  public Double compute(Edge e) {
    return (Double) e.getProperty("weight");
  }
}