Interactive Query with Gremlin

Gremlin is one of the most popular query languages in graph databases, as SQL in relational databases. Here, we will give some examples to illustrate how gremlin helps navigate the vertices and edges of a graph.

In [ ]:
# Install graphscope package if you are NOT in the Playground

!pip3 install graphscope


MODERN, a toy graph from tinkerpop, which consists of 6 vertices and 6 edges. Here are the edges:


In [ ]:
# Import the graphscope module and load the modern graph

import graphscope
from graphscope.dataset import load_modern_graph

graphscope.set_option(show_log=False)  # enable logging

modern_graph = load_modern_graph()
In [ ]:
# Create GIE engine for Gremlin

interactive = graphscope.gremlin(modern_graph)

Between Vertices

Traversals between two particular vertices is quite common situations in graph databases. For example, to figure out the relationships between v1 and v2/v3, a gremlin query can be written like this:

In [ ]:
q1 = interactive.execute(
    'g.V().has("id", 1).as("u").out().has("id", eq(2).or(eq(3))).as("v").select("u", "v").by("id")'

for p in q1:

Here is an example which is popular in social network scenarios, such as finding common features between two different people, one called "marko" while another called "peter".

In [ ]:
q2 = interactive.execute(
    'g.V().has("name", "marko").out().where("name", "peter")).valueMap()'

for p in q2:

Degree Centrality

Degree centrality is a measure of the number of edges associated to each vertex, which is of statistical significance in large-scale data processing. Here are some examples:

In [ ]:
q3 = interactive.execute("g.V().group().by().by(bothE().count())").all()
for p in q3:

q4 = interactive.execute("g.V().group().by().by(inE().count())").all()
for p in q4:

Cycle Detection

Cycle detection is another important application of graph query in commerce area where cycles are usually considered as fraudulent patterns. Here is an example illustrating how gremlin helps detect cycles in a graph.

In [ ]:
q5 = interactive.execute(
In [ ]: