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();


}
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"}]}}
{"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]...
Jump to solution
3 Replies
danielcraig23
danielcraig23OP12mo ago
I'd like to see the result set include firstSource and firstSink, in addition to secondSource and secondSink. For context, I am attempting to break up a query that is used to sample real data from our database with the goal of using it as a test resource. I need to sample a bit of data from here and there, and as it happens the query times out before finishing. Or could I somehow aggregate multiple Graph objects into one?
Solution
spmallette
spmallette12mo ago
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]
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.
danielcraig23
danielcraig23OP12mo ago
I will try this and let you know
Want results from more Discord servers?
Add your server