1

I have an in-memory TinkerGraph. I want to write a Spring Boot REST controller to expose a serialized (as GraphML) representation of this Tinkergraph. The serialization APIs (g.io) needs a String filepath to be passed to it. Currently I am having to write to a /tmp file and then read the file to get a String representation of the serialized GraphML.

Is there a way to directly get a String output of the serialized GraphML? Without having to write into a tmp file and read it back in?

g.io("graph.xml").write().iterate()
nitkart
  • 137
  • 3
  • 14

1 Answers1

2

As of the current latest release 3.4.2, I'm afraid that there is no way to do it with the Gremlin language. The reason it only writes to a file and not to something like an Java OutputStream is that the io() step is meant to be programming language agnostic. Python and other languages off the JVM have no way to construct or specify such an object so writing to file makes it work across the board. I don't know if that will change in the future, unless we came up with a reasonable API that would work intuitively across programming languages.

Since you are using an in-memory TinkerGraph you could bypass Gremlin and got back to the very old way of doing things:

Graph graph = TinkerFactory.createModern();
try (OutputStream os = new FileOutputStream("tinkerpop-modern.xml")) {
    graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
}

You would just replace the FileOutputStream with whatever kind of OutputStream you wanted to use. This approach uses the old Graph API which I think is just deprecated in newer versions, so the option should still be available to you. Note that if you are not on the JVM the only way to do return a String would be by submitting a Gremlin script to Gremlin Server.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135