Hi everyone, We are gonna start with a brief explanation about topic. Gremlin is the graph traversal language of Apache TinkerPop. It is a functional, data-flow language that enables users to succinctly express complex traversals on (or queries of) their application’s property graph.
After installation from this website ,start the gremlin console.
Creating a Graph
This is our graph which is we are gonna build. First of all we create our nodes.
graph = TinkerGraph.open()
v1=graph.addVertex(T.id,1,T.label,"person","personId","p4361","name","Jonny Deep")
v2=graph.addVertex(T.id,2,T.label,"user","userId","u185","age",12,"gender","M")
v3=graph.addVertex(T.id,3,T.label,"user","userId","u75","age",17,"gender","F")
v4=graph.addVertex(T.id,4,T.label,"movie","movieId","m267","title","Alice in Wonderland","year",2010,"duration","108","country","US")
v5=graph.addVertex(T.id,5,T.label,"genre","genreId","g2","name","Adventure")
v6=graph.addVertex(T.id,6,T.label,"movie","movieId","m16","title","Alice in Wonderland","year",1951,"duration","75","country","US")Now, we build their edges.
v4.addEdge("actor",v1,"id",7)
v4.addEdge("belongsTo",v5,"id",8)
v2.addEdge("rated",v4,"id",9,"rating",5)
v2.addEdge("rated",v6,"id",10,"rating",6)
v6.addEdge("belongsTo",v5,"id",11)
v3.addEdge("knows",v2,"id",12)Graph Traversal
We use graph traversal if we want to draw a desired property of the node.
g=graph.traversal()
g.V().has("title","Alice in Wonderland").has("year",2010).out("actor").values("name")values(“age”) take the age values for us. If “values()” is not added at the end ,only the nodes and their numbers will be printed
Get Zehra’s stories in your inbox
Join Medium for free to get updates from this writer.
mean(): averaging
g.V().has("gender",within("F","M")).values("age")
g.V().has("gender",within("F","M")).values("age").mean()
g.V().has("gender",within("F","M"))in() & out()
g.V().has("country","US").in("rated").values("age")
g.V().has("gender","M").out("rated").values("title","year")We can group them according to the feature we choose.
g.V().group().by(label)In the next part of our series, we will continue with expanding our graph.
We’ll add 2 more vertex and edges.
v7=graph.addVertex(T.id,13,T.label,"person","personId","p5206","name","Linda Woolverton")
v8=graph.addVertex(T.id,14,T.label,"person","personId","p8153","name","Tim Burton")
v4.addEdge("screenwriter",v7,"id",15)
v4.addEdge("director",v8,"id",16)If we want to learn the name of the director of the movie Alice in Wonderland, which was released in 2010.
(==>Tim Burton)
g.V().has("title","Alice in Wonderland").has("year",2010).out("director").values("name")If we want the names of both director and screenwriter, we just need to add “screenwriter”.
(==>Tim Burton
==>Linda Woolverton)
g.V().has("title","Alice in Wonderland").has("year",2010).out("director","screenwriter").values("name")Note: If you wanna learn more about tinkerpop, I recommend you check out this YouTube video and tinkerpop tutorial.
https://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#gremlininstall








