Neo4j comes with enhanced connectivity of data and whiteboard friendly paradigm. It also brings a gremlin in your code : one of the supported graph query language brings a refreshing look at how one can search for data in a vast and interconnect web of data. Gremlin provides an abstract layer that make it easy to express your business logic without fighting with the code. It may even change your mind on object oriented programming.
18. Intro recap
nodes and vertices : basic blocs
in and out (and both) : navigation
except(), in(‘label’) : filtering
19. Traversing the
graph
Means reading information in the graph
Traversing involves listing nodes then following
links until all conditions are met
The graph contains Vertices and Edges. Is there
anything else ?
25. Gremlin functions
Pipe level function
in, out, unique, count,
Node level function
map, has, filter{}
Value level
{property}
26. Property level
// making name UPPERCASE
g.v(79).name.toUpperCase(); EXT/GEARMAN
// size of the name’s string
g.v(130).name.toList().size(); 13
// extracting words http://groovy.codehaus.o
in a string
Documentation
g.v(146).transform{ it.name.tokenize();}
[Johann-Peter, Hartmann]
30. Working with pipes
Pipes functions often offer possibility for closure
Closure is between {}
and uses ‘it’ as current node
Closure often have a standard default behavior, so
they are sometimes stealth
31. Filtering
filter links by label (in/out/both(‘label’))
Filter node with has(‘property’,
‘value’)
or
hasNot(‘property’, null)
Filter authors with 14 or more extensions
g.V.in(‘WROTE’).filter{it.out(‘WROTE’).count() > 14}
Ilia Alshanetsky, Wez Furlong, Sara Golemon
Filter allows us to work within the pipe
32. Filtering
List of contributors with more ext in two categories
g.V.in(‘wrote’).filter{
it.out(‘wrote’).in(‘has’).unique().count() > 2
}.name
Filter longer than query ?
33. GroupCount
groupCount(m)
Number of categories by author
g.V.out(‘wrote’).in(‘has’).groupCount(m)
Apparently counted but
who is v(274) ?
==> v[380]=1=
v[379]=1==>
v[378]=2==>
v[273]=2==>
v[274]=2==>
v[272]=2==>
v[173]=3==>
v[240]=2==>
35. GroupCount
Count of categories, without PHP standard
distribution
The second closure counts elements (default +1)
g.V.has(‘wrote’)
.in(‘has’)
.groupCount(m)
{it.name}
{if (!it.name in [‘mysql’, ‘timezonedb’,’gd’,
‘dbase’ /* ....*/]) { it.b + 1;} }
36. Pipes
array notation
closure usage
useful function for pipes :
groupcount, groupby{key}{value}{mapreduce},
ordermap, [n..m] operator,
More on http://gremlindocs.com/
37. Graph modifications
Gremlin allows graph modification
Adding a type property to authors
g.V.in(‘WROTE’).each{
it.setProperty(‘type’, ‘author’);
}
38. Updating on the way
sideEffect runs a closure, but keep running the
query
Adding type to extensions AND categories in the
same query
g.V.as(‘ext’)
.in(‘HAS’)
.sideEffect{
it.setProperty(‘type’, ‘Category’);
.back(‘ext’)
.sideEffect{
it.setProperty(‘type’, ‘Extension’);
}
}
39. back tracking
back( ‘name’ ) : goes back to the vertex or edge
that was named with as(‘name’)
back( n ) : goes back n vertex or edges behind
Make is possible to check a branch, come back and
check another branch
43. Kevin Bacon
Suggest collaborators to authors ?
Authors who worked with collaborators but not with
the author, are recommendations
g.V.has(‘name’, ‘contrib’).sideEffect{init =
it}out(‘wrote’).in(‘wrote’).except(init).has(‘name’,‘con
trib2’).path