使用writeGraph()将Tinkerpop图形导出到不同的OutputStream。

问题描述 投票:2回答:1

以下语句将在图形服务器上创建一个新文件。

graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(new FileOutputStream("export.graphml"), graph)

我想使用一个不同的 输出流 来直接在我的gremlin客户端中查看输出。我尝试了DataOutputStream(),但得到了一个NullPointerException。我如何从writeGraph()中获取响应?

gremlin
1个回答
2
投票

我最初读这个问题是想知道如何创建一个远程的 OutputStream 来从服务器写回客户端的某个本地文件,这很可能有一些解决方案,但我不知道答案是什么。不过我碰巧又看了一下这个问题,赏金中指出你对 "将完整的tinkerpop图导出到控制台 "感兴趣,在这种情况下,也许换一种方法就能满足。

我会简单地构建一个 GraphMLWriter 直接使用其 Builder 然后写给 byte[] 并将其作为 String:

baos = new ByteArrayOutputStream()
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph)
baos.toByteArray()

这是完整的控制台会话,不过我省略了一些图文,以帮助阅读。

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控制台的例子是将脚本发送到服务器,以便在会话中执行(因此,这种方法也可以作为驱动程序发送的脚本,假设它是作为一个脚本发送的,或者如果使用会话,每行可以单独发送)。请注意,从服务器返回的每一行的结果都存储在一个 result 变量在控制台中。为了使其正常工作,重要的是你要用像默认的 remote-objects.yaml 中返回的是对象而不仅仅是字符串表示,不过在这种情况下,由于最终对象是一个 String 可能就没那么重要了。我想,是否要看你拿到结果后打算怎么处理它本身。

一些需要考虑的注意事项。

  1. 对于一个大的图形来说,这个操作会相当昂贵 因为不仅是一个完整的图形扫描 而且在流式传输之前,你还要把整个图形写到内存中去
  2. 使用 Graph API(即 graph.io()),或直接使用 GraphWriter 所以我想这些API可能会在未来的版本中消失,但鉴于目前还没有更好的解决方案,恐怕没有太多选择。
© www.soinside.com 2019 - 2024. All rights reserved.