dump gremlin-python query to json

2,059 views
Skip to first unread message

Tomasz

unread,
Oct 10, 2017, 1:45:27 PM10/10/17
to Gremlin-users
I have a graph which has 3 vertices (people, name respectively: John, Mary, Jane) and one edge (relation: 'knows'). John -knows-> Mary, John -knows->Jane.

I want to get JSON similar to that:

{ "name": "John",
 
"knows":
   
{
   
"name": "Jane",
   
"name": "Mary"
   
}
}

My query and my result are respectively: result =
g.V().as_('u').valueMap('name').as_('user').select('u').out('knows').valueMap('name').group().by(__.select('user')).toList()

[{'{name=[John]}': [{'name': ['Jane']}, {'name': ['Mary']}]}]


Is there exist any automatic or semi-automatic way to convert gremlin-python query result into JSON?

Jason Plurad

unread,
Oct 10, 2017, 11:36:14 PM10/10/17
to Gremlin-users
Here's one approach:

# build the graph
john
= g.addV('person').property('name', 'john').next()
mary
= g.addV('person').property('name', 'mary').next()
jane
= g.addV('person').property('name', 'jane').next()
g
.V(john).addE('knows').to( g.V(mary) ).next()
g
.V(john).addE('knows').to( g.V(jane) ).next()

# build a dict
v
= john
rel
= 'knows'
d1
= { 'name': g.V(v).values('name').next(), rel: g.V(v).out(rel).values('name').toList() }
d2
= { 'name': g.V(v).values('name').next(), rel: g.V(v).out(rel).map(lambda: ("['name': it.get().value('name')]", "gremlin-groovy")).toList() }

# serialize as json
import json

json
.dumps(d1)
# '{"knows": ["mary", "jane"], "name": "john"}'

json.dumps(d2)
# '{"knows": [{"name": "mary"}, {"name": "jane"}], "name": "john"}'

Tomasz

unread,
Oct 11, 2017, 3:37:20 AM10/11/17
to Gremlin-users
I'm surprised that there is no "build in" tool which returns JSON from the gremlin query.
For what purpose do most people use TinkerPop? Do they want to analyze data? I ask because I want to build REST API based on JanusGraph and I thought that returning JSON will be easier.
Do you know if there is a tool to convert gremlin query to JSON? Do community need something like this? I can write it as soon as I will feel more comfortable with the gremlin language.
 

Tomasz

unread,
Oct 11, 2017, 5:22:08 AM10/11/17
to Gremlin-users
I found here: http://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#_graphson, graphSON which looks exactly like the feature I talk about.  Do anybody knows how can I achieve this kind of result from gremlin query using graphSON in python?

Stephen Mallette

unread,
Oct 11, 2017, 7:43:22 AM10/11/17
to Gremlin-users
I'm surprised that there is no "build in" tool which returns JSON from the gremlin query.

Raw GraphSON/JSON is hidden from you with when you use the Traversal API of gremlin-python. That was a wonderful improvement because it:allowed non JVM languages like Python to have the much more similar behavior to the Java/Groovy/Scala/JVM versions of Gremlin. Without that your experience was limited to:

client = client.Client('ws://localhost:8182/gremlin', 'g')
result_set = client.submit("g.V()")  
future_results = result_set.all()  
results = future_results.result()

in which case you would be working with strings of Gremlin and GraphSON results. It is a more error prone and less maintainable way to write your code.

 I ask because I want to build REST API based on JanusGraph and I thought that returning JSON will be easier. 

Gremlin is not just an analysis tool. It is used for building applications and Gremlin can surely be used as part of development of a REST API.

Do you know if there is a tool to convert gremlin query to JSON?

You've already become familiar with GraphSON as  way to do this - you can see the serialization code for GraphSON in python here:


I don't know your use case, so I could be wrong, but it seems a bit strange to use GraphSON for your REST API. Your REST API is presumably something specific to your domain (not to graphs) therefore your REST APIs should have it's own format that makes sense to it's users. Getting back a "reference vertex" in a REST API related to the auto industry (or whatever your domain is) doesn't seem like the right way to go. Most REST APIs that I've seen developed with Gremlin over the years don't return vertices/edges/properties which is the core of what GraphSON is about. 



