GremlinX is an attempt to implement the gremlin language on networkx.
Load a dataset of air routes Load a dataset of air routes
from gremlinx.core import (
GraphTraversalSource,
)
import networkx as nx
G = nx.read_graphml("test/data/air-routes.graphml")
g = GraphTraversalSource(graph=G)The query below will return any vertices (nodes) that have the airport label.
# Find vertices that are airports
g.V().hasLabel('airport')This query will return the vertex that represents the Dallas Fort Worth (DFW) airport.
# Find the DFW vertex
g.V().has('code','DFW')The next two queries combine the previous two into a single query.
# Combining those two previous queries (two ways that are equivalent)
g.V().hasLabel('airport').has('code','DFW')
g.V().has('airport','code','DFW')# What property values are stored in the DFW vertex?
g.V().has('airport','code','DFW').values()The values step can take parameters that tell it to only return the values for the provided key names.
g.V().has('airport','code','DFW').values('city')
g.V().has('airport','code','DFW').values('runways','icao')You can simply test to see if a property exists as well as testing for it containing a specific value.
# Find all edges that have a 'dist' property
g.E().has('dist')
# Find all vertices that have a 'region' property
g.V().has('region')
# Find all the vertices that do not have a 'region' property
g.V().hasNot('region')
# The above is shorthand for
g.V().not(has('region'))# How many airports are there in the graph?
g.V().hasLabel('airport').count()
# How many routes are there?
g.V().hasLabel('airport').outE('route').count()
# How many routes are there?
g.V().outE('route').count()
# How many routes are there?
g.E().hasLabel('route').count()# How many of each type of vertex are there?
g.V().groupCount().by(label)We can also run a similar query to find out the distribution of edge labels in the graph
# How many of each type of edge are there?
g.E().groupCount().by(label)By way of a side note, the examples above are shorthand ways of writing something like this example which also counts vertices by label.
# As above but using group()
g.V().group().by(label).by(count())# How many airports are there in each country?
g.V().hasLabel('airport').groupCount().by('country')