Reputation: 46816
I'd like to print a Gremlin query results in a JSON style. That is, all properties in a key: value format, one per line, and optionally, the edges too, like, "edge label" -> v[1234], or such.
In Java, when I use toString()
on a vertex, it prints what I want, only on a single line.
I've tried g.V().toString()
but that prints the same as if toString()
is not there.
Maybe some Groovy trick could be used in combination with getProperties()
?
(There's a similar question, How to print out Gremlin pipe / traversal results, but that's something different.)
Upvotes: 2
Views: 2971
Reputation: 6792
If you are using Gremlin 2.x, you could use the GraphSONUtility http://www.tinkerpop.com/docs/javadocs/blueprints/2.6.0/com/tinkerpop/blueprints/util/io/graphson/GraphSONUtility.html
\,,,/
(o o)
-----oOOo-(_)-oOOo-----
gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> v = g.addVertex()
==>v[0]
gremlin> v.setProperty('name','jason')
==>null
gremlin> v.setProperty('type','person')
==>null
gremlin> w = g.addVertex()
==>v[1]
gremlin> w.setProperty('name','ondra')
==>null
gremlin> w.setProperty('type','person')
==>null
gremlin> e = v.addEdge('chat', w)
==>e[2][0-chat->1]
gremlin> e.setProperty('datestamp','20150910')
==>null
gremlin> GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL)
==>{"name":"jason","type":"person","_id":"0","_type":"vertex"}
gremlin> GraphSONUtility.jsonFromElement(w, ['name'] as Set, GraphSONMode.NORMAL)
==>{"name":"ondra","_id":"1","_type":"vertex"}
gremlin> GraphSONUtility.jsonFromElement(e, null, GraphSONMode.NORMAL)
==>{"datestamp":"20150910","_id":"2","_type":"edge","_outV":"0","_inV":"1","_label":"chat"}
If you are trying to transform the entire graph, you can use GraphSONWriter http://www.tinkerpop.com/docs/javadocs/blueprints/2.6.0/com/tinkerpop/blueprints/util/io/graphson/GraphSONWriter.html
gremlin> os = new FileOutputStream('/tmp/graph.json')
==>java.io.FileOutputStream@7889a1ac
gremlin> GraphSONWriter.outputGraph(g, os)
==>null
gremlin> new File('/tmp/graph.json').text
==>{"mode":"NORMAL","vertices":[{"name":"jason","type":"person","_id":"0","_type":"vertex"},{"name":"ondra","type":"person","_id":"1","_type":"vertex"}],"edges":[{"datestamp":"20150910","_id":"2","_type":"edge","_outV":"0","_inV":"1","_label":"chat"}]}
Upvotes: 0