Export Tinkerpop graph using writeGraph() to different OutputStream

Viewed 169

The following statement will create a new file on the graph server:

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

I want to use a different OutputStream to see the output directly in my gremlin client. I have tried DataOutputStream() but got a NullPointerException. How would I get the response from writeGraph()?

1 Answers

I originally read this question as wanting to know how to create a remote OutputStream to write from the server back to some local file on the client, which likely has some solution but I'm not sure what the answer is to that. As I happen to look at this question again though the bounty notes that you are interested in "Export of full tinkerpop graph to console" in which case perhaps a different approach will satisfy.

I would simply construct a GraphMLWriter directly using its Builder and then write to a byte[] and return that as a String:

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

Here is the full console session though I've elided some of the graphml to help with readability:

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> 

My Gremlin Console example sends scripts to the server for execution in a session (thus this approach will also work as a script sent from drivers assuming it is sent as one script or if using session each line can be sent individually). Note that the result of each line returned from the server is stored in a result variable in the console. For this to work properly it is important that you connect with a configuration like the one presented in the default remote-objects.yaml where objects are returned rather than just string representations, though in this case since the ultimate object is a String it might not matter so much. I suppose whether or not it depends on what you intend to do with the result itself once you have it.

Some caveats to consider:

  1. For a large graph this operation will be fairly expensive because not only is it a full graph scan but you're also writing the entire graph to memory before streaming it back.
  2. The use of the Graph API (i.e. graph.io()) and/or direct use of GraphWriter implementations isn't recommended, so I suppose these APIs could disappear in future versions but give that there aren't better solutions at this time there isn't much choice I'm afraid.
Related