python gremlin add Vertex - addV() - how to.

3,657 views
Skip to first unread message

jbda...@gmail.com

unread,
May 9, 2018, 9:49:23 AM5/9/18
to Gremlin-users
Hi,

I try to create Vertex with the python variant. I "translate" a simple Gremlin script to python and unfortunately it doesn't work.

Gremlin console :
graph = TinkerGraph.open()
traversal = graph.traversal()
graph.addVertex(T.label, "person", T.id, 1, "name", "marko", "age", 29)
traversal.V().values()
The output:
  ==>marko
  ==>29


Python code :
<import...>
graph = Graph()
traversal = graph.traversal().withRemote(DriverRemoteConnection('ws://192.168.56.103:8182/gremlin','g'))
traversal.addV(T.label, "person", T.id, 1, "name", "marko", "age", 29)
print(traversal.V().toList())

The output:
  []
Graph remains empty.

I'm surprise that the addV() python method come from the traversal object and not from graph as Gremlin native.
Can someone help me and correct my python code to do the same as Gremlin code ?

Stephen Mallette

unread,
May 9, 2018, 9:51:05 AM5/9/18
to Gremlin-users

--
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/256ea19d-03f8-4078-9704-8a0dfa4b676b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen Mallette

unread,
May 9, 2018, 9:52:58 AM5/9/18
to Gremlin-users
Also, in your Gremlin Console code, you should avoid use of Graph.addVertex() and prefer addV(). 

Thomas Wood

unread,
Oct 12, 2018, 2:26:04 PM10/12/18
to Gremlin-users
This is all good advice, but it doesn't really touch on why addV isn't working through the gremlin python API.

I'm having the same problem. Any idea how to resolve the python issue?
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Stephen Mallette

unread,
Oct 12, 2018, 2:37:39 PM10/12/18
to gremli...@googlegroups.com
I don't think that you followed my answer and now that I look again, I don't think that my answer was complete - bah. This line:

traversal.addV(T.label, "person", T.id, 1, "name", "marko", "age", 29)  

calls a long deprecated version of addV()...depending on the graph you're using and what it supports that might yield weird results. It should be more like:

traversal.addV('person').property(id,1).property('name','marko').property('age',29)

so...that was the part that wasn't complete and I dont necessarily think was the reason things were bad. i think the reason the graph remained unchanged was because the traversal does not get iterated so it never executes...it just adds the addV() step to the "traversal" variable...that's all that happens. You have to iterate() the traversal in some way:


thus written more nicely:

graph = Graph()
g = graph.traversal().withRemote(DriverRemoteConnection('ws://192.168.56.103:8182/gremlin','g'))
g.addV('person').property(id,1).property('name','marko').property('age',29).iterate()
print(g.V().toList())

i hope that is more clear.
 

Thomas Wood

unread,
Oct 12, 2018, 2:41:18 PM10/12/18
to Gremlin-users
That is extremely clear. Thank you so much. I had been trying it with the new method like you showed but with .next() instead of .iterate() with little success. This code you shared does the trick.

Thank you again.

Stephen Mallette

unread,
Oct 12, 2018, 2:47:36 PM10/12/18
to gremli...@googlegroups.com
next() should work but i suppose it depends on the number of mutations that are being executed as part of the stream. iterate() is typically more safe when generating side-effects. I think iterate() also applies a none() step to the end of the traversal which auto-filters out all the results so that nothing gets sent back over the wire. Obviously that should be faster because it saves derser and network costs.

Florian Hockmann

unread,
Oct 15, 2018, 3:57:07 AM10/15/18
to Gremlin-users
I think iterate() also applies a none() step to the end of the traversal which auto-filters out all the results so that nothing gets sent back over the wire.

I always wondered whether we should do something like that, also for the hasNext() step which could be really expensive for a traversal like g.V().hasNext() coming from a GLV as the server only gets the g.V() part and therefore tries to respond with all vertices in the graph (or am I missing something here?).
However, I don't know of any optimizations like that right now in the GLVs and I also don't see anything in that direction for gremlin-python's iterate() step.
Maybe we have to simply add the termination step itself to the Bytecode we're sending over the wire?

Stephen Mallette

unread,
Oct 15, 2018, 6:47:04 AM10/15/18
to gremli...@googlegroups.com
yeah - that's none of that is good. i guess only Java uses the none() step on iterate() and hasNext() is grossly under-optimized in all remote traversals. that's really bad.................


That one should be a priority after release this week.

Josh Braun

unread,
Apr 8, 2019, 5:24:41 PM4/8/19
to Gremlin-users
Hi,

I have been trying to get your example code below to work for a while now and can't quite get it. 

g.addV('person').property(id,1).property('name','marko').property('age',29).iterate()


The trouble seems to be with the id part. When I run this code, I get the following error.

TypeError: Object of type builtin_function_or_method is not JSON serializable

I have tried looking into other functions to set the id property of the vertex, but the id() function, for example, is meant to return the vertex's id, not set it.

Any ideas of how to overcome this error? It would be very helpful to set the vertex's id property for fast, indexed traversals.

Thanks,
Josh
To unsubscribe from this group and stop receiving emails from it, send an email to gremli...@googlegroups.com.

Michael Tolson

unread,
Apr 8, 2019, 5:29:16 PM4/8/19
to gremli...@googlegroups.com
Hi,
‘id’ is a python <built-in function id>
Michael 


gremlin-grp

unread,
Apr 8, 2019, 5:29:33 PM4/8/19
to Gremlin-users
id needs to be a string not an integer try `...property(id,'1')...`

Josh Braun

unread,
Apr 8, 2019, 5:34:04 PM4/8/19
to Gremlin-users
Just tried '...property(id,'1')...' but it is giving me the same error. I think it has something to do with the 'id' part. I found something about using T.id instead, where T is an enum from gremlin_python.process.traversal. When I try '...property(T.id,'1')..." it gives me this error:

gremlin_python.driver.protocol.GremlinServerError: 500: Vertex does not support user supplied identifiers

I found this article (https://github.com/JanusGraph/janusgraph/issues/1274) but haven't quite gotten it yet.

Josh Braun

unread,
Apr 8, 2019, 5:56:18 PM4/8/19
to Gremlin-users
I found the following in relation to setting the id of vertices on the graph.

Screen Shot 2019-04-08 at 4.52.35 PM.png


What I am really concerned about is the speed of looking up vertices by an id I have given them. My graph has millions of vertices. Do I need to set the id property for very fast traversals, or is there a way to index that would give similar performance?

Stephen Mallette

unread,
Apr 9, 2019, 8:26:23 AM4/9/19
to gremli...@googlegroups.com
Just to clarify the answer to this problem generally - most graph don't allow you to assign your own identifier. Typically, you just create your own id property key and index it. If a graph allows you to easily and directly assign ids with property(T.id, xxx) then it might make sense to do that. As for JanusGraph, you might want to post questions on specifics related to that graph system on their user list but typically your own identifier with a composite index should suffice for "fast".

Reply all
Reply to author
Forward
0 new messages