Reputation: 800
I am having a hard time understanding the way to return back data in gremlin syntax when you have vertices that combine to create a complex object.
my syntax below will be in gremlin syntax used with gremlin.net to access cosmos db - so the graphSON returned through cosmos and then my POCO object is in C# syntax.
Say I had an example that was more like a tree, where everything was related, but I didn't want repeated data. So if I have a property - like an apt. You have the Property vertex, Room vertices, People Vertices. If I was doing a standard C# POCO, it may look like this:
public class Property{
public List<Room> Rooms {get;set;}
public List<Person> Managers {get;set;}
//additional general properties of the property - name, address, etc
}
public class Room {
public List<Person> Tenants {get;set;}
//other room properties - number, size, etc
}
public class Person{
//general properties - name, age, gender, etc
}
So I have been trying to somewhat replicate that structure in graphSON syntax, but once I get down one level, it doesn't really seem like that's what is done - at least I haven't found any examples. I was expecting to be able to potentially get the graphSON to look more like this when returned:
"property":{
"basic":{
//the property vertex
},
"managers":[ //array of managers
"manager":{
//person vertex
}
],
"rooms":[ //array of rooms
"room":{
//a room vertex
},
"tenants":[
{
"tenant":{
//person vertex
}
}
]
]
}
The one other caveat, is generally I may want certain properties returned or only parts and not the entire vertex - so most likely valueMap or something like that.
I've tried sideEffects, flatMap, maps, local in a variety of ways to see if I could get it, but it always seems to fall apart fairly quickly.
If I do a call like this:
g.V('my-key').as('property').flatmap(out('a-room')).as('r').select('property','r')
I'll get something more like this for return:
[
{
"property":{} //property vertex
"r":{}//a room vertex
},
{
"property":{} //property vertex
"r":{}//a room vertex
},
//repeat until you have all rooms
]
that causes a lot of extra data returned because I only need property info once.
g.V('my-key').as('p').local(out('a-room').fold()).as('r').unfold().local(out('a-tenant').fold()).as('t').select('p','r','t')
That causes duplicate data again and keeps everything one level down and not sub levels.
So my question is:
is the graphSON format I proposed possible?
Am I thinking in the wrong way when trying to pull back data?
Is what I'm doing uncommon with graphDBs as I've had a hard time finding this type of single to multiple relationships with multi levels to create a complex object.
Upvotes: 0
Views: 1092
Reputation: 46226
When asking questions about Gremlin it's always best to include a brief Gremlin script that can create some sample data as it makes it incredibly easy for those who answer to give you an exact/tested traversal that can solve your problem (example).
As to your question, you can definitely return data in whatever form you need. It might help to read this recipe in the TinkerPop documentation on Collections. In your case, I think you just need a nested project()
type of traversal:
g.V("my-key").
project('name','address', 'rooms')
by('name').
by('address').
by(out('a-room').
project('number','size','tenantCount')
by('number').
by('size').
by(out('a-tenant').count()).
fold())
Upvotes: 2