Gremlin Script to select multiple vertices with labels from the previous statements

993 views
Skip to first unread message

Venkata Phani Kumar Mangipudi

unread,
Mar 2, 2016, 10:58:09 AM3/2/16
to Gremlin-users
I want to do the following from gremlin script, 

> m = g.V().addV(label,'Person','name','Marko').iterator().next(); d = g.V().addV(label, 'Person','name', 'Daniel').iterator().next(); pl = g.addV(label,'Place','place_name','California').iterator().next(); m.addEdge('livesIn', pl); m.addEdge('friend', d);

With the above script, I am able to create persons Marko, Daniel, and place "California" and create edges between Marko and California AND Marko and Daniel.

I want to retrieve the vertex Ids created by the system for subsequent processing in my application. Hence, I want to extend the above script and retrieve the vertices I just created at the end of the script with some labels associated to them. say, I want the result something like: 

==>marko=v[8232]
==>daniel=v[8252]
==>place=v[4136]

When I add "inject(m, d, pl)" at the end of above script, I am getting the vertices, but not able to assign them some labels.

I tried, union, group, and steps in multiple ways but was not able to come up with the solution. Kindly help.

Mark Henderson

unread,
Mar 2, 2016, 12:18:59 PM3/2/16
to Gremlin-users
Would doing at the end of your script help?


['marco':m,'daniel':d,'place':the_variable_your_edge_is_assigne_to];

Daniel Kuppitz

unread,
Mar 2, 2016, 2:35:11 PM3/2/16
to gremli...@googlegroups.com
You can make that a single statement:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]

gremlin> g.addV(label, 'Person', 'name', 'Marko').as('marko').
gremlin>   addV(label, 'Person', 'name', 'Daniel').as('daniel').
gremlin>   addV(label, 'Place',  'name', 'California').as('place').
gremlin>   addE('livesIn').from('marko').to('place').
gremlin>   addE('friend').from('marko').to('daniel').select('marko', 'daniel', 'place')
==>[marko:v[0], daniel:v[2], place:v[4]]

However, this graph is based on wrong assumptions. Neither does Marko live in California, nor are Marko and Daniel friends ;).

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-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/123441dd-6b0c-4127-ac28-c396df050cf1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Venkata Phani Kumar Mangipudi

unread,
Mar 3, 2016, 12:10:51 AM3/3/16
to Gremlin-users
Thank you Mark, tried this and it worked great :)

Venkata Phani Kumar Mangipudi

unread,
Mar 3, 2016, 9:11:58 AM3/3/16
to Gremlin-users
Thank you very much Daniel for the insights and also the domain knowledge :) 
Next time, I'll be careful with my examples and will stick to the domain as close as possible and avoid assumptions ;)

The aliases we create are pertaining to only one statement, right. 
Do I have any additional benefit (say performance) with single statement vs multiple statements?

Thanks and Best Regards,
Phani

Daniel Kuppitz

unread,
Mar 3, 2016, 10:37:40 AM3/3/16
to gremli...@googlegroups.com
Yes, a single statement will be (marginally) faster, since there's only one traversal that needs to be compiled (when I say "compiled" I mean the process where traversal strategies get applied).

Cheers,
Daniel


Venkata Phani Kumar Mangipudi

unread,
Mar 3, 2016, 12:40:20 PM3/3/16
to Gremlin-users
Thank you very much Daniel. 

I'll use single statement for better performance, where ever I can do with good readability. 

Thanks and Best Regards,
Phani
Reply all
Reply to author
Forward
0 new messages