On Wed, Oct 11, 2017 at 5:22 AM, Tomasz <troj...@gmail.com> wrote:
I found here: http://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#_graphson, graphSON which looks exactly like the feature I talk about.  Do anybody knows how can I achieve this kind of result from gremlin query using graphSON in python?

--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/08a7c1ee-a6c8-4b2b-9459-4bb7804acb1a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tomasz

unread,
Oct 11, 2017, 9:03:26 AM10/11/17
to Gremlin-users
Image situation in which I have data(e.g. vertices): people, project, and role (in the project). I want to return via REST API information about all people (with their "properties" e.g. email, address etc.) which are managers in projects which take place between two dates.
Previously I started to develop this kind of project in Neo4j and there I prepare a query to ask DB engine about what I want (it was exactly what I want to return via API) dump it to JSON and return. I thought that I can do the same with Gremlin.
Do you think it is not good aproche? How works REST API that you have seen over the years?  

Stephen Mallette

unread,
Oct 11, 2017, 9:45:21 AM10/11/17
to Gremlin-users
It's not wrong to return JSON from your REST API, but if your REST API is about people/projects/roles then that has nothing to do with with vertices/edges/properties - I would abstract away those concepts from the users of the API. You might have a person vertex but the shape of a person vertex as JSON is different than the shape of a person entity in your REST API. Just consider the hierarchy of a Vertex:


I don't think that trying to return that format from g.V().has('person','name','marko') in your REST API is useful to the APIs users. While that output contains all the data about person (as a Vertex), I think that from a REST API perspective you want something more meaningful to the domain you are writing this API for. As an example, you already mentioned that you want to return relationships to other entities tied to that person. The above vertex is from The Crew toy graph...consider the case of a REST API based on that and you wanted to return a person and their skills - you devise this bit of Gremlin:

gremlin> g.V().has('person','name','marko').project('person','software').by().by(out('uses').fold())
==>[person:v[1],software:[v[11],v[10]]]

pass that through the GraphSON mapper and you get:


Your data is all there but none of that looks like a "Person Entity". Someone not familiar with Graphs/Vertices/Edges using your API wouldn't have an easy time working with that. It also suffers from retrieving all properties when you might not need them. You wouldn't do a SELECT * FROM table in SQL - the same concepts apply here - only get the data you need. I think a REST API is better designed with entities that stand on their own without "graph/vertex/edge" influence. All that means is writing your Gremlin differently:

gremlin>  g.V().has('person','name','marko').
......1>    project('name','location','skills').
......2>      by('name').
......3>      by(values('location').fold()).
......4>      by(out('uses').values('name').fold())
==>[name:marko,location:[san diego,santa cruz,brussels,santa fe],skills:[tinkergraph,gremlin]]

This JSON is so simple I can even paste it here without formatting:

{"name":"marko","location":["san diego","santa cruz","brussels","santa fe"],"skills":["tinkergraph","gremlin"]}

and you don't need a GraphSON toolkit to serialize simple lists/maps. The code Jason Plurad presented shows how simple it is in python to do that. Anyway, I hope that helps in some way. 







--
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-users+unsubscribe@googlegroups.com.

Tomasz

unread,
Oct 11, 2017, 10:18:21 AM10/11/17
to Gremlin-users
Thank you, Stephen. I thought that result from the gremlin query looks more or less like Facebook GraphQL query. For example, I ask for all people's properties which meet certain criteria (e.g. have projects). I expect to get (from gremlin query) only people's/project's properties and relations between them (people and projects). I see that query return much more information and that's the problem to simply dump it to the JSON object.


Stephen Mallette

unread,
Oct 11, 2017, 10:28:01 AM10/11/17
to Gremlin-users
Gremlin queries only return what you ask of them, but you can inadvertently ask for more than what you want. Again, the point is to write your traversals in your REST API with greater specificity to include only the data you want and in a form most useful to your users. Good luck!

On Wed, Oct 11, 2017 at 10:18 AM, Tomasz <troj...@gmail.com> wrote:
Thank you, Stephen. I thought that result from the gremlin query looks more or less like Facebook GraphQL query. For example, I ask for all people's properties which meet certain criteria (e.g. have projects). I expect to get (from gremlin query) only people's/project's properties and relations between them (people and projects). I see that query return much more information and that's the problem to simply dump it to the JSON object.


--
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-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages