How can I use the .io("filename.json").write() pattern to append to an existing graphson file?

I have read about defining a custom GraphWriter using the builder, but wanted to ask first before spending more time

Here is my example code:
 private static Graph buildGraph() {
        BaseConfiguration configuration = new BaseConfiguration();
        configuration.setProperty("gremlin.tinkerGraph.vertexIdManager", "ANY");

        return TinkerGraph.open(configuration);
    }

    @Test
    public void test() {
        Graph graph = buildGraph();
        GraphTraversalSource g;
        g = AnonymousTraversalSource.traversal().withEmbedded(graph);

        g.addV("source").property(T.id, "firstSource")
                .addV("sink").property(T.id, "firstSink")
                .addE("edge").property(T.id, "firstEdge").from(__.V("firstSource")).to(__.V("firstSink"))
                .iterate();

        g.addV("source").property(T.id, "secondSource")
                .addV("sink").property(T.id, "secondSink")
                .addE("edge").property(T.id, "secondEdge").from(__.V("secondSource")).to(__.V("secondSink"))
                .iterate();

        final Graph firstResult = (Graph) g.E("firstEdge").subgraph("subgraph").cap("subgraph").next();

        GraphTraversalSource firstGts = AnonymousTraversalSource.traversal().withEmbedded(firstResult);
        firstGts.io("src/test/resources/example.json").write().iterate();

        // Here the initial contents of the file are overwritten

        final Graph secondResult = (Graph) g.E("secondEdge").subgraph("subgraph").cap("subgraph").next();

        GraphTraversalSource secondGts = AnonymousTraversalSource.traversal().withEmbedded(secondResult);
        secondGts.io("src/test/resources/example.json").write().iterate();


    }


, which gets results as follows:
{"id":"secondSink","label":"sink","inE":{"edge":[{"id":"secondEdge","outV":"secondSource"}]}}
{"id":"secondSource","label":"source","outE":{"edge":[{"id":"secondEdge","inV":"secondSink"}]}}
Solution
The subgraph is stored as a side-effect, so you could use withSideEffect() to give it your own graph instance and then reuse that across multiple traversals to subgraph:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> sub = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g.withSideEffect('sg',sub).E(11).subgraph('sg').iterate()
gremlin> g.withSideEffect('sg',sub).E(12).subgraph('sg').iterate()
gremlin> sub
==>tinkergraph[vertices:3 edges:2]
gremlin> sg = sub.traversal()
==>graphtraversalsource[tinkergraph[vertices:3 edges:2], standard]
gremlin> sg.E()
==>e[11][4-created->3]
==>e[12][6-created->3]

I assume this will work on Neptune with Java, but I'm not completely sure. If it doesn't work I guess there's ways to merge the two subgraphs to one and then write that out, but I think this is the canonical answer for TinkerPop. Please let us know if it works for you.
Was this page helpful?