2

I want to cast a gremlinpython Graph (gremlin_python.structure.graph.Graph) into a networkx graph.

I found that with package ipython-gremlin (https://github.com/davebshow/ipython-gremlin) one can convert vertices to pandas dataframes and edges to networkx.classes.multidigraph.MultiDiGraph but the package is outdated and I can't find the code location on which they do the conversion. Is there a way to do the conversion?

Thanks!

cabo
  • 127
  • 1
  • 9

1 Answers1

3

You will probably have to write a little code to do this. I have done it before using the subgraph step to return the part of the graph I am interested in and then iterating over that creating the NetworkX vertices and edges. It's fairly straightforward. The only issue to be aware of is that the current Gremlin Python client uses the default Tornado maximum result frame size which I believe is 10mb so if your subgraph result exceeds that you will hit an exception. Here is a simple example that finds the star graph of routes from the SAF airport in the air routes data set and creates a NetworkX DiGraph from it

import networkx as nx
G = nx.DiGraph()
sg = g.V().has('code','SAF').outE().subgraph('sg').cap('sg').next()
for e in sg['@value']['edges']:
    G.add_edge(e.outV.id,e.inV.id,elabel=e.label)

print(G.nodes())
print(G.edges())

When run the results are

['44', '13', '20', '31', '8']
[('44', '13'), ('44', '20'), ('44', '31'), ('44', '8')]
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • Thank you so much! This worked like a charm! – cabo Feb 19 '21 at 21:47
  • Is there any good library similar to networkx which stores and computes in a distributed with all the features of networkx. As, I feel the current implementation is not scalable. Thanks – Srinath Thota May 31 '22 at 11:20
  • 1
    At a certain point you most likely will end up looking at something like Spark with GraphX or GraphFrames if you need to go to a more distributed model. – Kelvin Lawrence May 31 '22 at 15:09