Introduction to       Gremlin Chicago Graph Database Meet-Up         Max De Marzi
About Me    Built the Neography Gem (Ruby    Wrapper to the Neo4j REST API)    Playing with Neo4j since 10/2009•   My Blog...
Agenda•   What is Gremlin?•   Gremlin in Neo4j•   Gremlin Steps•   Gremlin Recommends
What is Gremlin?
Not a Car
Not a little Monster
Gremlin is• A Graph Traversal Language• A domain specific language for traversing  property graphs• Implemented by most Gr...
Created by:Marko Rodriguezhttp://markorodriguez.com
A Graph DSLA Dynamic Language for the JVMA Data Flow Framework“JDBC” for Graph DBs
Gremlin in Neo4j
Gremlin in Neo4jg = (neo4jgraph[EmbeddedGraphDatabase [/neo4j/data/graph.db]]
g.v(1) 1
g.v(1).first_name    first_name=Max         1
g.v(1).last_name   last_name=De Marzi          1
g.v(1).map()   first_name=Max last_name=De Marzi        1
g.v(1).outE    knows1
g.v(1).outE.since               null               since=2009    knows1              since=2010
g.v(1).outE.inV            2    knows   31            4
g.v(1).outE.inV.name            2   name=neography    knows   3    name=Neo4j1            4   name=Gremlin
g.v(1).outE.filter{it.label==‘knows’}     knows 1
g.v(1).outE.filter{it.label==‘knows’}.count()                 2
g.v(1). outE.filter{it.label==‘knows’}.inV.name      knows   3     name=Neo4j 1              4    name=Gremlin
g.v(1). out(‘knows’).name    knows      3     name=Neo4j1               4    name=Gremlin
g.v(1). out(‘created’)                 2   1
g.v(1). out(‘created’).in(‘contributed’)                 2                         contributed   1                 5
g.v(1). out(‘created’).in(‘contributed’).name                2                        contributed   1                5    ...
g.v(1). out(‘created’).in(‘contributed’).name.back(1)                2                        contributed   1             ...
g.v(1). out(‘created’).in(‘contributed’).name.back(1).sideEffect{g.addEdge(g.v(1), it, ‘collaborator’)}                   ...
Gremlin Steps
Gremlin Transform Steps_           in        memoizeV           inE       gatherE           inV       scatterid          b...
Gremlin Filter Steps[i]        dedup[i..j]     simplePathhas           excepthasNot           retainback           filtera...
Gremlin Side-Effect StepsgroupBy      sideEffectgroupCountaggregatetabletreeasoptionalstore
Gremlin Branch StepsloopifThenElsecopySplitfairMergeexhaustMerge
Gremlin Recommends
Our Graph (from MovieLens)
Recommendation Algorithmm = [:];x = [] as Set;           (continued)v = g.v(node_id);        outV.                        ...
Explanationm = [:];x = [] as Set;v = g.v(node_id);In Groovy [:] is a map, we will return thisThe set “x” will hold the col...
Explanationv.out(hasGenre).       (we are now at a genre node)aggregate(x).We fill the empty set “x” with the genres of ou...
Explanationback(2).                 (we are back to our starting point)inE(rated).filter{it. stars > 3}.   (we are now at ...
ExplanationoutV.                   (we are now at a user node)outE(rated).filter{it.stars > 3}.   (we are now at the link ...
ExplanationinV.                 (we are now at a movie node)filter{it != v}.We follow our relationships to the movies who ...
Explanationfilter{it.out(hasGenre).toSet().equals(x)}.We also want to keep only the movies that have the same genres as ou...
ExplanationgroupCount(m){"${it.id}:${it.title}"}.iterate();groupCount does what it sounds like and stores the values in th...
Explanationm.sort{a,b -> b.value <=> a.value}[0..24]Finally, we sort our map by value in descending order and grab the top...
How to treat Gremlin in Neo4jAs the equivalent of Stored Procedures in SQL.Allow only parameters from end-users, do notgen...
Questions?  ?
Thank you! http://maxdemarzi.com
Upcoming SlideShare
Loading in...5
×

Introduction to Gremlin

15,250

Published on

1 Comment
11 Likes
Statistics
Notes
No Downloads
Views
Total Views
15,250
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
204
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide

Introduction to Gremlin

  1. 1. Introduction to Gremlin Chicago Graph Database Meet-Up Max De Marzi
  2. 2. About Me Built the Neography Gem (Ruby Wrapper to the Neo4j REST API) Playing with Neo4j since 10/2009• My Blog: http://maxdemarzi.com• Find me on Twitter: @maxdemarzi• Email me: maxdemarzi@gmail.com• GitHub: http://github.com/maxdemarzi
  3. 3. Agenda• What is Gremlin?• Gremlin in Neo4j• Gremlin Steps• Gremlin Recommends
  4. 4. What is Gremlin?
  5. 5. Not a Car
  6. 6. Not a little Monster
  7. 7. Gremlin is• A Graph Traversal Language• A domain specific language for traversing property graphs• Implemented by most Graph Database Vendors• Primarily seen with the Groovy Language• With JVM connectivity in Java, Scala, and other languages
  8. 8. Created by:Marko Rodriguezhttp://markorodriguez.com
  9. 9. A Graph DSLA Dynamic Language for the JVMA Data Flow Framework“JDBC” for Graph DBs
  10. 10. Gremlin in Neo4j
  11. 11. Gremlin in Neo4jg = (neo4jgraph[EmbeddedGraphDatabase [/neo4j/data/graph.db]]
  12. 12. g.v(1) 1
  13. 13. g.v(1).first_name first_name=Max 1
  14. 14. g.v(1).last_name last_name=De Marzi 1
  15. 15. g.v(1).map() first_name=Max last_name=De Marzi 1
  16. 16. g.v(1).outE knows1
  17. 17. g.v(1).outE.since null since=2009 knows1 since=2010
  18. 18. g.v(1).outE.inV 2 knows 31 4
  19. 19. g.v(1).outE.inV.name 2 name=neography knows 3 name=Neo4j1 4 name=Gremlin
  20. 20. g.v(1).outE.filter{it.label==‘knows’} knows 1
  21. 21. g.v(1).outE.filter{it.label==‘knows’}.count() 2
  22. 22. g.v(1). outE.filter{it.label==‘knows’}.inV.name knows 3 name=Neo4j 1 4 name=Gremlin
  23. 23. g.v(1). out(‘knows’).name knows 3 name=Neo4j1 4 name=Gremlin
  24. 24. g.v(1). out(‘created’) 2 1
  25. 25. g.v(1). out(‘created’).in(‘contributed’) 2 contributed 1 5
  26. 26. g.v(1). out(‘created’).in(‘contributed’).name 2 contributed 1 5 name=Peter
  27. 27. g.v(1). out(‘created’).in(‘contributed’).name.back(1) 2 contributed 1 5 name=Peter
  28. 28. g.v(1). out(‘created’).in(‘contributed’).name.back(1).sideEffect{g.addEdge(g.v(1), it, ‘collaborator’)} 2 contributed 1 5 name=Peter collaborator
  29. 29. Gremlin Steps
  30. 30. Gremlin Transform Steps_ in memoizeV inE gatherE inV scatterid both pathlabel bothE capout bothVoutE select keyoutV map transform
  31. 31. Gremlin Filter Steps[i] dedup[i..j] simplePathhas excepthasNot retainback filterandorrandom
  32. 32. Gremlin Side-Effect StepsgroupBy sideEffectgroupCountaggregatetabletreeasoptionalstore
  33. 33. Gremlin Branch StepsloopifThenElsecopySplitfairMergeexhaustMerge
  34. 34. Gremlin Recommends
  35. 35. Our Graph (from MovieLens)
  36. 36. Recommendation Algorithmm = [:];x = [] as Set; (continued)v = g.v(node_id); outV. outE(rated).v. filter{it.stars > 3}.out(hasGenre). inV.aggregate(x). filter{it != v}.back(2). filter{it.out(hasGenre).toSet().equals(x)}.inE(rated). groupCount(m){"${it.id}:${it.title}"}.iterate();filter{it. stars > 3}. m.sort{a,b -> b.value <=> a.value}[0..24]
  37. 37. Explanationm = [:];x = [] as Set;v = g.v(node_id);In Groovy [:] is a map, we will return thisThe set “x” will hold the collection of genres we want our recommendedmovies to have.v is our starting point.
  38. 38. Explanationv.out(hasGenre). (we are now at a genre node)aggregate(x).We fill the empty set “x” with the genres of our movie.These are the properties we want to make sure our recommendations have.
  39. 39. Explanationback(2). (we are back to our starting point)inE(rated).filter{it. stars > 3}. (we are now at the link between our movie and users)We go back two steps to our starting movie, go to the relationship ‘rated’and filter it so we only keep those with more than 3 stars.
  40. 40. ExplanationoutV. (we are now at a user node)outE(rated).filter{it.stars > 3}. (we are now at the link between user and movie)We follow our relationships to the users who made them, and thengo to the “rated” relationships of movies which also received morethan 3 stars.
  41. 41. ExplanationinV. (we are now at a movie node)filter{it != v}.We follow our relationships to the movies who received the, but filter out “v”which is our starting movie. We do not want the system to recommend thesame movie we just watched.
  42. 42. Explanationfilter{it.out(hasGenre).toSet().equals(x)}.We also want to keep only the movies that have the same genres as ourstarting movie. People gave Toy Story and Terminator both 4 stars,but you wouldn’t want to recommend one to the other.
  43. 43. ExplanationgroupCount(m){"${it.id}:${it.title}"}.iterate();groupCount does what it sounds like and stores the values in the map “m”we created earlier, but we to retain the id and title of the movies.iterate() is needed from the Neo4j REST API, the gremlin shell doesit automatically for you. You will forget this one day and kill30 minutes of your life trying to figure out why you get nothing.
  44. 44. Explanationm.sort{a,b -> b.value <=> a.value}[0..24]Finally, we sort our map by value in descending order and grab the top25 items… and you’re done.See http://maxdemarzi.com/2012/01/16/neo4j-on-heroku-part-two/for the full walk-through including data loading.
  45. 45. How to treat Gremlin in Neo4jAs the equivalent of Stored Procedures in SQL.Allow only parameters from end-users, do notgenerate gremlin dynamically or you’ll have themother of all SQL injection vulnerabilities…Gremlin => Groovy => JVM => Full Power
  46. 46. Questions? ?
  47. 47. Thank you! http://maxdemarzi.com
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×