how to build nested property value maps using gremlin.

1,318 views
Skip to first unread message

dap...@gmail.com

unread,
Jan 24, 2018, 2:44:02 AM1/24/18
to Gremlin-users
I'm trying to run a query that will return something like:

[
 
{
   
'students':[
     
{
       
'id': 'student-1',
       
'name': 'john'
       
'subjects': [
           
{'id': 'subject-1', 'sequence': 1},
           
{'id': 'subject-2', 'sequence': 2 }
       
]
     
},
     
// more students and subjects here
   
]
 
}
]

Assuming I have 1) a vertex  with label "student" and property "name", 2) a vertex with label "subject", 3) an edge between them student-[has_subject]->subject with edge property 'sequence'. Here is what am trying:

query = g.V().hasLabel("student").match(
__.as('stu').id().as('id'),
__.as_('stu').coalesce(__.values('name'),constant('')).as('name'),
__.as_('stu').union(
__.outE('has_subject').match(
__.as('stu-rel').inV().id().as('subject_id'),
__.as_('stu-rel').coalesce(__.values('sequence'),constant('')).as('sequence')
).select('subject_id', 'sequence')
).fold().as('subjects')
).select('id', 'name', 'subjects')


This seems to work ok, but my complaint is that I can't use as('id') again inside my second match statement. Instead I had to use as('subject_id') instead.

I'm probably using the wrong step, or approaching this the wrong way, but I can't be the first person who needed nested results with property value maps where some property names might be duplicates of their parent. 

Anyone have a better solution, or insights on why this might be a dumb way to approach gremlin queries? (still trying to grok them)

Daniel Kuppitz

unread,
Jan 25, 2018, 2:51:18 PM1/25/18
to gremli...@googlegroups.com
You can use some simple projections here.

g.V().hasLabel("student").
  project("id","name","subjects").
    by(id).
    by("name").
    by(outE("has_subject").
       project("id","sequence").
         by(inV().id()).
         by("sequence").
       fold())

Cheers,
Daniel


--
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/8540e4a9-418f-4f63-927d-5f1c570baa23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dap...@gmail.com

unread,
Jan 26, 2018, 11:05:39 AM1/26/18
to Gremlin-users
Wow, thank you so much! The simplicity of this is just breathtaking 
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Blake Pavel

unread,
Apr 17, 2018, 9:12:58 PM4/17/18
to Gremlin-users
I'm doing something similar and this is the closest example I've seen to get what I'm after, but I'm having one problem with it.

I'm attempting to get the values of an incoming relationship as well as the outgoing relationships, but when I do something like
g.V().hasLabel("student").
  project
("id","name","teacher").
   
by(id).
   
by("name").
   
by(in("teaches").
       project
("name").
         
by("name").
       fold
())
I get a groovysh_parse error on the by(in()) piece.

However, if I use the out() like
g.V().hasLabel("student").
  project
("id","name","subjects").
   
by(id).
   
by("name").

   
by(out("has_subject").
       project
("level").
         
by("level").
       fold
())


it works perfectly fine.

I can't seem to figure out why there would be a syntax error for in() and not out(), but maybe I'm missing something in the traversal.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Daniel Kuppitz

unread,
Apr 17, 2018, 9:33:01 PM4/17/18
to gremli...@googlegroups.com
Sometimes Getting Started docs are really worth a look.


Cheers,
Daniel



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/4bfaeda6-9006-494f-b576-11177111e11a%40googlegroups.com.

Jean-Philippe B

unread,
Apr 18, 2018, 3:41:24 AM4/18/18
to gremli...@googlegroups.com
`in` is a reserved keyword in groovy

https://tinkerpop.apache.org/docs/current/reference/#gremlin-groovy

Excerpts from Blake Pavel's message of April 18, 2018 2:34 am:
>>> This seems to work ok, but my complaint is that I can't use *as('id') *again
>>> inside my second match statement. Instead I had to use *as('subject_id')
>>> *instead.
>>>
>>> I'm probably using the wrong step, or approaching this the wrong way, but
>>> I can't be the first person who needed nested results with property value
>>> maps where some property names might be duplicates of their parent.
>>>
>>> Anyone have a better solution, or insights on why this might be a dumb
>>> way to approach gremlin queries? (still trying to grok them)
>>>
>>> --
>>> 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 <javascript:>.
>>> <https://groups.google.com/d/msgid/gremlin-users/8540e4a9-418f-4f63-927d-5f1c570baa23%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>
> --
> 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/4bfaeda6-9006-494f-b576-11177111e11a%40googlegroups.com.

Blake Pavel

unread,
Apr 18, 2018, 12:01:31 PM4/18/18
to Gremlin-users
Sometimes Getting Started docs are really worth a look.

Yeah, I've been through them a few times. In my java app I have it statically imported, so I forgot I needed '__' when in the gremlin console. Dumb mistake.

 I realized the issue a few minutes after posting this after trying to figure it out for some time, but couldn't remove/edit due to approval process.

Thanks for your time.
Reply all
Reply to author
Forward
0 new messages