SlideShare a Scribd company logo
The Gremlin Graph Traversal Language
Marko A. Rodriguez and Daniel Kuppitz
http://tinkerpop.incubator.apache.org
user movie categoryoccupation
occupation rated category
name:String
gender:[M,F]
age:integer
name:String
year:integer
name:String
stars:[1,2,3,4,5]
http://grouplens.org/datasets/movielens/
MovieLens Dataset
user
|Vuser| = 6040
|Vmovie| = 3883
movie
|Vcategory| = 18
category
|Voccupation| = 21
occupation
occupation
|Eoccupation| = 6040
rated
|Erated| = 1000209
category
|Ecategory| = 6408
name:String
gender:[M,F]
age:integer
name:String
year:integer
name:String
stars:[1,2,3,4,5]
http://grouplens.org/datasets/movielens/
G = (V, E)
MovieLens Dataset
~/tinkerpop3$ bin/gremlin.sh
gremlin>
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin>
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin>
Gremlin-Java8
Gremlin-Groovy*
Gremlin-Scala
Gremlin-Clojure
Gremlin-JavaScript
Gremlin-Python
Gremlin-PHP
...
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin>
"Create a new TinkerGraph."
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin>
"Create a new TinkerGraph."
G = (V = ∅, E = ∅)
G The graph is a set of vertices and edges
V The set of vertices in the graph
E The set of edges in the graph
∅ The empty set -- no elements
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin>
"Create a new TinkerGraph."
G = (V = ∅, E = ∅)
TitanGraph.open(…)
Neo4jGraph.open(…)
OrientGraph.open(…)
SqlgGraph.open(…)
HadoopGraph.open(…)
GiraphGraphComputer
SparkGraphComputer
ElasticGraph.open(…)
...
G = (V = ∅, E ⊆ (V × V ))
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin>
"Load the MovieLens dataset into the newly created TinkerGraph."
Set A is a subset of (or equal to) set B
The set of all ordered pairs of vertices (directed binary edges)
A ⊆ B
(V × V )
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin>
"Create a graph traversal source for spawning graph traversals over the MovieLens graph."
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> g.V().count()
==>9962
gremlin>
"Count the number of vertices in the graph."
|V | = 9962
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> g.V().count()
==>9962
gremlin>
"Count the number of vertices in the graph."
|V | = 9962
Vertex
map 9962
reducing barrier
Long
seed=0
value=seed
binary operator: value -> value+1
count()
{ "m
any-to-one"
Edge
map 1012657
reducing barrier
Long
|E| = 1012657
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> g.V().count()
==>9962
gremlin> g.E().count()
==>1012657
gremlin>
"Count the number of edges in the graph."
count()
{ "m
any-to-one"
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> g.V().count()
==>9962
gremlin> g.E().count()
==>1012657
gremlin> g.V().label().groupCount()
==>[occupation:21, movie:3883, category:18, user:6040]
gremlin>
"For each vertex in the graph, emit its label, then group and count each distinct label."
user
user
movie
category
...
Vertex String Map<String,Long>
user
user
movie
category
map map
reducing
barrier
[
occupation=21,
movie=3883,
category=18,
user=6040
]
label() groupCount(){"one-to-one" "many-to-one"
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> g.V().count()
==>9962
gremlin> g.E().count()
==>1012657
gremlin> g.V().label().groupCount()
==>[occupation:21, movie:3883, category:18, user:6040]
gremlin> g.E().hasLabel('rated').values('stars').mean()
==>3.581564453029317
gremlin>
"For each rated-edge in the graph, emit its stars property value and compute the average value."
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> g.V().count()
==>9962
gremlin> g.E().count()
==>1012657
gremlin> g.V().label().groupCount()
==>[occupation:21, movie:3883, category:18, user:6040]
gremlin> g.E().hasLabel('rated').values('stars').mean()
==>3.581564453029317
gremlin> g.V().hasLabel('user').map(outE('rated').count()).max()
==>2314
gremlin>
"What is the maximum number of movies a single user rated?"
~/tinkerpop3$ bin/gremlin.sh
,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo')
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> g.V().count()
==>9962
gremlin> g.E().count()
==>1012657
gremlin> g.V().label().groupCount()
==>[occupation:21, movie:3883, category:18, user:6040]
gremlin> g.E().hasLabel('rated').values('stars').mean()
==>3.581564453029317
gremlin> g.V().hasLabel('user').map(outE('rated').count()).max()
==>2314
gremlin> g.V().hasLabel('movie').values('year').min()
==>1919
gremlin>
"What year was the oldest movie made?"
gremlin> g.V().hasLabel('category').values('name')
==>Animation
==>Children's
==>Comedy
==>Adventure
==>Fantasy
==>Romance
==>Drama
==>Action
==>Crime
==>Thriller
==>Horror
==>Sci-Fi
==>Documentary
==>War
==>Musical
==>Mystery
==>Film-Noir
==>Western
"For each vertex that is labeled 'category,' emit the name property value of that vertex."
category
user
user
movie
category
Vertex
category
category
category
category
category
categorycategory
category
Vertex
Animation
Children's
Comedy
Adventure
Western
...
String
filter map
hasLabel('category') values('name')
"one-to-[one-or-none]" "one-to-one"
gremlin> g.V().hasLabel('category').as('a','b').
select('a','b').
by('name').
by(inE('category').count())
"For each category vertex, emit a map of its name and the number of movies it represents."
hasLabelcategory : V ∗
→ V ∗
gremlin> g.V().hasLabel('category').as('a','b').
select('a','b').
by('name').
by(inE('category').count())
"For each category vertex, emit a map of its name and the number of movies it represents."
V : G → V ∗
asa,b : V ∗
→ (V × V )∗
G The set of all graphs
f : A → B The function f maps values of type A to values of type B
A∗ A stream of values of type A
(A × B) The set of all pairs of values from A and B (cross product)
N The set of all natural numbers (1, 2, 3, 4, …)
The set of all strings (a, b, aa, ab, bb, …)
selecta,b : (V × V )∗
→
a valuesname : V ∗
→ S
b (inEcategory : V ∗
→ E∗
) ◦ (count : E∗
→ N)
→ (S × N)∗
S Σ∗
typically denoted
gremlin> g.V().hasLabel('category').as('a','b').
select('a','b').
by('name').
by(inE('category').count())
==>[a:Animation, b:105]
==>[a:Children's, b:251]
==>[a:Comedy, b:1200]
==>[a:Adventure, b:283]
==>[a:Fantasy, b:68]
==>[a:Romance, b:471]
==>[a:Drama, b:1603]
==>[a:Action, b:503]
==>[a:Crime, b:211]
==>[a:Thriller, b:492]
==>[a:Horror, b:343]
==>[a:Sci-Fi, b:276]
==>[a:Documentary, b:127]
==>[a:War, b:143]
==>[a:Musical, b:114]
==>[a:Mystery, b:106]
==>[a:Film-Noir, b:44]
==>[a:Western, b:68]
"For each category vertex, emit a map of its name and the number of movies it represents."
category
user
user
movie
category
Vertex
category
category
category
category
category
categorycategory
category
Vertex
[a:Animation, b:105]
[a:Children's, b:251]
[a:Comedy, b:1200]
[a:Adventure, b:283]
…
[a:Western, b:68]
Map<String,Long>
filter map
category
name:Animation
category
category
category
...
category
map map
reducing
barrier
105
Vertex Edge Long
category
name:Animation
Vertex String
map Animationa
b
map
flatMap
{"one-to-m
any"
"one-to-one"
gremlin> g.V().hasLabel('movie').as('a','b').
select('a','b').
by('name').
by(inE('rated').values('stars').mean()).
order().by(select('b'),decr).
limit(10)
"For each movie, emit a map of its name and average rating.
Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)."
gremlin> g.V().hasLabel('movie').as('a','b').
select('a','b').
by('name').
by(inE('rated').values('stars').mean()).
order().by(select('b'),decr).
limit(10)
==>[a:Charm's Incidents, b:NaN]
==>[a:Prerokbe Ognja, b:NaN]
==>[a:Leopard Son, The, b:NaN]
==>[a:Bird of Prey, b:NaN]
==>[a:Plutonium Circus, b:NaN]
==>[a:Hustler White, b:NaN]
==>[a:Curtis's Charm, b:NaN]
==>[a:Three Lives and Only One Death, b:NaN]
==>[a:Hoogste tijd, b:NaN]
==>[a:Entertaining Angels: The Dorothy Day Story, b:NaN]
category
user
user
movie
category
Vertex
movie
movie
movie
movie
movie
moviemovie
movie
Vertex
[a:Charm's Incidents, b:NaN]
[a:Prerokbe Ognja, b:NaN]
[a:Leopard Son, The, b:NaN]
[a:Bird of Prey, b:NaN]
...
[a:Entertaining Angels, b:NaN]
Map<String,Double>
filter map
movie
name:Charm's Incidents
map
reducing
barrier
NaN
Vertex Edge Double
movie
name:Charm's Incidents
Vertex String
map Charm's Incidentsa
b
map
map
Integer
...
"For each movie, emit a map of its name and average rating.
Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)."
flatMap
gremlin> g.V().hasLabel('movie').as('a','b').
select('a','b').
by('name').
by(coalesce(
inE('rated').values('stars'),
constant(0)).mean()).
order().by(select('b'),decr).
limit(10)
==>[a:Lured, b:5.0]
==>[a:One Little Indian, b:5.0]
==>[a:Bittersweet Motel, b:5.0]
==>[a:Gate of Heavenly Peace, The, b:5.0]
==>[a:Follow the Bitch, b:5.0]
==>[a:Schlafes Bruder (Brother of Sleep), b:5.0]
==>[a:Ulysses (Ulisse), b:5.0]
==>[a:Song of Freedom, b:5.0]
==>[a:Smashing Time, b:5.0]
==>[a:Baby, The, b:5.0]
"For each movie, get its name and mean rating (or 0 if no ratings). Order by average rating and emit top 10."
user
user
movie
category
Vertex
movie
movie
movie
movie
moviemovie
movie
Vertex Map<String,Double>
filter map
movie
name:Charm's Incidents
map map
reducing
barrier
0.0
Vertex Integer Double
movie
name:Charm's Incidents
Vertex String
map Charm's Incidentsa
b
map
0
[a:Lured, b:5.0]
[a:One Little Indian, b:5.0]
[a:Bittersweet Motel, b:5.0]
[a:Gate of Heavenly Peace, b:5.0]
...
[a:Baby, The, b:5.0]
Map<String,Double>
map
[a:Charm's Incidents, b:NaN]
[a:Prerokbe Ognja, b:NaN]
[a:Leopard Son, The, b:NaN]
[a:Bird of Prey, b:NaN]
...
[a:Entertaining Angels, b:NaN]
collecting
barrier
gremlin> g.V().hasLabel('movie').as('a','b').
where(inE('rated').count().is(gt(10))).
select('a','b').
by('name').
by(inE('rated').values('stars').mean()).
order().by(select('b'),decr).
limit(10)
"For each movie with at least 11 ratings, emit a map of its name and average rating.
Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)."
gremlin> g.V().hasLabel('movie').as('a','b').
where(inE('rated').count().is(gt(10))).
select('a','b').
by('name').
by(inE('rated').values('stars').mean()).
order().by(select('b'),decr).
limit(10)
==>[a:Sanjuro, b:4.608695652173913]
==>[a:Seven Samurai (The Magnificent Seven), b:4.560509554140127]
==>[a:Shawshank Redemption, The, b:4.554557700942973]
==>[a:Godfather, The, b:4.524966261808367]
==>[a:Close Shave, A, b:4.52054794520548]
==>[a:Usual Suspects, The, b:4.517106001121705]
==>[a:Schindler's List, b:4.510416666666667]
==>[a:Wrong Trousers, The, b:4.507936507936508]
==>[a:Sunset Blvd. (a.k.a. Sunset Boulevard), b:4.491489361702127]
==>[a:Raiders of the Lost Ark, b:4.47772]
"For each movie with at least 11 ratings, emit a map of its name and average rating.
Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)."
map
movie
name:Sanjuro
rated
rated
rated
...
rated
map
reducing
barrier
4.60
Vertex Edge Double
movie
name:Sanjuro
Vertex String
map Sanjuroa
b
map
map
Integer
user
user
movie
category
Vertex
movie
movie
movie
movie
moviemovie
movie
Vertex
filter
movie
rated
rated
rated
...
rated
reducing
barrier
Vertex Edge
map
Long
69
name:Sanjuro
filter
movie
movie
movie
movie
movie
Vertex
5
4
…
5
[[a:Sanjuro, b:4.60]
[a:Seven Samurai, b:4.56]
[a:Shawshank Redemption, b:4.55]
[a:Godfather, The, b:4.52]
...
[a:Raiders of the Lost Ark, b:4.47]
[…]
[…]
[…]
[…]
…
[…]
Map<String,Double> Map<String,Double>
map
collecting
barrier
flatMap
flatMap
{
{
gremlin> g.V().hasLabel('movie').
where(inE('rated').count().is(gt(10))).
toString()
==>[GraphStep([],vertex), HasStep([~label.eq(movie)]),
TraversalFilterStep([
VertexStep(IN,[rated],edge),
CountGlobalStep,
IsStep(gt(10))])]
"What is the execution plan for the traversal prior to compiler optimizations being applied?"
V : G → V ∗
hasLabelmovie : V ∗
→ V ∗
where : V ∗
→
inErated : V ∗
→ E∗
count : E∗
→ N
isgt(10) : N → (N ∪ ∅)
→ V ∗
"true
orfalse"
gremlin> g.V().hasLabel('movie').
where(inE('rated').count().is(gt(10))).
iterate().toString()
==>[TinkerGraphStep(vertex,[~label.eq(movie)]),
TraversalFilterStep([
VertexStep(IN,[rated],edge),
RangeGlobalStep(0,11),
CountGlobalStep,
IsStep(gt(10))])]
"What is the execution plan for the traversal after compiler optimizations have been applied?"
* TinkerGraphStragegy: Access vendor-specific vertex partition by label.
* RangeByIsCountStrategy: Only iterate 1 more than required count.
where : V ∗
→
inErated : V ∗
→ E∗
count : E∗
→ N
isgt(10) : N → (N ∪ ∅)
limit11 : E∗
→ E∗
Vlabel=movie : G → V ∗
→ V ∗
"true
orfalse"
gremlin> g.getStrategies()
==>ConjunctionStrategy
a.and().b => and(a,b)
a.or().b => or(a,b)
a.or().b.and().c => or(a,and(b,c))
a.and().b.or().c => or(and(a,b),c)
==>IncidentToAdjacentStrategy
a.outE().inV().b => a.out().b
==>AdjacentToIncidentStrategy
a.in().count().b => a.inE().count().b
a.where(out()).b => a.where(outE()).b
a.and(in(),out()).b => a.and(inE(),outE()).b
==>IdentityRemovalStrategy
a.identity().b => a.b
==>FilterRankingStrategy
a.order().dedup().b => a.dedup().order().b
a.and(c,d).has().b => a.has().and(c,d).b
a.simplePath().where().b => b.where().simplePath().a
==>MatchPredicateStrategy
a.match(c,d).where(e).b => a.match(c,d,e)
a.match(has(),c,d).b => a.has().match(c,d).b
==>RangeByIsCountStrategy
a.count().is(0) => a.limit(1).count().is(0)
==>TinkerGraphStepStrategy
V.has().has().b => V[has,has].b
==>ProfileStrategy
a.b.c.profile() => a.profile().b.profile().c.profile()
==>ComputerVerificationStrategy
a.order.b => IllegalStateException
a.local(out().out()).b => IllegalStateException
"What compilation strategies are associated with the graph traversal source?"
gremlin> g.V().has('movie','name','Die Hard').
inE('rated').values('stars').mean()
==>4.121848739495798
"What is Die Hard's average rating?"
gremlin> g.V().has('movie','name','Die Hard').
inE('rated').values('stars').mean()
==>4.121848739495798
"What is Die Hard's average rating?"
movie
movie
movie
Vertex
movie
Vertex
filter
name:Die Hard
flatMap
rated
rated
rated
...
rated
Edge
map
3
5
5
…
4
Integer
map
Double
4.1218
reducing
barrier
V : G → V ∗
hasLabelmovie : V ∗
→ V ∗
hasname=Die Hard : V ∗
→ V ∗
inErated : V ∗
→ E∗ mean : N∗
→ R
valuesstars : E∗
→ N∗
user
user
movie
user
Vertex
filter
{
"one-to-[one-or-none]"
"one-to-[one-or-none]"
"one-to-many"
"one-to-one"
"many-to-one"
gremlin> g.V().has('movie','name','Die Hard').as('a').
inE('rated').has('stars',5).outV().
where(out('occupation').has('name','programmer')).
outE('rated').has('stars',5).inV().
where(neq('a')).
groupCount().by('name').
order(local).by(valueDecr).
limit(local,10).
unfold() // so its not printed on a single line
"Which programmers like Die Hard and what other movies do they like?
Group and count the movies by their name. Sort the group count map in decreasing order by the count.
Clip the map to the top 10 entries and stream out the map's entries (for display purposes)."
gremlin> g.V().has('movie','name','Die Hard').as('a').
inE('rated').has('stars',5).outV().
where(out('occupation').has('name','programmer')).
outE('rated').has('stars',5).inV().
where(neq('a')).
groupCount().by('name').
order(local).by(valueDecr).
limit(local,10).
unfold() // so its not printed on a single line
"Which programmers like Die Hard and what other movies do they like?
Group and count the movies by their name. Sort the group count map in decreasing order by the count.
Clip the map to the top 10 entries and stream out the map's entries (for display purposes)."
user
user
movie
user
Vertex
movie
Vertex
filter filter
name:Die Hard
flatMap
rated
rated
rated
...
rated
Edge
filter
rated
...
rated
Edge
map
Vertex
user
user
user
useruser
user
Vertex Vertex
occupationflatMap
Vertex
filter
rated
rated
rated
...
rated
Edge
filter
rated
...
rated
Edge
map
Vertex
movie
movie
moviemovie
movie
Vertex
Vertex
user
user
user
flatMap filter movie
Vertex
filter
Vertex
movie
movie
movie
map
reducing
barrier
[
Aliens=105,
Braveheart=24,
…
Pulp Fiction=19
]
Map<String,Long>
map
collecting
barrier
[
Raider of the Lost Ark=36,
Star Wars: Episode V=24,
Star Wars: Episode IV=34
…
Airplane II: The Sequel=1
]
Map<String,Long>
[
Raider of the Lost Ark=36,
Star Wars: Episode V=24,
Star Wars: Episode IV=34
…
Alien=22
]
Map<String,Long>
map
occupation
programmer
not Die Hard
gremlin> g.V().has('movie','name','Die Hard').as('a').
inE('rated').has('stars',5).outV().
where(out('occupation').has('name','programmer')).
outE('rated').has('stars',5).inV().
where(neq('a')).
groupCount().by('name').
order(local).by(valueDecr).
limit(local,10).
unfold() // so its not printed on a single line
==>Raiders of the Lost Ark=36
==>Star Wars: Episode V - The Empire Strikes Back=36
==>Star Wars: Episode IV - A New Hope=34
==>Matrix, The=32
==>Terminator, The=29
==>Star Wars: Episode VI - Return of the Jedi=26
==>Sixth Sense, The=26
==>Braveheart=24
==>Aliens=23
==>Alien=22
gremlin>
"Which programmers like Die Hard and what other movies do they like?
Group and count the movies by their name. Sort the group count map in decreasing order by the count.
Clip the map to the top 10 entries and stream out the map's entries (for display purposes)."
gremlin> g.V().
match(
__.as('a').hasLabel('movie'),
__.as('a').out('category').has('name','Action'),
__.as('a').has('year',between(1980,1990)),
__.as('a').inE('rated').as('b'),
__.as('b').has('stars',5),
__.as('b').outV().as('c'),
__.as('c').out('occupation').has('name','programmer'),
__.as('c').has('age',between(30,40))).
select('a').groupCount().by('name').
order(local).by(valueDecr).
limit(local,10).
unfold() // so its not printed on a single line
"What 80's action movies do 30-something programmers like?
Group count the movies by their name and sort the group count map in decreasing order by value.
Clip the map to the top 10 and emit the map entries."
"What 80's action movies do 30-something programmers like?
Group count the movies by their name and sort the group count map in decreasing order by value.
Clip the map to the top 10 and emit the map entries."
gremlin> g.V().
match(
__.as('a').hasLabel('movie'),
__.as('a').out('category').has('name','Action'),
__.as('a').has('year',between(1980,1990)),
__.as('a').inE('rated').as('b'),
__.as('b').has('stars',5),
__.as('b').outV().as('c'),
__.as('c').out('occupation').has('name','programmer'),
__.as('c').has('age',between(30,40))).
select('a').groupCount().by('name').
order(local).by(valueDecr).
limit(local,10).
unfold() // so its not printed on a single line
==>Raiders of the Lost Ark=26
==>Star Wars: Episode V - The Empire Strikes Back=26
==>Terminator, The=23
==>Star Wars: Episode VI - Return of the Jedi=22
==>Princess Bride, The=19
==>Aliens=18
==>Boat, The (Das Boot)=11
==>Indiana Jones and the Last Crusade=11
==>Star Trek: The Wrath of Khan=10
==>Abyss, The=9
gremlin>
MatchStep
GraphTraversal.match(Traversal... traversalPatterns)
x.match(
a...b
a...c
c...
or(
a...c
a...b
)
c.repeat(...).b
not(c...a)
b...count().e
c...count().e
).dedup(a,b).y
a,b,c,e : once a variable is set, it must hold equal for all patterns
c... : "predicate patterns" simply check for the existence of a result
or()/and() : nested conjunctive patterns supported
repeat(...) : recursive patterns supported
not(...) : not'ing of patterns supported
count() : barrier patterns supported
dedup(a,b) : internal de-duplication of variable values supported
x.match().y : possible to go from imperative to declarative, etc.
Plug and Play MatchAlgorithms
GreedyMatchAlgorithm :
try each pattern in the order provided by the user
CountMatchAlgorithm :
continually re-sort patterns by the cardinality of their set reductions
// CountMatchAlgorithm (default)
gremlin> clockWithResult(50){
g.V().match(
__.as('a').out('rated').as('b'),
__.as('a').out('occupation').has('name','farmer')).
select('a','b').count().next()}
==>66.31955294 // time in milliseconds
==>2706 // number of results
// GreedyMatchAlgorithm
gremlin> g = graph.traversal(GraphTraversalSource.build().
with(MatchAlgorithmStrategy.build().
algorithm(MatchStep.GreedyMatchAlgorithm).create()))
==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard]
gremlin> clockWithResult(50){
g.V().match(
__.as('a').out('rated').as('b'),
__.as('a').out('occupation').has('name','farmer')).
select('a','b').count().next()}
==>1902.6290871599997 // time in milliseconds
==>2706 // number of results
"Which movies did each farmer rate? -- benchmark CountMatchAlgorithm vs. GreedyMatchAlgorithm."
farmermoviesusers
1000209 2706
farmer moviesusers
17 2706
gremlin> g.V().hasLabel('movie').
where(inE('rated').count().is(gt(10))).
group().
by{((int)(it.value('year') / 10)) * 10}.
by().
by(unfold().order().
by(inE('rated').values('stars').mean(),decr).
values('name').
limit(1)).
order(local).by(keyIncr).
unfold() // so its not printed on a single line
"What is the most liked movie in each decade?"
gremlin> g.V().hasLabel('movie').
where(inE('rated').count().is(gt(10))).
group().
by{((int)(it.value('year') / 10)) * 10}.
by().
by(unfold().order().
by(inE('rated').values('stars').mean(),decr).
values('name').
limit(1)).
order(local).by(keyIncr).
unfold() // so its not printed on a single line
"What is the most liked movie in each decade?"
λ
λ
Nearly every step that takes a traversal argument can also take a lambda.
It is recommended that users do not use lambdas as they are not subject to traversal strategy (i.e. compiler) optimization.
However, they are useful when no provided step yields the desired computation.
gremlin> g.V().hasLabel('movie').
where(inE('rated').count().is(gt(10))).
group().
by{((int)(it.value('year') / 10)) * 10}.
by().
by(unfold().order().
by(inE('rated').values('stars').mean(),decr).
values('name').
limit(1)).
order(local).by(keyIncr).
unfold() // so its not printed on a single line
==>1910=Daddy Long Legs
==>1920=General, The
==>1930=City Lights
==>1940=Third Man, The
==>1950=Seven Samurai (The Magnificent Seven)
==>1960=Sanjuro
==>1970=Godfather, The
==>1980=Raiders of the Lost Ark
==>1990=Shawshank Redemption, The
==>2000=Almost Famous
gremlin>
"What is the most liked movie in each decade?"
gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties')
==>hadoopgraph[gryoinputformat->gryooutputformat]
gremlin> g = graph.traversal(computer(SparkGraphComputer))
==>graphtraversalsource
[hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer]
gremlin>
"Which movies are most central in the implicit 5-stars graph?"
gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties')
==>hadoopgraph[gryoinputformat->gryooutputformat]
gremlin> g = graph.traversal(computer(SparkGraphComputer))
==>graphtraversalsource
[hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer]
gremlin> g.V().repeat(outE('rated').has('stars', 5).inV().
groupCount('m').by('name').
inE('rated').has('stars', 5).outV()).
times(4).cap('m')
"Which movies are most central in the implicit 5-stars graph?"
user movie user
ratedrated
m
4x
cap('m')
repeat(…).times(4)
g.V()
stars=5 stars=5
gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties')
==>hadoopgraph[gryoinputformat->gryooutputformat]
gremlin> g = graph.traversal(computer(SparkGraphComputer))
==>graphtraversalsource
[hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer]
gremlin> g.V().repeat(outE('rated').has('stars', 5).inV().
groupCount('m').by('name').
inE('rated').has('stars', 5).outV()).
times(4).cap('m')
==>Fantasia 2000=2676505178171564
==>Pale Rider=1369969000295362
==>Crucible, The=401712993698149
==>About Adam=37981148456999
==>Akira=3659939409345918
...
gremlin>
"Which movies are most central in the implicit 5-stars graph?"
gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties')
==>hadoopgraph[gryoinputformat->gryooutputformat]
gremlin> g = graph.traversal(computer(SparkGraphComputer))
==>graphtraversalsource
[hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer]
gremlin> g.V().repeat(outE('rated').has('stars', 5).inV().
groupCount('m').by('name').
inE('rated').has('stars', 5).outV()).
times(4).cap('m')
==>Fantasia 2000=2676505178171564
==>Pale Rider=1369969000295362
==>Crucible, The=401712993698149
==>About Adam=37981148456999
==>Akira=3659939409345918
...
gremlin> hdfs.ls('output/m')
==>rw-r--r-- daniel supergroup 0 _SUCCESS
==>rw-r--r-- daniel supergroup 245314 part-r-00000
gremlin> hdfs.head('output/m', ObjectWritable).sort {-it.value}.take(10)
==>Star Wars: Episode IV - A New Hope 35405394353105332
==>American Beauty 31943228282020585
==>Raiders of the Lost Ark 31224779793238499
==>Star Wars: Episode V - The Empire Strikes Back 30434677119726223
==>Godfather, The 30258518523013057
==>Shawshank Redemption, The 28297717387901031
==>Schindler's List 27539336654199309
==>Silence of the Lambs, The 26736276376806173
==>Fargo 26531050311325270
==>Matrix, The 26395118239203191
"Which movies are most central in the implicit 5-stars graph?"
gremlin> :plugin use tinkerpop.gephi
==>tinkerpop.gephi activated
gremlin> :remote connect tinkerpop.gephi
==>Connection to Gephi - http://localhost:8080/workspace0 with stepDelay:1000,
startRGBColor:[0.0, 1.0, 0.5], colorToFade:g, colorFadeRate:0.7, startSize:
20.0,sizeDecrementRate:0.33
gremlin>
gremlin> :plugin use tinkerpop.gephi
==>tinkerpop.gephi activated
gremlin> :remote connect tinkerpop.gephi
==>Connection to Gephi - http://localhost:8080/workspace0 with stepDelay:1000,
startRGBColor:[0.0, 1.0, 0.5], colorToFade:g, colorFadeRate:0.7, startSize:
20.0,sizeDecrementRate:0.33
gremlin> :> g.V().hasLabel('user').
order().
by(outE('rated').count(), decr).limit(10).as('a').
local(outE('rated').order().
by('stars', decr). // first by stars
by(inV().inE('rated').count(), decr). // then by ratings
limit(10)).
subgraph('sg').inV().outE('category').
subgraph('sg').select('a').outE('occupation').
subgraph('sg').cap('sg').next()
==>tinkergraph[vertices:82 edges:233]
gremlin>
"Which users rated the most movies?
For each user, display their 10 favorite movies, the categories of those movies, and their occupation.
"moviebuffs"
The Gremlin Graph Traversal Language
Thanks for listening…

More Related Content

What's hot (20)

KEY
Graphdatabases
Henning Rauch
 
PDF
The Apache Spark File Format Ecosystem
Databricks
 
PPTX
Programming in Spark using PySpark
Mostafa
 
PDF
Sqoop
Prashant Gupta
 
PDF
Neo4j 4.1 overview
Neo4j
 
PPTX
Introduction to Neo4j and .Net
Neo4j
 
PDF
Spark Summit EU talk by Ted Malaska
Spark Summit
 
PPTX
SPARQL Cheat Sheet
LeeFeigenbaum
 
PDF
How to Use JSON in MySQL Wrong
Karwin Software Solutions LLC
 
PPTX
Understanding my database through SQL*Plus using the free tool eDB360
Carlos Sierra
 
PDF
Storing time series data with Apache Cassandra
Patrick McFadin
 
PDF
Spark + Parquet In Depth: Spark Summit East Talk by Emily Curtin and Robbie S...
Spark Summit
 
KEY
Intro to Neo4j presentation
jexp
 
PDF
Introduction to Graph Database
Eric Lee
 
PPTX
Introducing MongoDB Atlas
MongoDB
 
PPTX
SHACL by example
Jose Emilio Labra Gayo
 
PDF
RDF, SPARQL and Semantic Repositories
Marin Dimitrov
 
PPTX
File Format Benchmark - Avro, JSON, ORC & Parquet
DataWorks Summit/Hadoop Summit
 
PDF
The Graph Database Universe: Neo4j Overview
Neo4j
 
PPTX
A simple introduction to redis
Zhichao Liang
 
Graphdatabases
Henning Rauch
 
The Apache Spark File Format Ecosystem
Databricks
 
Programming in Spark using PySpark
Mostafa
 
Neo4j 4.1 overview
Neo4j
 
Introduction to Neo4j and .Net
Neo4j
 
Spark Summit EU talk by Ted Malaska
Spark Summit
 
SPARQL Cheat Sheet
LeeFeigenbaum
 
How to Use JSON in MySQL Wrong
Karwin Software Solutions LLC
 
Understanding my database through SQL*Plus using the free tool eDB360
Carlos Sierra
 
Storing time series data with Apache Cassandra
Patrick McFadin
 
Spark + Parquet In Depth: Spark Summit East Talk by Emily Curtin and Robbie S...
Spark Summit
 
Intro to Neo4j presentation
jexp
 
Introduction to Graph Database
Eric Lee
 
Introducing MongoDB Atlas
MongoDB
 
SHACL by example
Jose Emilio Labra Gayo
 
RDF, SPARQL and Semantic Repositories
Marin Dimitrov
 
File Format Benchmark - Avro, JSON, ORC & Parquet
DataWorks Summit/Hadoop Summit
 
The Graph Database Universe: Neo4j Overview
Neo4j
 
A simple introduction to redis
Zhichao Liang
 

Viewers also liked (20)

PDF
ACM DBPL Keynote: The Graph Traversal Machine and Language
Marko Rodriguez
 
PDF
Quantum Processes in Graph Computing
Marko Rodriguez
 
PDF
Gremlin: A Graph-Based Programming Language
Marko Rodriguez
 
PPTX
Cassandra Summit - What's New In Apache TinkerPop?
Stephen Mallette
 
PDF
Traversing Graph Databases with Gremlin
Marko Rodriguez
 
PDF
Intro to Graph Databases Using Tinkerpop, TitanDB, and Gremlin
Caleb Jones
 
PDF
The Path Forward
Marko Rodriguez
 
PDF
The Gremlin in the Graph
Marko Rodriguez
 
PDF
Titan: Big Graph Data with Cassandra
Matthias Broecheler
 
PDF
Solving Problems with Graphs
Marko Rodriguez
 
PDF
Cassandra Virtual Node talk
Patrick McFadin
 
PDF
Temporal dynamics of human behavior in social networks (i)
Esteban Moro
 
PDF
Putting the Dance Theatre of Harlem Archives Back Together
danceheritage
 
PDF
European Government Bond Correlation Dynamics: Taming Contagion Risks
Peter Schwendner
 
PDF
Titan: The Rise of Big Graph Data
Marko Rodriguez
 
PDF
Network Approaches for Interbank Markets
Kimmo Soramaki
 
PDF
DataStax: What's New in Apache TinkerPop - the Graph Computing Framework
DataStax Academy
 
PDF
F8 tech talk_pinterest_v4
malorie_pinterest
 
PPT
Seda an architecture for well-conditioned scalable internet services
bdemchak
 
PPTX
Facebook's TAO & Unicorn data storage and search platforms
Nitish Upreti
 
ACM DBPL Keynote: The Graph Traversal Machine and Language
Marko Rodriguez
 
Quantum Processes in Graph Computing
Marko Rodriguez
 
Gremlin: A Graph-Based Programming Language
Marko Rodriguez
 
Cassandra Summit - What's New In Apache TinkerPop?
Stephen Mallette
 
Traversing Graph Databases with Gremlin
Marko Rodriguez
 
Intro to Graph Databases Using Tinkerpop, TitanDB, and Gremlin
Caleb Jones
 
The Path Forward
Marko Rodriguez
 
The Gremlin in the Graph
Marko Rodriguez
 
Titan: Big Graph Data with Cassandra
Matthias Broecheler
 
Solving Problems with Graphs
Marko Rodriguez
 
Cassandra Virtual Node talk
Patrick McFadin
 
Temporal dynamics of human behavior in social networks (i)
Esteban Moro
 
Putting the Dance Theatre of Harlem Archives Back Together
danceheritage
 
European Government Bond Correlation Dynamics: Taming Contagion Risks
Peter Schwendner
 
Titan: The Rise of Big Graph Data
Marko Rodriguez
 
Network Approaches for Interbank Markets
Kimmo Soramaki
 
DataStax: What's New in Apache TinkerPop - the Graph Computing Framework
DataStax Academy
 
F8 tech talk_pinterest_v4
malorie_pinterest
 
Seda an architecture for well-conditioned scalable internet services
bdemchak
 
Facebook's TAO & Unicorn data storage and search platforms
Nitish Upreti
 
Ad

Similar to The Gremlin Graph Traversal Language (16)

PDF
What's New in Apache TinkerPop - the Graph Computing Framework
DataStax Academy
 
PDF
TinkerPop 2020
Joshua Shinavier
 
PDF
A walk in graph databases v1.0
Pierre De Wilde
 
PPTX
Introduction to Gremlin
Max De Marzi
 
PDF
From zero to gremlin hero - Part I
GraphRM
 
PDF
Traversing Graphs with Gremlin
Artem Chebotko
 
PPTX
Data Con LA 2018 - Graph Computing: How the Gremlin Stole Christmas by Justin...
Data Con LA
 
PDF
DataStax | Graph Computing with Apache TinkerPop (Marko Rodriguez) | Cassandr...
DataStax
 
PDF
TinkerPop: a story of graphs, DBs, and graph DBs
Joshua Shinavier
 
PDF
Gremlin 101.3 On Your FM Dial
Marko Rodriguez
 
PDF
The Path-o-Logical Gremlin
Marko Rodriguez
 
PDF
An Introduction to Tinkerpop
Takahiro Inoue
 
PPTX
Gremlin Queries with DataStax Enterprise Graph
Stephen Mallette
 
PPTX
Meet Gremlin – your guide through graphs in Cosmos DB
Rafał Hryniewski
 
PPTX
GRAPH 101- GETTING STARTED WITH TITAN AND CASSANDRA
Shaunak Das
 
What's New in Apache TinkerPop - the Graph Computing Framework
DataStax Academy
 
TinkerPop 2020
Joshua Shinavier
 
A walk in graph databases v1.0
Pierre De Wilde
 
Introduction to Gremlin
Max De Marzi
 
From zero to gremlin hero - Part I
GraphRM
 
Traversing Graphs with Gremlin
Artem Chebotko
 
Data Con LA 2018 - Graph Computing: How the Gremlin Stole Christmas by Justin...
Data Con LA
 
DataStax | Graph Computing with Apache TinkerPop (Marko Rodriguez) | Cassandr...
DataStax
 
TinkerPop: a story of graphs, DBs, and graph DBs
Joshua Shinavier
 
Gremlin 101.3 On Your FM Dial
Marko Rodriguez
 
The Path-o-Logical Gremlin
Marko Rodriguez
 
An Introduction to Tinkerpop
Takahiro Inoue
 
Gremlin Queries with DataStax Enterprise Graph
Stephen Mallette
 
Meet Gremlin – your guide through graphs in Cosmos DB
Rafał Hryniewski
 
GRAPH 101- GETTING STARTED WITH TITAN AND CASSANDRA
Shaunak Das
 
Ad

More from Marko Rodriguez (20)

PDF
mm-ADT: A Virtual Machine/An Economic Machine
Marko Rodriguez
 
PDF
mm-ADT: A Multi-Model Abstract Data Type
Marko Rodriguez
 
PDF
Open Problems in the Universal Graph Theory
Marko Rodriguez
 
PDF
Faunus: Graph Analytics Engine
Marko Rodriguez
 
PDF
The Pathology of Graph Databases
Marko Rodriguez
 
PDF
Memoirs of a Graph Addict: Despair to Redemption
Marko Rodriguez
 
PDF
Graph Databases: Trends in the Web of Data
Marko Rodriguez
 
PDF
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Marko Rodriguez
 
PDF
A Perspective on Graph Theory and Network Science
Marko Rodriguez
 
PDF
The Graph Traversal Programming Pattern
Marko Rodriguez
 
PPT
The Network Data Structure in Computing
Marko Rodriguez
 
PPT
A Model of the Scholarly Community
Marko Rodriguez
 
PDF
General-Purpose, Internet-Scale Distributed Computing with Linked Process
Marko Rodriguez
 
PDF
Collective Decision Making Systems: From the Ideal State to Human Eudaimonia
Marko Rodriguez
 
PDF
Distributed Graph Databases and the Emerging Web of Data
Marko Rodriguez
 
PDF
An Overview of Data Management Paradigms: Relational, Document, and Graph
Marko Rodriguez
 
PDF
Graph Databases and the Future of Large-Scale Knowledge Management
Marko Rodriguez
 
PPT
Automatic Metadata Generation using Associative Networks
Marko Rodriguez
 
PDF
Evolving the Web into a Giant Global Database
Marko Rodriguez
 
PPT
Computing with Directed Labeled Graphs
Marko Rodriguez
 
mm-ADT: A Virtual Machine/An Economic Machine
Marko Rodriguez
 
mm-ADT: A Multi-Model Abstract Data Type
Marko Rodriguez
 
Open Problems in the Universal Graph Theory
Marko Rodriguez
 
Faunus: Graph Analytics Engine
Marko Rodriguez
 
The Pathology of Graph Databases
Marko Rodriguez
 
Memoirs of a Graph Addict: Despair to Redemption
Marko Rodriguez
 
Graph Databases: Trends in the Web of Data
Marko Rodriguez
 
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Marko Rodriguez
 
A Perspective on Graph Theory and Network Science
Marko Rodriguez
 
The Graph Traversal Programming Pattern
Marko Rodriguez
 
The Network Data Structure in Computing
Marko Rodriguez
 
A Model of the Scholarly Community
Marko Rodriguez
 
General-Purpose, Internet-Scale Distributed Computing with Linked Process
Marko Rodriguez
 
Collective Decision Making Systems: From the Ideal State to Human Eudaimonia
Marko Rodriguez
 
Distributed Graph Databases and the Emerging Web of Data
Marko Rodriguez
 
An Overview of Data Management Paradigms: Relational, Document, and Graph
Marko Rodriguez
 
Graph Databases and the Future of Large-Scale Knowledge Management
Marko Rodriguez
 
Automatic Metadata Generation using Associative Networks
Marko Rodriguez
 
Evolving the Web into a Giant Global Database
Marko Rodriguez
 
Computing with Directed Labeled Graphs
Marko Rodriguez
 

Recently uploaded (20)

PDF
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPTX
ChessBase 18.02 Crack + Serial Key Free Download
cracked shares
 
PDF
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
PDF
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PDF
Troubleshooting Virtual Threads in Java!
Tier1 app
 
PDF
What companies do with Pharo (ESUG 2025)
ESUG
 
PDF
Malaysia’s e-Invoice System: A Complete Guide for Businesses
Matiyas Solutions
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
SAP Public Cloud PPT , SAP PPT, Public Cloud PPT
sonawanekundan2024
 
PPTX
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
PDF
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PPTX
Chess King 25.0.0.2500 With Crack Full Free Download
cracked shares
 
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
ChessBase 18.02 Crack + Serial Key Free Download
cracked shares
 
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Troubleshooting Virtual Threads in Java!
Tier1 app
 
What companies do with Pharo (ESUG 2025)
ESUG
 
Malaysia’s e-Invoice System: A Complete Guide for Businesses
Matiyas Solutions
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
Activate_Methodology_Summary presentatio
annapureddyn
 
SAP Public Cloud PPT , SAP PPT, Public Cloud PPT
sonawanekundan2024
 
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Chess King 25.0.0.2500 With Crack Full Free Download
cracked shares
 

The Gremlin Graph Traversal Language

  • 1. The Gremlin Graph Traversal Language Marko A. Rodriguez and Daniel Kuppitz http://tinkerpop.incubator.apache.org
  • 2. user movie categoryoccupation occupation rated category name:String gender:[M,F] age:integer name:String year:integer name:String stars:[1,2,3,4,5] http://grouplens.org/datasets/movielens/ MovieLens Dataset
  • 3. user |Vuser| = 6040 |Vmovie| = 3883 movie |Vcategory| = 18 category |Voccupation| = 21 occupation occupation |Eoccupation| = 6040 rated |Erated| = 1000209 category |Ecategory| = 6408 name:String gender:[M,F] age:integer name:String year:integer name:String stars:[1,2,3,4,5] http://grouplens.org/datasets/movielens/ G = (V, E) MovieLens Dataset
  • 5. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin>
  • 6. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> Gremlin-Java8 Gremlin-Groovy* Gremlin-Scala Gremlin-Clojure Gremlin-JavaScript Gremlin-Python Gremlin-PHP ...
  • 7. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> "Create a new TinkerGraph."
  • 8. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> "Create a new TinkerGraph." G = (V = ∅, E = ∅) G The graph is a set of vertices and edges V The set of vertices in the graph E The set of edges in the graph ∅ The empty set -- no elements
  • 9. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> "Create a new TinkerGraph." G = (V = ∅, E = ∅) TitanGraph.open(…) Neo4jGraph.open(…) OrientGraph.open(…) SqlgGraph.open(…) HadoopGraph.open(…) GiraphGraphComputer SparkGraphComputer ElasticGraph.open(…) ...
  • 10. G = (V = ∅, E ⊆ (V × V )) ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> "Load the MovieLens dataset into the newly created TinkerGraph." Set A is a subset of (or equal to) set B The set of all ordered pairs of vertices (directed binary edges) A ⊆ B (V × V )
  • 11. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> "Create a graph traversal source for spawning graph traversals over the MovieLens graph."
  • 12. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> g.V().count() ==>9962 gremlin> "Count the number of vertices in the graph." |V | = 9962
  • 13. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> g.V().count() ==>9962 gremlin> "Count the number of vertices in the graph." |V | = 9962 Vertex map 9962 reducing barrier Long seed=0 value=seed binary operator: value -> value+1 count() { "m any-to-one"
  • 14. Edge map 1012657 reducing barrier Long |E| = 1012657 ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> g.V().count() ==>9962 gremlin> g.E().count() ==>1012657 gremlin> "Count the number of edges in the graph." count() { "m any-to-one"
  • 15. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> g.V().count() ==>9962 gremlin> g.E().count() ==>1012657 gremlin> g.V().label().groupCount() ==>[occupation:21, movie:3883, category:18, user:6040] gremlin> "For each vertex in the graph, emit its label, then group and count each distinct label." user user movie category ... Vertex String Map<String,Long> user user movie category map map reducing barrier [ occupation=21, movie=3883, category=18, user=6040 ] label() groupCount(){"one-to-one" "many-to-one"
  • 16. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> g.V().count() ==>9962 gremlin> g.E().count() ==>1012657 gremlin> g.V().label().groupCount() ==>[occupation:21, movie:3883, category:18, user:6040] gremlin> g.E().hasLabel('rated').values('stars').mean() ==>3.581564453029317 gremlin> "For each rated-edge in the graph, emit its stars property value and compute the average value."
  • 17. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> g.V().count() ==>9962 gremlin> g.E().count() ==>1012657 gremlin> g.V().label().groupCount() ==>[occupation:21, movie:3883, category:18, user:6040] gremlin> g.E().hasLabel('rated').values('stars').mean() ==>3.581564453029317 gremlin> g.V().hasLabel('user').map(outE('rated').count()).max() ==>2314 gremlin> "What is the maximum number of movies a single user rated?"
  • 18. ~/tinkerpop3$ bin/gremlin.sh ,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> graph.io(gryo()).readGraph('/tmp/movie-lens.kryo') ==>null gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> g.V().count() ==>9962 gremlin> g.E().count() ==>1012657 gremlin> g.V().label().groupCount() ==>[occupation:21, movie:3883, category:18, user:6040] gremlin> g.E().hasLabel('rated').values('stars').mean() ==>3.581564453029317 gremlin> g.V().hasLabel('user').map(outE('rated').count()).max() ==>2314 gremlin> g.V().hasLabel('movie').values('year').min() ==>1919 gremlin> "What year was the oldest movie made?"
  • 19. gremlin> g.V().hasLabel('category').values('name') ==>Animation ==>Children's ==>Comedy ==>Adventure ==>Fantasy ==>Romance ==>Drama ==>Action ==>Crime ==>Thriller ==>Horror ==>Sci-Fi ==>Documentary ==>War ==>Musical ==>Mystery ==>Film-Noir ==>Western "For each vertex that is labeled 'category,' emit the name property value of that vertex." category user user movie category Vertex category category category category category categorycategory category Vertex Animation Children's Comedy Adventure Western ... String filter map hasLabel('category') values('name') "one-to-[one-or-none]" "one-to-one"
  • 20. gremlin> g.V().hasLabel('category').as('a','b'). select('a','b'). by('name'). by(inE('category').count()) "For each category vertex, emit a map of its name and the number of movies it represents."
  • 21. hasLabelcategory : V ∗ → V ∗ gremlin> g.V().hasLabel('category').as('a','b'). select('a','b'). by('name'). by(inE('category').count()) "For each category vertex, emit a map of its name and the number of movies it represents." V : G → V ∗ asa,b : V ∗ → (V × V )∗ G The set of all graphs f : A → B The function f maps values of type A to values of type B A∗ A stream of values of type A (A × B) The set of all pairs of values from A and B (cross product) N The set of all natural numbers (1, 2, 3, 4, …) The set of all strings (a, b, aa, ab, bb, …) selecta,b : (V × V )∗ → a valuesname : V ∗ → S b (inEcategory : V ∗ → E∗ ) ◦ (count : E∗ → N) → (S × N)∗ S Σ∗ typically denoted
  • 22. gremlin> g.V().hasLabel('category').as('a','b'). select('a','b'). by('name'). by(inE('category').count()) ==>[a:Animation, b:105] ==>[a:Children's, b:251] ==>[a:Comedy, b:1200] ==>[a:Adventure, b:283] ==>[a:Fantasy, b:68] ==>[a:Romance, b:471] ==>[a:Drama, b:1603] ==>[a:Action, b:503] ==>[a:Crime, b:211] ==>[a:Thriller, b:492] ==>[a:Horror, b:343] ==>[a:Sci-Fi, b:276] ==>[a:Documentary, b:127] ==>[a:War, b:143] ==>[a:Musical, b:114] ==>[a:Mystery, b:106] ==>[a:Film-Noir, b:44] ==>[a:Western, b:68] "For each category vertex, emit a map of its name and the number of movies it represents." category user user movie category Vertex category category category category category categorycategory category Vertex [a:Animation, b:105] [a:Children's, b:251] [a:Comedy, b:1200] [a:Adventure, b:283] … [a:Western, b:68] Map<String,Long> filter map category name:Animation category category category ... category map map reducing barrier 105 Vertex Edge Long category name:Animation Vertex String map Animationa b map flatMap {"one-to-m any" "one-to-one"
  • 23. gremlin> g.V().hasLabel('movie').as('a','b'). select('a','b'). by('name'). by(inE('rated').values('stars').mean()). order().by(select('b'),decr). limit(10) "For each movie, emit a map of its name and average rating. Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)."
  • 24. gremlin> g.V().hasLabel('movie').as('a','b'). select('a','b'). by('name'). by(inE('rated').values('stars').mean()). order().by(select('b'),decr). limit(10) ==>[a:Charm's Incidents, b:NaN] ==>[a:Prerokbe Ognja, b:NaN] ==>[a:Leopard Son, The, b:NaN] ==>[a:Bird of Prey, b:NaN] ==>[a:Plutonium Circus, b:NaN] ==>[a:Hustler White, b:NaN] ==>[a:Curtis's Charm, b:NaN] ==>[a:Three Lives and Only One Death, b:NaN] ==>[a:Hoogste tijd, b:NaN] ==>[a:Entertaining Angels: The Dorothy Day Story, b:NaN] category user user movie category Vertex movie movie movie movie movie moviemovie movie Vertex [a:Charm's Incidents, b:NaN] [a:Prerokbe Ognja, b:NaN] [a:Leopard Son, The, b:NaN] [a:Bird of Prey, b:NaN] ... [a:Entertaining Angels, b:NaN] Map<String,Double> filter map movie name:Charm's Incidents map reducing barrier NaN Vertex Edge Double movie name:Charm's Incidents Vertex String map Charm's Incidentsa b map map Integer ... "For each movie, emit a map of its name and average rating. Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)." flatMap
  • 25. gremlin> g.V().hasLabel('movie').as('a','b'). select('a','b'). by('name'). by(coalesce( inE('rated').values('stars'), constant(0)).mean()). order().by(select('b'),decr). limit(10) ==>[a:Lured, b:5.0] ==>[a:One Little Indian, b:5.0] ==>[a:Bittersweet Motel, b:5.0] ==>[a:Gate of Heavenly Peace, The, b:5.0] ==>[a:Follow the Bitch, b:5.0] ==>[a:Schlafes Bruder (Brother of Sleep), b:5.0] ==>[a:Ulysses (Ulisse), b:5.0] ==>[a:Song of Freedom, b:5.0] ==>[a:Smashing Time, b:5.0] ==>[a:Baby, The, b:5.0] "For each movie, get its name and mean rating (or 0 if no ratings). Order by average rating and emit top 10." user user movie category Vertex movie movie movie movie moviemovie movie Vertex Map<String,Double> filter map movie name:Charm's Incidents map map reducing barrier 0.0 Vertex Integer Double movie name:Charm's Incidents Vertex String map Charm's Incidentsa b map 0 [a:Lured, b:5.0] [a:One Little Indian, b:5.0] [a:Bittersweet Motel, b:5.0] [a:Gate of Heavenly Peace, b:5.0] ... [a:Baby, The, b:5.0] Map<String,Double> map [a:Charm's Incidents, b:NaN] [a:Prerokbe Ognja, b:NaN] [a:Leopard Son, The, b:NaN] [a:Bird of Prey, b:NaN] ... [a:Entertaining Angels, b:NaN] collecting barrier
  • 26. gremlin> g.V().hasLabel('movie').as('a','b'). where(inE('rated').count().is(gt(10))). select('a','b'). by('name'). by(inE('rated').values('stars').mean()). order().by(select('b'),decr). limit(10) "For each movie with at least 11 ratings, emit a map of its name and average rating. Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)."
  • 27. gremlin> g.V().hasLabel('movie').as('a','b'). where(inE('rated').count().is(gt(10))). select('a','b'). by('name'). by(inE('rated').values('stars').mean()). order().by(select('b'),decr). limit(10) ==>[a:Sanjuro, b:4.608695652173913] ==>[a:Seven Samurai (The Magnificent Seven), b:4.560509554140127] ==>[a:Shawshank Redemption, The, b:4.554557700942973] ==>[a:Godfather, The, b:4.524966261808367] ==>[a:Close Shave, A, b:4.52054794520548] ==>[a:Usual Suspects, The, b:4.517106001121705] ==>[a:Schindler's List, b:4.510416666666667] ==>[a:Wrong Trousers, The, b:4.507936507936508] ==>[a:Sunset Blvd. (a.k.a. Sunset Boulevard), b:4.491489361702127] ==>[a:Raiders of the Lost Ark, b:4.47772] "For each movie with at least 11 ratings, emit a map of its name and average rating. Sort the maps in decreasing order by their average rating. Emit the first 10 maps (i.e. top 10)." map movie name:Sanjuro rated rated rated ... rated map reducing barrier 4.60 Vertex Edge Double movie name:Sanjuro Vertex String map Sanjuroa b map map Integer user user movie category Vertex movie movie movie movie moviemovie movie Vertex filter movie rated rated rated ... rated reducing barrier Vertex Edge map Long 69 name:Sanjuro filter movie movie movie movie movie Vertex 5 4 … 5 [[a:Sanjuro, b:4.60] [a:Seven Samurai, b:4.56] [a:Shawshank Redemption, b:4.55] [a:Godfather, The, b:4.52] ... [a:Raiders of the Lost Ark, b:4.47] […] […] […] […] … […] Map<String,Double> Map<String,Double> map collecting barrier flatMap flatMap { {
  • 28. gremlin> g.V().hasLabel('movie'). where(inE('rated').count().is(gt(10))). toString() ==>[GraphStep([],vertex), HasStep([~label.eq(movie)]), TraversalFilterStep([ VertexStep(IN,[rated],edge), CountGlobalStep, IsStep(gt(10))])] "What is the execution plan for the traversal prior to compiler optimizations being applied?" V : G → V ∗ hasLabelmovie : V ∗ → V ∗ where : V ∗ → inErated : V ∗ → E∗ count : E∗ → N isgt(10) : N → (N ∪ ∅) → V ∗ "true orfalse"
  • 29. gremlin> g.V().hasLabel('movie'). where(inE('rated').count().is(gt(10))). iterate().toString() ==>[TinkerGraphStep(vertex,[~label.eq(movie)]), TraversalFilterStep([ VertexStep(IN,[rated],edge), RangeGlobalStep(0,11), CountGlobalStep, IsStep(gt(10))])] "What is the execution plan for the traversal after compiler optimizations have been applied?" * TinkerGraphStragegy: Access vendor-specific vertex partition by label. * RangeByIsCountStrategy: Only iterate 1 more than required count. where : V ∗ → inErated : V ∗ → E∗ count : E∗ → N isgt(10) : N → (N ∪ ∅) limit11 : E∗ → E∗ Vlabel=movie : G → V ∗ → V ∗ "true orfalse"
  • 30. gremlin> g.getStrategies() ==>ConjunctionStrategy a.and().b => and(a,b) a.or().b => or(a,b) a.or().b.and().c => or(a,and(b,c)) a.and().b.or().c => or(and(a,b),c) ==>IncidentToAdjacentStrategy a.outE().inV().b => a.out().b ==>AdjacentToIncidentStrategy a.in().count().b => a.inE().count().b a.where(out()).b => a.where(outE()).b a.and(in(),out()).b => a.and(inE(),outE()).b ==>IdentityRemovalStrategy a.identity().b => a.b ==>FilterRankingStrategy a.order().dedup().b => a.dedup().order().b a.and(c,d).has().b => a.has().and(c,d).b a.simplePath().where().b => b.where().simplePath().a ==>MatchPredicateStrategy a.match(c,d).where(e).b => a.match(c,d,e) a.match(has(),c,d).b => a.has().match(c,d).b ==>RangeByIsCountStrategy a.count().is(0) => a.limit(1).count().is(0) ==>TinkerGraphStepStrategy V.has().has().b => V[has,has].b ==>ProfileStrategy a.b.c.profile() => a.profile().b.profile().c.profile() ==>ComputerVerificationStrategy a.order.b => IllegalStateException a.local(out().out()).b => IllegalStateException "What compilation strategies are associated with the graph traversal source?"
  • 32. gremlin> g.V().has('movie','name','Die Hard'). inE('rated').values('stars').mean() ==>4.121848739495798 "What is Die Hard's average rating?" movie movie movie Vertex movie Vertex filter name:Die Hard flatMap rated rated rated ... rated Edge map 3 5 5 … 4 Integer map Double 4.1218 reducing barrier V : G → V ∗ hasLabelmovie : V ∗ → V ∗ hasname=Die Hard : V ∗ → V ∗ inErated : V ∗ → E∗ mean : N∗ → R valuesstars : E∗ → N∗ user user movie user Vertex filter { "one-to-[one-or-none]" "one-to-[one-or-none]" "one-to-many" "one-to-one" "many-to-one"
  • 33. gremlin> g.V().has('movie','name','Die Hard').as('a'). inE('rated').has('stars',5).outV(). where(out('occupation').has('name','programmer')). outE('rated').has('stars',5).inV(). where(neq('a')). groupCount().by('name'). order(local).by(valueDecr). limit(local,10). unfold() // so its not printed on a single line "Which programmers like Die Hard and what other movies do they like? Group and count the movies by their name. Sort the group count map in decreasing order by the count. Clip the map to the top 10 entries and stream out the map's entries (for display purposes)."
  • 34. gremlin> g.V().has('movie','name','Die Hard').as('a'). inE('rated').has('stars',5).outV(). where(out('occupation').has('name','programmer')). outE('rated').has('stars',5).inV(). where(neq('a')). groupCount().by('name'). order(local).by(valueDecr). limit(local,10). unfold() // so its not printed on a single line "Which programmers like Die Hard and what other movies do they like? Group and count the movies by their name. Sort the group count map in decreasing order by the count. Clip the map to the top 10 entries and stream out the map's entries (for display purposes)." user user movie user Vertex movie Vertex filter filter name:Die Hard flatMap rated rated rated ... rated Edge filter rated ... rated Edge map Vertex user user user useruser user Vertex Vertex occupationflatMap Vertex filter rated rated rated ... rated Edge filter rated ... rated Edge map Vertex movie movie moviemovie movie Vertex Vertex user user user flatMap filter movie Vertex filter Vertex movie movie movie map reducing barrier [ Aliens=105, Braveheart=24, … Pulp Fiction=19 ] Map<String,Long> map collecting barrier [ Raider of the Lost Ark=36, Star Wars: Episode V=24, Star Wars: Episode IV=34 … Airplane II: The Sequel=1 ] Map<String,Long> [ Raider of the Lost Ark=36, Star Wars: Episode V=24, Star Wars: Episode IV=34 … Alien=22 ] Map<String,Long> map occupation programmer not Die Hard
  • 35. gremlin> g.V().has('movie','name','Die Hard').as('a'). inE('rated').has('stars',5).outV(). where(out('occupation').has('name','programmer')). outE('rated').has('stars',5).inV(). where(neq('a')). groupCount().by('name'). order(local).by(valueDecr). limit(local,10). unfold() // so its not printed on a single line ==>Raiders of the Lost Ark=36 ==>Star Wars: Episode V - The Empire Strikes Back=36 ==>Star Wars: Episode IV - A New Hope=34 ==>Matrix, The=32 ==>Terminator, The=29 ==>Star Wars: Episode VI - Return of the Jedi=26 ==>Sixth Sense, The=26 ==>Braveheart=24 ==>Aliens=23 ==>Alien=22 gremlin> "Which programmers like Die Hard and what other movies do they like? Group and count the movies by their name. Sort the group count map in decreasing order by the count. Clip the map to the top 10 entries and stream out the map's entries (for display purposes)."
  • 37. "What 80's action movies do 30-something programmers like? Group count the movies by their name and sort the group count map in decreasing order by value. Clip the map to the top 10 and emit the map entries." gremlin> g.V(). match( __.as('a').hasLabel('movie'), __.as('a').out('category').has('name','Action'), __.as('a').has('year',between(1980,1990)), __.as('a').inE('rated').as('b'), __.as('b').has('stars',5), __.as('b').outV().as('c'), __.as('c').out('occupation').has('name','programmer'), __.as('c').has('age',between(30,40))). select('a').groupCount().by('name'). order(local).by(valueDecr). limit(local,10). unfold() // so its not printed on a single line ==>Raiders of the Lost Ark=26 ==>Star Wars: Episode V - The Empire Strikes Back=26 ==>Terminator, The=23 ==>Star Wars: Episode VI - Return of the Jedi=22 ==>Princess Bride, The=19 ==>Aliens=18 ==>Boat, The (Das Boot)=11 ==>Indiana Jones and the Last Crusade=11 ==>Star Trek: The Wrath of Khan=10 ==>Abyss, The=9 gremlin>
  • 38. MatchStep GraphTraversal.match(Traversal... traversalPatterns) x.match( a...b a...c c... or( a...c a...b ) c.repeat(...).b not(c...a) b...count().e c...count().e ).dedup(a,b).y a,b,c,e : once a variable is set, it must hold equal for all patterns c... : "predicate patterns" simply check for the existence of a result or()/and() : nested conjunctive patterns supported repeat(...) : recursive patterns supported not(...) : not'ing of patterns supported count() : barrier patterns supported dedup(a,b) : internal de-duplication of variable values supported x.match().y : possible to go from imperative to declarative, etc. Plug and Play MatchAlgorithms GreedyMatchAlgorithm : try each pattern in the order provided by the user CountMatchAlgorithm : continually re-sort patterns by the cardinality of their set reductions
  • 39. // CountMatchAlgorithm (default) gremlin> clockWithResult(50){ g.V().match( __.as('a').out('rated').as('b'), __.as('a').out('occupation').has('name','farmer')). select('a','b').count().next()} ==>66.31955294 // time in milliseconds ==>2706 // number of results // GreedyMatchAlgorithm gremlin> g = graph.traversal(GraphTraversalSource.build(). with(MatchAlgorithmStrategy.build(). algorithm(MatchStep.GreedyMatchAlgorithm).create())) ==>graphtraversalsource[tinkergraph[vertices:9962 edges:1012657], standard] gremlin> clockWithResult(50){ g.V().match( __.as('a').out('rated').as('b'), __.as('a').out('occupation').has('name','farmer')). select('a','b').count().next()} ==>1902.6290871599997 // time in milliseconds ==>2706 // number of results "Which movies did each farmer rate? -- benchmark CountMatchAlgorithm vs. GreedyMatchAlgorithm." farmermoviesusers 1000209 2706 farmer moviesusers 17 2706
  • 40. gremlin> g.V().hasLabel('movie'). where(inE('rated').count().is(gt(10))). group(). by{((int)(it.value('year') / 10)) * 10}. by(). by(unfold().order(). by(inE('rated').values('stars').mean(),decr). values('name'). limit(1)). order(local).by(keyIncr). unfold() // so its not printed on a single line "What is the most liked movie in each decade?"
  • 41. gremlin> g.V().hasLabel('movie'). where(inE('rated').count().is(gt(10))). group(). by{((int)(it.value('year') / 10)) * 10}. by(). by(unfold().order(). by(inE('rated').values('stars').mean(),decr). values('name'). limit(1)). order(local).by(keyIncr). unfold() // so its not printed on a single line "What is the most liked movie in each decade?" λ λ Nearly every step that takes a traversal argument can also take a lambda. It is recommended that users do not use lambdas as they are not subject to traversal strategy (i.e. compiler) optimization. However, they are useful when no provided step yields the desired computation.
  • 42. gremlin> g.V().hasLabel('movie'). where(inE('rated').count().is(gt(10))). group(). by{((int)(it.value('year') / 10)) * 10}. by(). by(unfold().order(). by(inE('rated').values('stars').mean(),decr). values('name'). limit(1)). order(local).by(keyIncr). unfold() // so its not printed on a single line ==>1910=Daddy Long Legs ==>1920=General, The ==>1930=City Lights ==>1940=Third Man, The ==>1950=Seven Samurai (The Magnificent Seven) ==>1960=Sanjuro ==>1970=Godfather, The ==>1980=Raiders of the Lost Ark ==>1990=Shawshank Redemption, The ==>2000=Almost Famous gremlin> "What is the most liked movie in each decade?"
  • 43. gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties') ==>hadoopgraph[gryoinputformat->gryooutputformat] gremlin> g = graph.traversal(computer(SparkGraphComputer)) ==>graphtraversalsource [hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer] gremlin> "Which movies are most central in the implicit 5-stars graph?"
  • 44. gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties') ==>hadoopgraph[gryoinputformat->gryooutputformat] gremlin> g = graph.traversal(computer(SparkGraphComputer)) ==>graphtraversalsource [hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer] gremlin> g.V().repeat(outE('rated').has('stars', 5).inV(). groupCount('m').by('name'). inE('rated').has('stars', 5).outV()). times(4).cap('m') "Which movies are most central in the implicit 5-stars graph?" user movie user ratedrated m 4x cap('m') repeat(…).times(4) g.V() stars=5 stars=5
  • 45. gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties') ==>hadoopgraph[gryoinputformat->gryooutputformat] gremlin> g = graph.traversal(computer(SparkGraphComputer)) ==>graphtraversalsource [hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer] gremlin> g.V().repeat(outE('rated').has('stars', 5).inV(). groupCount('m').by('name'). inE('rated').has('stars', 5).outV()). times(4).cap('m') ==>Fantasia 2000=2676505178171564 ==>Pale Rider=1369969000295362 ==>Crucible, The=401712993698149 ==>About Adam=37981148456999 ==>Akira=3659939409345918 ... gremlin> "Which movies are most central in the implicit 5-stars graph?"
  • 46. gremlin> graph = HadoopGraph.open('conf/hadoop/movie-lens.properties') ==>hadoopgraph[gryoinputformat->gryooutputformat] gremlin> g = graph.traversal(computer(SparkGraphComputer)) ==>graphtraversalsource [hadoopgraph[gryoinputformat->gryooutputformat], sparkgraphcomputer] gremlin> g.V().repeat(outE('rated').has('stars', 5).inV(). groupCount('m').by('name'). inE('rated').has('stars', 5).outV()). times(4).cap('m') ==>Fantasia 2000=2676505178171564 ==>Pale Rider=1369969000295362 ==>Crucible, The=401712993698149 ==>About Adam=37981148456999 ==>Akira=3659939409345918 ... gremlin> hdfs.ls('output/m') ==>rw-r--r-- daniel supergroup 0 _SUCCESS ==>rw-r--r-- daniel supergroup 245314 part-r-00000 gremlin> hdfs.head('output/m', ObjectWritable).sort {-it.value}.take(10) ==>Star Wars: Episode IV - A New Hope 35405394353105332 ==>American Beauty 31943228282020585 ==>Raiders of the Lost Ark 31224779793238499 ==>Star Wars: Episode V - The Empire Strikes Back 30434677119726223 ==>Godfather, The 30258518523013057 ==>Shawshank Redemption, The 28297717387901031 ==>Schindler's List 27539336654199309 ==>Silence of the Lambs, The 26736276376806173 ==>Fargo 26531050311325270 ==>Matrix, The 26395118239203191 "Which movies are most central in the implicit 5-stars graph?"
  • 47. gremlin> :plugin use tinkerpop.gephi ==>tinkerpop.gephi activated gremlin> :remote connect tinkerpop.gephi ==>Connection to Gephi - http://localhost:8080/workspace0 with stepDelay:1000, startRGBColor:[0.0, 1.0, 0.5], colorToFade:g, colorFadeRate:0.7, startSize: 20.0,sizeDecrementRate:0.33 gremlin>
  • 48. gremlin> :plugin use tinkerpop.gephi ==>tinkerpop.gephi activated gremlin> :remote connect tinkerpop.gephi ==>Connection to Gephi - http://localhost:8080/workspace0 with stepDelay:1000, startRGBColor:[0.0, 1.0, 0.5], colorToFade:g, colorFadeRate:0.7, startSize: 20.0,sizeDecrementRate:0.33 gremlin> :> g.V().hasLabel('user'). order(). by(outE('rated').count(), decr).limit(10).as('a'). local(outE('rated').order(). by('stars', decr). // first by stars by(inV().inE('rated').count(), decr). // then by ratings limit(10)). subgraph('sg').inV().outE('category'). subgraph('sg').select('a').outE('occupation'). subgraph('sg').cap('sg').next() ==>tinkergraph[vertices:82 edges:233] gremlin> "Which users rated the most movies? For each user, display their 10 favorite movies, the categories of those movies, and their occupation. "moviebuffs"