Can GraphBinary be used to save a graph to file?

Can GraphBinary be used to save graph in a file. Any example is welcome.
Solution:
I think you would have to use GraphSON the way @danielcraig23 described. Or if using Java, Gryo. We simply never created GraphBinaryGraphWriter implementations because GraphBinary was built for network serialization. I suppose it could be used that way, but no one has ever really made a request to do that.
Jump to solution
5 Replies
danielcraig23
danielcraig238mo ago
I use something like this to save a subgraph to file:
Graph modernGraph = TinkerFactory.createModern();

GraphTraversalSource g = AnonymousTraversalSource.traversal().withEmbedded(modernGraph);

final Graph graph = (Graph) g.V()
.hasLabel("person")
.bothE()
.subgraph("subGraph")
.cap("subGraph")
.next();

GraphTraversalSource gts = AnonymousTraversalSource.traversal().withEmbedded(graph);
gts.io("personGraph.json").write().iterate();
Graph modernGraph = TinkerFactory.createModern();

GraphTraversalSource g = AnonymousTraversalSource.traversal().withEmbedded(modernGraph);

final Graph graph = (Graph) g.V()
.hasLabel("person")
.bothE()
.subgraph("subGraph")
.cap("subGraph")
.next();

GraphTraversalSource gts = AnonymousTraversalSource.traversal().withEmbedded(graph);
gts.io("personGraph.json").write().iterate();
danielcraig23
danielcraig238mo ago
The resulting json is quite large but it looks something like this
No description
danielcraig23
danielcraig238mo ago
If you truly want to save your whole graph, you can do a query like the following:
final Graph graph = (Graph) g.E()
.subgraph("subGraph")
.cap("subGraph")
.next();
final Graph graph = (Graph) g.E()
.subgraph("subGraph")
.cap("subGraph")
.next();
instead of the g.V().hasLabel("person").bothE() query For creating a tinkerGraph from the file, I do the following:

GraphTraversalSource g;

private static Graph buildGraph() {
BaseConfiguration configuration = new BaseConfiguration();
configuration.setProperty("gremlin.tinkerGraph.vertexIdManager", "ANY");

return TinkerGraph.open(configuration);
}

@Before
public void init() {
Graph graph = buildGraph();
g = AnonymousTraversalSource.traversal().withEmbedded(graph);
g.io("src/test/resources/testGraph2.json").read().iterate();
}

@Test
public void given_when_then_example_test() {
Long vertexCount = g.V().count().next();
Assert.assertEquals(5, vertexCount)
}

GraphTraversalSource g;

private static Graph buildGraph() {
BaseConfiguration configuration = new BaseConfiguration();
configuration.setProperty("gremlin.tinkerGraph.vertexIdManager", "ANY");

return TinkerGraph.open(configuration);
}

@Before
public void init() {
Graph graph = buildGraph();
g = AnonymousTraversalSource.traversal().withEmbedded(graph);
g.io("src/test/resources/testGraph2.json").read().iterate();
}

@Test
public void given_when_then_example_test() {
Long vertexCount = g.V().count().next();
Assert.assertEquals(5, vertexCount)
}
This is how I write Junit test cases with graph snapshots as test resources The vertexIdManager config is needed to allow Tinkergraph to mimic the rules that Neptune has for vertex ID's but maybe that part's not interesting to you
Solution
spmallette
spmallette8mo ago
I think you would have to use GraphSON the way @danielcraig23 described. Or if using Java, Gryo. We simply never created GraphBinaryGraphWriter implementations because GraphBinary was built for network serialization. I suppose it could be used that way, but no one has ever really made a request to do that.
Tanvir
Tanvir8mo ago
Thanks for the answer. Meanwhile tried using Janusgraph and saving graph locally using barkley db.