Can we get output of gremlin query in json/gson format in java??

558 views
Skip to first unread message

Sumit Suman

unread,
Oct 29, 2021, 9:32:10 AM10/29/21
to Gremlin-users
Hi All, 

I am new to Gremlin, I am a backend developer in java. I just want to execute the query and get the output of the gremlin query in JSON format. I have searched a lot but I didn't get any solutions. 

The solution which is present on the internet is 

ResultSet results = client.submit(query);
CompletableFuture<List<Result>> completableFutureResults;
Result completableResults;
CompletableFuture<Map<String, Object>> completableFutureStatusAttributes;
List<Result> resultList;
Map<String, Object> statusAttributes;
try{
                completableFutureResults = results.all();
                completableFutureStatusAttributes = results.statusAttributes();
                resultList = completableFutureResults.get();
                statusAttributes = completableFutureStatusAttributes.get();
}
catch(ExecutionException | InterruptedException e){
                e.printStackTrace();
                break;
}
completableResults = result;
String stringresult = completableResults.toString();

I am looking for any support that gremlin is having with java???

Please suggest something.

Thanks
Sumit

HadoopMarc

unread,
Oct 30, 2021, 5:21:26 AM10/30/21
to Gremlin-users
Hi,

If you develop a graph system backend in java, are you sure you need Gremlin Server at all (there could be valid reasons)? If you use some TinkerPop compliant implementation as an embedded graph, you can write your gremlin query such that you get results as a java Map that you can serialize to json blindly and get the json string you want.

Marc

Op vrijdag 29 oktober 2021 om 15:32:10 UTC+2 schreef sumits...@gmail.com:

Stephen Mallette

unread,
Nov 1, 2021, 6:55:16 AM11/1/21
to gremli...@googlegroups.com
Serialization of results is transparent to the user with most drivers. In other words, you don't see the GraphSON, GraphBinary, etc when you get a result, but instead get an actual object as is natural to your programming language. We tend to find this more convenient. The binary formats are not human readable and GraphSON has types embedded which make it more complicated to parse. There are really only two ways to get a JSON result:

1. Use HTTP[1]
2. Send Gremlin scripts to the server through the driver that include something to serialize your result to a JSON string. 

I don't have an example for 2, but basically you would have to include some Jackson code in your script to do this. Perhaps you could create a global function in your server start script[2] and then call it like toJson(g.V()). This approach will tie you quite heavily to your Gremlin Server implementation of course and I think you would really need to examine the reasons for doing this instead of just working with the standard serialization model. What gap are you trying to fill in the getting an object back that is better satisfied by a custom JSON result?


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/0e606550-a104-45b0-9b46-ced1a88e9bf5n%40googlegroups.com.

Sumit Suman

unread,
Nov 8, 2021, 5:26:42 AM11/8/21
to Gremlin-users
Thanks but for the response but Sorry I was not able to get the above solutions.

I have simple requirement where I have my data loaded to gremlin graph db and using java/springboot, I need the response in JSON format.

Stephen Mallette

unread,
Nov 8, 2021, 6:36:22 AM11/8/21
to gremli...@googlegroups.com
> Thanks but for the response but Sorry I was not able to get the above solutions.

what problem did you have in trying?

> I have simple requirement where I have my data loaded to gremlin graph db and using java/springboot, I need the response in JSON format.

i don't know springboot but i know many folks manage to use it with TinkerPop. Could you please be more explicit in the gap your trying to fill by requiring JSON?

Reply all
Reply to author
Forward
0 new messages