How to query janusgraph in python

44 views
Skip to first unread message

Jeff Zhang

unread,
Nov 25, 2024, 11:08:10 AM11/25/24
to Gremlin-users

I'd like to query janusgraphin python. I follow the instruction here, but didn't success. What might be wrong ? Thanks

Here's my code:

from gremlin_python import statics 
from gremlin_python.structure.graph import Graph 
from gremlin_python.process.graph_traversal import __ 
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection from gremlin_python.process.anonymous_traversal import traversal 
connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g') 
g = traversal().withRemote(connection) 
g.V().count()

The following output is weird to me, even after I add some nodes.

>>> g.V().count() 
[['V'], ['count']]  

Daniel Craig

unread,
Nov 27, 2024, 12:05:05 AM11/27/24
to Gremlin-users
The .next() step will get the first result from the traversal.  Using .next(), your traversal will look like this: g.V().count().next()

Jeff Zhang

unread,
Nov 27, 2024, 12:51:16 AM11/27/24
to Gremlin-users
Thanks, next() works on count, but how to get the values of one vertex? 

Stephen Mallette

unread,
Dec 2, 2024, 7:44:50 AM12/2/24
to gremli...@googlegroups.com
you would still use something like next() or to_list() to get a vertex:

>>> g.V().limit(1).to_list()

when you ask "how to get the values of one vertex?" i assume you are questioning the fact that a vertex returned this way does not have any properties when you try to access them. You can only get that if you are using 3.7.0 or later. There are details on that here:




--
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 visit https://groups.google.com/d/msgid/gremlin-users/fa7a4347-31b0-439f-aaff-a96506e38514n%40googlegroups.com.

Josh Perryman

unread,
Dec 2, 2024, 6:43:50 PM12/2/24
to Gremlin-users
I suspect that what you want is something like this: 

g.V().has('my_id_key','my_id_value').elementMap().next()

(I'm unsure of the Python versions of those steps. Maybe element_map()?)

That will return a map (aka dictionary) of all of the properties for the vertex with the my_id_key that has a value of 'my_id_value'. 

Note that the has() step is there as a filter step in order to select just one vertex. Apply filtering as you see best.

The elementMap() step give a simplified version of the vertex's properties and usually the best starting place for getting all of the properties on a vertex. It is similar to the valueMap() step which is returns a map / dictionary of lists. 

-Josh

Reply all
Reply to author
Forward
0 new messages