Simple Java Question - TinkerGraph

287 views
Skip to first unread message

Rahul

unread,
Dec 29, 2017, 11:08:18 AM12/29/17
to Gremlin-users
Hello 

I am able to populate the in-memory TinkerGraph using Java. Now I am trying to query the graph and print results of the query in console. But I am struggling to do simple Java operations. Most of the documentation/tutorial online gives example using Groovy and unfortunately I don't know Groove yet.

TinkerGraph g = TinkerGraph.open();
g
.createIndex("userId", Vertex.class);
GraphTraversalSource tg = g.traversal();
       
....
    populate graph
       
....
                 
System.out.println(tg.V().has("userId", "1234").values("name")); // how do I print out value of "name" ?




So values() method returns GraphTraversal which I believe is to chain subsequent queries. But how do I get actual value from GraphTraversal ? I apologize if this is too simple question to ask. 

Thanks!

Stephen Mallette

unread,
Dec 29, 2017, 11:49:50 AM12/29/17
to Gremlin-users
I think you just need to iterate your traversal:


or more simply:

System.out.println(tg.V().has("userId", "1234").values("name").next())

--
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/d3d0074c-bad8-4e5a-bde7-9152b8c3fc8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rahul

unread,
Dec 29, 2017, 1:32:25 PM12/29/17
to Gremlin-users
Thanks for the link. It helped a lot.

So after trying your suggestion, I am able to retrieve the node itself and but failed to print out 'name' value. Upon further investigation, I see that there are no property value associated to node. I am creating graph and populating property values like below from employee list  -


public void buildGraph(){
     
List<Employee> employees = .. get from source

     TinkerGraph g = TinkerGraph.open();
     g
.createIndex("userId", Vertex.class);
     
GraphTraversalSource tg = g.traversal();

 
     employees
.stream().filter(Objects::nonNull).forEach(e -> {
 
       
Vertex v1 = getOrCreate(tg, g, e.getEmpId());
        v1
.properties("name", e.getFirstName());
       
if(e.getReportsTo() != null){
           
Vertex v2 = getOrCreate(tg, g, e.getReportsTo());
            v1
.addEdge("reportsTo", v2);
       
}
     
});
 
     
Vertex it = tg.V().has("userId", "1234").next();
     
String val = it.value("name");
     
System.out.println(val);
 
 
}
 
 
private Vertex getOrCreate(GraphTraversalSource tg, Graph g, String eId){
     
GraphTraversal<Vertex, Vertex> t = tg.V().has("person", "userId", eId);
     
return t.hasNext() ? t.next() : tg.addV("person").property("userId", eId).next();
 
}






On Friday, December 29, 2017 at 10:49:50 AM UTC-6, Stephen Mallette wrote:
I think you just need to iterate your traversal:


or more simply:

System.out.println(tg.V().has("userId", "1234").values("name").next())
On Fri, Dec 29, 2017 at 11:08 AM, 'Rahul' via Gremlin-users <gremli...@googlegroups.com> wrote:
Hello 

I am able to populate the in-memory TinkerGraph using Java. Now I am trying to query the graph and print results of the query in console. But I am struggling to do simple Java operations. Most of the documentation/tutorial online gives example using Groovy and unfortunately I don't know Groove yet.

TinkerGraph g = TinkerGraph.open();
g
.createIndex("userId", Vertex.class);
GraphTraversalSource tg = g.traversal();
       
....
    populate graph
       
....
                 
System.out.println(tg.V().has("userId", "1234").values("name")); // how do I print out value of "name" ?




So values() method returns GraphTraversal which I believe is to chain subsequent queries. But how do I get actual value from GraphTraversal ? I apologize if this is too simple question to ask. 

Thanks!

--
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.

Rahul

unread,
Dec 29, 2017, 2:26:05 PM12/29/17
to Gremlin-users

Ok. So I figured out that if I am supposed to use setProperty instead of setProperties.


  v1
.properties("name", e.getFirstName());     ->        v1.property("name", e.getFirstName());
 

This works until I start adding more properties like



v1
.property("firstName", e.getFirstName()).property("lastName", e.getLastName());


Now I cannot retrieve the value of lastName from vertex.


System.out.println(v1.values("firstName").next());  // WORKS
System.out.print(" " + v1.values("lastName").next());  //THROWS java.util.NoSuchElementException



Can anyone please help me understand what's going on here?


Rahul

unread,
Dec 29, 2017, 2:36:28 PM12/29/17
to Gremlin-users
I posted too soon. :-)

The chaining of property like this doesn't work.


v1.property("firstName", e.getFirstName()).property("lastName", e.getLastName());

Changed to below and it works. 

v1.property("firstName", e.getFirstName());
v1
.property("lastName", e.getLastName());


But still don't know why first option did not work or what's the use case for chaining property method calls.

Stephen Mallette

unread,
Dec 29, 2017, 3:16:32 PM12/29/17
to Gremlin-users
do:

g.V(v1).property("firstName", e.getFirstName()).property("lastName", e.getLastName()).iterate();

"v" in your case refers to a Vertex object which is not a GraphTraversal. Chaining property() calls together aren't possible on Vertex - that is a function of the Gremlin language and GraphTraversal objects.



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/9b1a7a55-0cf6-4261-9f90-4c3905df4d3b%40googlegroups.com.

Antriksh Shah

unread,
Jan 2, 2018, 12:54:47 AM1/2/18
to Gremlin-users
Hey, If you are new to JanusGraph, I would suggest do a quick read of the following https://github.com/krlawrence/graph . This would help you immensely.

Rahul

unread,
Jan 2, 2018, 5:10:56 PM1/2/18
to Gremlin-users
Yes I have been referencing that book and it helps a lot. Thanks!

Kelvin Lawrence

unread,
Jan 2, 2018, 9:07:01 PM1/2/18
to Gremlin-users
Glad to hear it's proving useful - I did a lot more work on it over the holiday period. I plan to put out another update in all formats soon. The Asciidoc version is always fully up to date of course but not as pretty :-)

Please do let me know of any areas where it would have helped to have additional coverage or anything that was hard to find or missing. Still very much a work in progress.

Cheers
Kelvin

Rahul

unread,
Jan 4, 2018, 11:05:33 AM1/4/18
to Gremlin-users
 Thanks Kelvin for great work. Your book has now become my go-to place for Gremlin and JanusGraph ref.  

It would be helpful if there is Java equivalent codes for each Groovy Gremlin examples. You have section on how to work with Java but it's very limited. Since I want to use GraphDB from my java app, it would have helped tremendously. Just my 2 cents. 

Kudos! 

Kelvin Lawrence

unread,
Jan 4, 2018, 11:42:43 AM1/4/18
to Gremlin-users
It's on my todo list :-) Thanks for the feedback

Kelvin Lawrence

unread,
Jan 5, 2018, 11:30:03 AM1/5/18
to Gremlin-users
Prompted by your feedback I added another Java sample program to the Git repo last night and will work on the Java section of the book more. I opened an issue to remind me :-)


On Thursday, January 4, 2018 at 10:05:33 AM UTC-6, Rahul wrote:
Reply all
Reply to author
Forward
0 new messages