• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to Gremlin

Introduction to Gremlin






Total Views
Views on SlideShare
Embed Views



10 Embeds 7,342

http://maxdemarzi.com 6702
http://architects.dzone.com 564
http://www.twylah.com 53
http://coderwall.com 13
https://twimg0-a.akamaihd.net 2
http://webcache.googleusercontent.com 2
http://dzone.com 2
http://www.google.com 2
http://www.the-movies.net 1
http://tweetedtimes.com 1


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Introduction to Gremlin Introduction to Gremlin Presentation Transcript

    • 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: http://maxdemarzi.com• Find me on Twitter: @maxdemarzi• Email me: maxdemarzi@gmail.com• GitHub: http://github.com/maxdemarzi
    • 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 Graph Database Vendors• Primarily seen with the Groovy Language• With JVM connectivity in Java, Scala, and other languages
    • 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 name=Peter
    • g.v(1). out(‘created’).in(‘contributed’).name.back(1) 2 contributed 1 5 name=Peter
    • 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
    • Gremlin Steps
    • Gremlin Transform Steps_ in memoizeV inE gatherE inV scatterid both pathlabel bothE capout bothVoutE select keyoutV map transform
    • Gremlin Filter Steps[i] dedup[i..j] simplePathhas excepthasNot retainback filterandorrandom
    • 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. 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]
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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
    • Questions? ?
    • Thank you! http://maxdemarzi.com