使用 writeGraph() 将 Tinkerpop 图导出到不同的 OutputStream
Export Tinkerpop graph using writeGraph() to different OutputStream
以下语句将在图形服务器上创建一个新文件:
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(new FileOutputStream("export.graphml"), graph)
我想使用不同的 OutputStream 直接在我的 gremlin 客户端中查看输出。我试过 DataOutputStream() 但得到了 NullPointerException。我如何从 writeGraph() 获得响应?
我最初读这个问题是想知道如何创建远程 OutputStream
以从服务器写回客户端上的某个本地文件,这可能有一些解决方案,但我不确定是什么答案是这样的。当我碰巧再次看这个问题时,尽管赏金指出您对 "Export of full tinkerpop graph to console" 感兴趣,在这种情况下,也许可以采用不同的方法来满足。
我会简单地直接使用它的 Builder
构造一个 GraphMLWriter
然后写入一个 byte[]
和 return 作为 String
:
baos = new ByteArrayOutputStream()
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph)
baos.toByteArray()
这里是完整的控制台会话,尽管我省略了一些 graphml 以帮助提高可读性:
gremlin> :remote connect tinkerpop.server conf/remote-objects.yaml session
==>Configured localhost/127.0.0.1:8182-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d] - type ':remote console' to return to local mode
gremlin> baos = new ByteArrayOutputStream();[]
gremlin> graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph);[]
gremlin> new String(baos.toByteArray(), java.nio.charset.StandardCharsets.UTF_8)
==><?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="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></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="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml>
gremlin> :remote console
==>All scripts will now be evaluated locally - type ':remote console' to return to remote mode for Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> result
==>result{object=<?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="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></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="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml> class=java.lang.String}
gremlin> result.get(0).getString()
==><?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="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></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="age">29</data>
<data key="name">marko</data>
</node>
...
</graph>
</graphml>
gremlin>
我的 Gremlin 控制台示例将脚本发送到服务器以在会话中执行(因此这种方法也可以作为驱动程序发送的脚本,假设它作为一个脚本发送,或者如果使用会话,则每一行都可以单独发送) .请注意,从服务器 return 编辑的每一行的结果存储在控制台的 result
变量中。为了使其正常工作,重要的是您连接到默认 remote-objects.yaml
中显示的配置,其中对象是 returned 而不仅仅是字符串表示,尽管在这种情况下,因为最终对象是a String
可能没那么重要。我想这是否取决于您打算如何处理结果本身。
需要考虑的一些注意事项:
- 对于大图,此操作将相当昂贵,因为它不仅是全图扫描,而且在将其流回之前还要将整个图写入内存。
- 不推荐使用
Graph
API(即 graph.io()
) and/or 直接使用 GraphWriter
实现,所以我想这些APIs 可能会在未来的版本中消失,但鉴于目前没有更好的解决方案,恐怕没有太多选择。
以下语句将在图形服务器上创建一个新文件:
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(new FileOutputStream("export.graphml"), graph)
我想使用不同的 OutputStream 直接在我的 gremlin 客户端中查看输出。我试过 DataOutputStream() 但得到了 NullPointerException。我如何从 writeGraph() 获得响应?
我最初读这个问题是想知道如何创建远程 OutputStream
以从服务器写回客户端上的某个本地文件,这可能有一些解决方案,但我不确定是什么答案是这样的。当我碰巧再次看这个问题时,尽管赏金指出您对 "Export of full tinkerpop graph to console" 感兴趣,在这种情况下,也许可以采用不同的方法来满足。
我会简单地直接使用它的 Builder
构造一个 GraphMLWriter
然后写入一个 byte[]
和 return 作为 String
:
baos = new ByteArrayOutputStream()
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph)
baos.toByteArray()
这里是完整的控制台会话,尽管我省略了一些 graphml 以帮助提高可读性:
gremlin> :remote connect tinkerpop.server conf/remote-objects.yaml session
==>Configured localhost/127.0.0.1:8182-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d] - type ':remote console' to return to local mode
gremlin> baos = new ByteArrayOutputStream();[]
gremlin> graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph);[]
gremlin> new String(baos.toByteArray(), java.nio.charset.StandardCharsets.UTF_8)
==><?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="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></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="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml>
gremlin> :remote console
==>All scripts will now be evaluated locally - type ':remote console' to return to remote mode for Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> result
==>result{object=<?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="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></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="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml> class=java.lang.String}
gremlin> result.get(0).getString()
==><?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="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></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="age">29</data>
<data key="name">marko</data>
</node>
...
</graph>
</graphml>
gremlin>
我的 Gremlin 控制台示例将脚本发送到服务器以在会话中执行(因此这种方法也可以作为驱动程序发送的脚本,假设它作为一个脚本发送,或者如果使用会话,则每一行都可以单独发送) .请注意,从服务器 return 编辑的每一行的结果存储在控制台的 result
变量中。为了使其正常工作,重要的是您连接到默认 remote-objects.yaml
中显示的配置,其中对象是 returned 而不仅仅是字符串表示,尽管在这种情况下,因为最终对象是a String
可能没那么重要。我想这是否取决于您打算如何处理结果本身。
需要考虑的一些注意事项:
- 对于大图,此操作将相当昂贵,因为它不仅是全图扫描,而且在将其流回之前还要将整个图写入内存。
- 不推荐使用
Graph
API(即graph.io()
) and/or 直接使用GraphWriter
实现,所以我想这些APIs 可能会在未来的版本中消失,但鉴于目前没有更好的解决方案,恐怕没有太多选择。