CRUD CheatSheet to Apache TinkerPop Gremlin.

Ravi Raja Merugu
May 21 · 3 min read
Image for post
Image for post

Apache TinkerPopGremlin 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')
.property('email', 'xxxx@invana.io')
.iterate();
organisation = g.addV('Organisation')
.property('name', 'Invana')
.property('website', 'https://www.invana.io')
.iterate();

1.2 Create an Edge

edge = person.addEdge("founded", organisation)or g.V(<personId>).addEdge(founded,g.V(<orgId>));
Image for post
Image for post
Created using https://github.com/invanalabs/graph-explorer

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

The keyword 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

g.V(<personId>)or g.V(123)

3.2 Filter Graph by Vertices by Property

g.V().has("username", "rrmerugu")

3.3 Filter Graph by Vertex by label

g.V().hasLabel("Person").toList()

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

g.V(126525496).drop()

4.2 Delete an Edge

g.E("24wyrr-23bvp4-gkk5-25ia5c").drop()

5. Do a little more than CRUD

5.1 To search for a word or a string “covid” in the field “text”.

g.V().has("text",Text.textContains("covid",)).count()

5.2 Search with two conditions conjugated by “and”

g.V().where(has(‘text’,Text.textContains(‘covid’)).and().has(‘text’,Text.textContains(‘government’)))

5.3 Search with two conditions conjugated by “or”

g.V().where(has(‘text’,Text.textContains(‘covid’)).or().has(‘text’,Text.textContains(‘government’)))

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 used gh.com/invanalabs/graph-explorer as visualiser and janusgraph as Graph Database.

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.

Invana

Product Stories, Research and Engineering Updates at Invana

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store