有没有办法从 gremlinpython 获取图形的 GraphML 表示
Is there a way to get the GraphML representation of a graph from gremlinpython
我在远程服务器上使用 Janusgraph,我通过
与 python 远程客户端连接
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)
但是当我尝试使用
将图形导出到 GraphML 文件时
g.io("graph.xml").write().iterate()
它被保存在远程服务器而不是本地客户端。
有没有办法在本地保存 GraphML 文件?
提前致谢!!!
恐怕你现在只能用基于脚本的请求来做这样的事情。您基本上会发送一个脚本,将图形写入一个 GraphML 字符串,然后 return 作为您的结果。此方法仅在您的服务器支持基于 Groovy 的脚本时才有效。您将发送具有此结构的脚本(在您的情况下,您当然会使用您在服务器上定义的 graph
):
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>
然后您会在 Python 中得到一串 GraphML,您可以根据需要对其进行处理。
我在远程服务器上使用 Janusgraph,我通过
与 python 远程客户端连接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)
但是当我尝试使用
将图形导出到 GraphML 文件时g.io("graph.xml").write().iterate()
它被保存在远程服务器而不是本地客户端。
有没有办法在本地保存 GraphML 文件? 提前致谢!!!
恐怕你现在只能用基于脚本的请求来做这样的事情。您基本上会发送一个脚本,将图形写入一个 GraphML 字符串,然后 return 作为您的结果。此方法仅在您的服务器支持基于 Groovy 的脚本时才有效。您将发送具有此结构的脚本(在您的情况下,您当然会使用您在服务器上定义的 graph
):
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>
然后您会在 Python 中得到一串 GraphML,您可以根据需要对其进行处理。