Search code examples
pythongremlingraphmlgremlinpython

Is there a way to get the GraphML representation of a graph from gremlinpython


I am using Janusgraph in a remote server to which I connect with a python remote client via

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
graph = Graph()
connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
g = graph.traversal().withRemote(connection)

but when I try to export the graph to a GraphML file with

g.io("graph.xml").write().iterate()

it gets saved in the remote server instead of the local client.

Is there a way to save the GraphML file locally? Thanks in advance!!!


Solution

  • I'm afraid that you can only do such a thing with a script based request at this point. You would basically send a script that writes the graph to a string of GraphML and return that as your result. This approach will only work if your server supports Groovy-based scripts. You would send a script with this structure (in your case you will of course use the graph you've defined on the server):

    gremlin> graph = TinkerFactory.createModern()
    ==>tinkergraph[vertices:6 edges:6]
    gremlin> writer = GraphMLWriter.build().create()
    ==>org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter@7baf1f5a
    gremlin> stream = new ByteArrayOutputStream()
    ==>
    gremlin> writer.writeGraph(stream, graph)
    ==>null
    gremlin> new String(stream.toByteArray())
    ==><?xml version="1.0" ?><graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd"><key id="labelV" for="node" attr.name="labelV" attr.type="string"></key><key id="name" for="node" attr.name="name" attr.type="string"></key><key id="lang" for="node" attr.name="lang" attr.type="string"></key><key id="age" for="node" attr.name="age" attr.type="int"></key><key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key><key id="weight" for="edge" attr.name="weight" attr.type="double"></key><graph id="G" edgedefault="directed"><node id="1"><data key="labelV">person</data><data key="name">marko</data><data key="age">29</data></node><node id="2"><data key="labelV">person</data><data key="name">vadas</data><data key="age">27</data></node><node id="3"><data key="labelV">software</data><data key="name">lop</data><data key="lang">java</data></node><node id="4"><data key="labelV">person</data><data key="name">josh</data><data key="age">32</data></node><node id="5"><data key="labelV">software</data><data key="name">ripple</data><data key="lang">java</data></node><node id="6"><data key="labelV">person</data><data key="name">peter</data><data key="age">35</data></node><edge id="7" source="1" target="2"><data key="labelE">knows</data><data key="weight">0.5</data></edge><edge id="8" source="1" target="4"><data key="labelE">knows</data><data key="weight">1.0</data></edge><edge id="9" source="1" target="3"><data key="labelE">created</data><data key="weight">0.4</data></edge><edge id="10" source="4" target="5"><data key="labelE">created</data><data key="weight">1.0</data></edge><edge id="11" source="4" target="3"><data key="labelE">created</data><data key="weight">0.4</data></edge><edge id="12" source="6" target="3"><data key="labelE">created</data><data key="weight">0.2</data></edge></graph></graphml>
    

    You would then get a string of GraphML back in Python which you could process as needed.