Gremlin-Python How to access properties of vertices

1,423 views
Skip to first unread message

Yuxing Han

unread,
Nov 10, 2019, 12:42:57 PM11/10/19
to Gremlin-users
I know how to access properties of vertices in java, like the following code:

ResultSet results = this.getClient().submit("g.V()");
for (Result result : results) {
   Vertex vertex = result.getVertex();
   CompositeId id = (CompositeId) vertex.id();
   Iterator<VertexProperty<Object>> propIter = vertex.properties();
   System.out.println("vertex id " + id.toString());
   while (propIter.hasNext()) {
       VertexProperty<Object> prop = propIter.next();
       System.out.println("property key=" + prop.key() + " value=" + prop.value());
   }
}

However, I don't know how to do the same thing with Python. Because the Vertex structure
in gremlin-python doesn't have *properties* method.

Could someone kindly help me figure out this ?

Kelvin Lawrence

unread,
Nov 10, 2019, 4:57:01 PM11/10/19
to Gremlin-users
The Python client returns a "reference vertex" that just includes the ID and the Label. You should explicitly request the fields that you need. Here are some examples

>>> a = g.V('3').next()
>>> a.label
'airport'
>>> a.id
'3'
>>> p = g.V('3').properties().toList()
>>> print(p)
[vp[code->AUS], vp[type->airport], vp[desc->Austin Bergstrom Int], vp[country->US], vp[longest->12250], vp[city->Austin], vp[lon->-97.6698989868164], vp[elev->542], vp[icao->KAUS], vp[region->US-TX], vp[runways->2], vp[lat->30.1944999694824]]
>>> vm = g.V('3').valueMap().next()
>>> print(vm)
{'country': ['US'], 'code': ['AUS'], 'longest': [12250], 'city': ['Austin'], 'elev': [542], 'icao': ['KAUS'], 'lon': [-97.6698989868164], 'runways': [2], 'type': ['airport'], 'region': ['US-TX'], 'lat': [30.1944999694824], 'desc': ['Austin Bergstrom International Airport']}
>>> g.V('3').valueMap('runways').next()
{'runways': [2]}


Stephen Mallette

unread,
Nov 11, 2019, 6:37:41 AM11/11/19
to gremli...@googlegroups.com
You can find further information on the topic here:


also, note that we now have elementMap() step which makes getting properties and structure a lot nicer/easier:




 

--
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/c3b2fc69-6d23-4842-b522-a4ef8c9c9349%40googlegroups.com.

Yuxing Han

unread,
Nov 27, 2019, 8:01:43 AM11/27/19
to Gremlin-users
Thanks for your reply.

According to the first document, script-based requests always return "detached elements" and bytecode-based requests always return "reference elements". My new question is that could we change these default behaviours? For example, what if I want to send bytecode-based requests and get "detached elements"?

I think we should make some changes in the server.yaml, i.e, the settings of gremlin server. But I don't know how. Could you kindly provide some advice?
 

在 2019年11月11日星期一 UTC+8下午7:37:41,Stephen Mallette写道:
To unsubscribe from this group and stop receiving emails from it, send an email to gremli...@googlegroups.com.

Stephen Mallette

unread,
Dec 2, 2019, 8:09:17 AM12/2/19
to gremli...@googlegroups.com
>  For example, what if I want to send bytecode-based requests and get "detached elements"?

Even if you configured it that way, it wouldn't matter. The properties would remain unavailable because gremlinpython (and all GLVs) do not have full Graph Structure implementations (i.e. no properties to deserialize "detached" data into). They only have reference implementations. As to whether we can change this behavior or not to make such things possible is a much bigger question that keeps coming up despite our reaching consensus on our current position on this some time ago. While the details of that consensus are available on the dev mailing list they may be difficult to find and piece together. I may write a fresh post to the dev list (or perhaps even add a section to the dev docs) that outlines this issue clearly so that it can be referenced as a link for folks who wish to be more informed on the topic. 

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/98253f64-ee02-4b09-9eec-ea0967be849d%40googlegroups.com.

Yuxing Han

unread,
Dec 2, 2019, 9:35:14 PM12/2/19
to gremli...@googlegroups.com
Thanks a lot. I will look forward to this post. :)
Stephen Mallette <spmal...@gmail.com> 于2019年12月2日周一 下午9:09写道:


--

-- 

Regards,

Yuxing Han

 

Email: yuxi...@gmail.com

Address: East China Normal University, Shanghai, China 200062

Stephen Mallette

unread,
Dec 3, 2019, 12:53:15 PM12/3/19
to gremli...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages