Apache TinkerPop™ Gremlin is a functional, data-flow language that enables users to succinctly express complex traversals on (or queries of) their application’s property graph. — tinkerpop.apache.org
Apache TinkerPop™ is a graph computing framework for both graph databases (OLTP) and graph analytic systems (OLAP). So in other words, Gremlin is like SQL, a language that lets you perform operations on Property Graph Databases.
Note: This blog is intended to be more of a cheatsheet for Gremlin and not intended to teach Graph Data, Knowledge graphs and Graph Databases. I will write another blogs about Graph Databases and Graph Theory soon. But for now, I assume you are familiar with them.
The reason I’m writing about CRUD cheatsheet first is, I believe it’s easy to connect-to, for most of the developers who don’t come from Graph Theory Backgrounds.
1. Create Data in to Graph
1.1 Create a Vertex
person = g.addV('Person')
.property('name', 'Ravi Raja')
.iterate();organisation = g.addV('Organisation')
1.2 Create an Edge
edge = person.addEdge("founded", organisation)or g.V(<personId>).addEdge(founded,g.V(<orgId>));
2. Update Data in Graph
2.1 Update Property of a Vertex
g.V(<PersonId>).property(“last_name”, “Merugu”).iterate()org.V(126525496).property("last_name", "Merugu").iterate()
2.2 Update Property of an Edge
g.E(<edgeId>).property("founded_year", 2007).iterate()org.E(1123-21231-12213-9216).property("founded_year", 2007).iterate()
3. Read DataGraph — Read one, Filter
has and hasLabel does all the magic, it can be used as
hasLabel to filter Vertex type or
has to filter by property.
3.1 Find a Vertex by Id
3.2 Filter Graph by Vertices by Property
3.3 Filter Graph by Vertex by label
3.4 Find a Vertex by label and property
g.V().hasLabel("Person").has("username", "rrmerugu") //filter by Vertex type and Property
4. Delete Data in Graph
4.1 Delete a Vertex
4.2 Delete an Edge
5. Do a little more than CRUD
5.1 To search for a word or a string “covid” in the field “text”.
5.2 Search with two conditions conjugated by “and”
5.3 Search with two conditions conjugated by “or”
Again! a lot of use cases can be covered to show the real power of Gremlin, and how it makes Graph Databases more powerful giving you ability to query the data faster.
I’m looking forward to writing more articles on Graph Databases, their use cases, how to build knowledge graphs, exploring graphs in near future (not necessarily in the same order).
Check out my latest work on building knowledge graph of twitter use case https://github.com/invanalabs/-twitter-usecase-playground using my latest work gh.com/invanalabs/invana-python.
If you enjoyed reading this article, please share the knowledge and follow for more articles like this.