Can't create an edge between to vertices using gremlin-python.

3,064 views
Skip to first unread message

Tomasz

unread,
Oct 10, 2017, 11:19:00 AM10/10/17
to Gremlin-users
I want to create simple graph made of two vertices and edge between them.
From the documentation http://tinkerpop.apache.org/docs/current/reference one can read that it could be done as follows:

gremlin
> marko = g.V().has('name','marko').next()
gremlin
> peter = g.V().has('name','peter').next()
gremlin
> g.V(marko).addE('knows').to(peter)

My python script contains:

user_1 = g.addV('people').property('name','John').next()
user_2
= g.addV('people').property('name','Mary').next()

g
.V(user_1).addE('knows').to(user_2)

people
= g.V().valueMap().toList()
connections
= g.E().valueMap().toList()

print(people)
print(connections)

As a result I get:

[{'name': ['Mary']}, {'name': ['John']}]
[]

Vertices were added without any problem but there is no edge. How can I add an edge using python-gremlin language variant?



Robert Dale

unread,
Oct 10, 2017, 11:22:14 AM10/10/17
to gremli...@googlegroups.com

The addE() is not iterated like as was done with the addV().  Add .next()

Robert Dale

--
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/4fcf8ea5-f7e5-4731-ae48-c863f45a0838%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Kuppitz

unread,
Oct 10, 2017, 11:29:38 AM10/10/17
to gremli...@googlegroups.com
.iterate() your traversals.

g.V(user_1).addE('knows').to(user_2).iterate() // or .next() or .toList()

Cheers,
Daniel


Message has been deleted

Robert Dale

unread,
Oct 10, 2017, 11:50:50 AM10/10/17
to gremli...@googlegroups.com
Right, you need to tell it that it's a vertex.

g.V(user_1).addE('knows').to(__.V(user_2)).next()

Robert Dale

On Tue, Oct 10, 2017 at 11:48 AM, Tomasz <troj...@gmail.com> wrote:
When I add .next()
g.V(user_1).addE('knows').to(user_2).next()

 I get an error:
    "{0}: {1}".format(status_code, data["status"]["message"]))
gremlin_python
.driver.protocol.GremlinServerError: 599: Could not locate method: DefaultGraphTraversal.to([v[20560]])




On Tuesday, October 10, 2017 at 5:22:14 PM UTC+2, Robert Dale wrote:

The addE() is not iterated like as was done with the addV().  Add .next()

Robert Dale

On Tue, Oct 10, 2017 at 11:18 AM, Tomasz <troj...@gmail.com> wrote:
I want to create simple graph made of two vertices and edge between them.
From the documentation http://tinkerpop.apache.org/docs/current/reference one can read that it could be done as follows:

gremlin
> marko = g.V().has('name','marko').next()
gremlin
> peter = g.V().has('name','peter').next()
gremlin
> g.V(marko).addE('knows').to(peter)

My python script contains:

user_1 = g.addV('people').property('name','John').next()
user_2
= g.addV('people').property('name','Mary').next()

g
.V(user_1).addE('knows').to(user_2)

people
= g.V().valueMap().toList()
connections
= g.E().valueMap().toList()

print(people)
print(connections)

As a result I get:

[{'name': ['Mary']}, {'name': ['John']}]
[]

Vertices were added without any problem but there is no edge. How can I add an edge using python-gremlin language variant?



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

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

Tomasz

unread,
Oct 10, 2017, 11:52:30 AM10/10/17
to Gremlin-users
I tried to add .next() or
.iterate() and I get an error:
Traceback (most recent call last):
  File "app.py", line 15, in <module>
    g.V(user_1).addE('knows').to(user_2).iterate()
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/process/traversal.py", line 57, in iterate
    try: self.nextTraverser()
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/process/traversal.py", line 61, in nextTraverser
    self.traversal_strategies.apply_strategies(self)
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/process/traversal.py", line 353, in apply_strategies
    traversal_strategy.apply(traversal)
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/driver/remote_connection.py", line 143, in apply
    remote_traversal = self.remote_connection.submit(traversal.bytecode)
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/driver/driver_remote_connection.py", line 54, in submit
    results = result_set.all().result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 405, in result
    return self.__get_result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/driver/resultset.py", line 81, in cb
    f.result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 398, in result
    return self.__get_result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/driver/connection.py", line 77, in _receive
    self._protocol.data_received(data, self._results)
  File "/home/tomek/Projects/stage-control/gremlinpython/.venv/lib/python3.5/site-packages/gremlin_python/driver/protocol.py", line 101, in data_received

    "{0}: {1}".format(status_code, data["status"]["message"]))
gremlin_python.driver.protocol.GremlinServerError: 599: Could not locate method: DefaultGraphTraversal.to([v[24744]])
(.venv) ➜  gremlinpython




Tomasz

unread,
Oct 10, 2017, 11:59:29 AM10/10/17
to Gremlin-users
I added __.V() as you suggested and currently I don't get any error but my "edge response" is: [{}, {}, {}]
(I executed this function three times). Why I don't see any data about relations?

Robert Dale


Robert Dale


Robert Dale

Robert Dale

unread,
Oct 10, 2017, 12:01:54 PM10/10/17
to Gremlin-users

TinkerPop 3.3.0 should support auto-reattaching vertexes. That's the latest and what the docs point to on the front page which is why you're probably seeing that.  What version are you using?

Tomasz

unread,
Oct 10, 2017, 12:03:42 PM10/10/17
to Gremlin-users

I use 3.2.6 version of TinkerPop.

Robert Dale

unread,
Oct 10, 2017, 12:10:48 PM10/10/17
to Gremlin-users

I don't know why this happens in python.  Any regular users of python know?

Tomasz

unread,
Oct 10, 2017, 12:19:45 PM10/10/17
to Gremlin-users
.valueMap() maps the properties not a labels, I change line which add egde to: g.V(user_1).addE('knows').property('since','1997').to(__.V(user_2)).next() and now I get [{'since': '1997'}, {}, {}, {}]
 

Jason Plurad

unread,
Oct 10, 2017, 5:02:29 PM10/10/17
to Gremlin-users
This works in TinkerPop 3.2.6. It looks like to/from has to be a traversal.

user_1 = g.addV('people').property('name','John').next()
user_2
= g.addV('people').property('name','Mary').next()
knows_2_1 = g.V(user_2).addE('knows').to( g.V(user_1) ).next()
knows_1_2
= g.V(user_2).addE('knows').from_( g.V(user_1) ).next()
knows_a_b
= g.V(user_1).as_('a').V(user_2).as_('b').addE('knows').from_('a').to('b').next()

FWIW, your original addE() query using the vertex works in TinkerPop 3.3.0.

Tomasz

unread,
Oct 10, 2017, 5:10:35 PM10/10/17
to Gremlin-users
Thank you, Rober and Jason.

Do you know how to return label and properties not only properties?

I can't use TinkerPop 3.3.0 because I use JanusGraph and it supports 3.2.6 version.

By the way, I find Gremlin a little bit weird compared to Cypher from Neo4j but it's probably powerful.

Robert Dale

unread,
Oct 10, 2017, 5:13:30 PM10/10/17
to gremli...@googlegroups.com
valueMap(true) or valueMap(true, property, ...)


Robert Dale

--
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/f303c33c-6106-424a-bf29-c41582bb4b3d%40googlegroups.com.

Tomasz

unread,
Oct 12, 2017, 4:47:16 AM10/12/17
to Gremlin-users
When I execute g.V().hasLabel('people').valueMap(true, 'name').toList() or g.V().hasLabel('people').valueMap(true).toList() I get an error:
NameError: name 'true' is not defined

Do you know why valueMap don't recognize true paramter. I also try to use 1 insed of true but it dosn't help.



On Tuesday, October 10, 2017 at 11:13:30 PM UTC+2, Robert Dale wrote:
valueMap(true) or valueMap(true, property, ...)


Robert Dale

On Tue, Oct 10, 2017 at 5:10 PM, Tomasz <troj...@gmail.com> wrote:
Thank you, Rober and Jason.

Do you know how to return label and properties not only properties?

I can't use TinkerPop 3.3.0 because I use JanusGraph and it supports 3.2.6 version.

By the way, I find Gremlin a little bit weird compared to Cypher from Neo4j but it's probably powerful.

On Tuesday, October 10, 2017 at 11:02:29 PM UTC+2, Jason Plurad wrote:
This works in TinkerPop 3.2.6. It looks like to/from has to be a traversal.

user_1 = g.addV('people').property('name','John').next()
user_2
= g.addV('people').property('name','Mary').next()
knows_2_1 = g.V(user_2).addE('knows').to( g.V(user_1) ).next()
knows_1_2
= g.V(user_2).addE('knows').from_( g.V(user_1) ).next()
knows_a_b
= g.V(user_1).as_('a').V(user_2).as_('b').addE('knows').from_('a').to('b').next()

FWIW, your original addE() query using the vertex works in TinkerPop 3.3.0.

On Tuesday, October 10, 2017 at 12:19:45 PM UTC-4, Tomasz wrote:
.valueMap() maps the properties not a labels, I change line which add egde to: g.V(user_1).addE('knows').property('since','1997').to(__.V(user_2)).next() and now I get [{'since': '1997'}, {}, {}, {}]
 

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

Jason Plurad

unread,
Oct 12, 2017, 8:33:35 AM10/12/17
to Gremlin-users
In Python, the Boolean values are capitalized: True and False

Eric Cyliu

unread,
Dec 7, 2017, 5:55:30 PM12/7/17
to Gremlin-users
Hi,

I am also attaching the full script and error msgs.

graph = Graph()

host='localhost';port=8182

remote_conn = "ws://{}:{}/gremlin".format(host, port)

g = graph.traversal().withRemote(DriverRemoteConnection(remote_conn, 'g'))


user_3 = g.V().has('shogun_id','54320').next()

user_4 = g.V().has('content_id','2000005457791').next()

knows_4_3 = g.V(user_4).addE('read').to( g.V(user_3) ).next()



Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 70, in next

    return self.__next__()

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 43, in __next__

    self.traversal_strategies.apply_strategies(self)

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 353, in apply_strategies

    traversal_strategy.apply(traversal)

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/driver/remote_connection.py", line 143, in apply

    remote_traversal = self.remote_connection.submit(traversal.bytecode)

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 54, in submit

    results = result_set.all().result()

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/concurrent/futures/_base.py", line 429, in result

    return self.__get_result()

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/concurrent/futures/_base.py", line 381, in __get_result

    raise exception_type, self._exception, self._traceback

gremlin_python.driver.protocol.GremlinServerError: 500: Cannot modify unmodifiable vertex: v[4300]


Robert Dale

Robert Dale

unread,
Dec 8, 2017, 7:06:04 AM12/8/17
to gremli...@googlegroups.com

In sub-traversals, you need the anonymous __.V().

knows_4_3 = g.V(user_4).addE('read').to(__.V(user_3) ).next()



Robert Dale

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/b47744bb-1302-4d5b-99ec-2377b9d330cf%40googlegroups.com.

Eric Cyliu

unread,
Dec 8, 2017, 8:32:34 AM12/8/17
to Gremlin-users
Hi Robert,
Thanks for the possible answer. But the result was the same. It gave me the same error message. What does unmodifiable mean ? I

  File "<stdin>", line 1, in <module>

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 70, in next

    return self.__next__()

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 43, in __next__

    self.traversal_strategies.apply_strategies(self)

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 353, in apply_strategies

    traversal_strategy.apply(traversal)

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/driver/remote_connection.py", line 143, in apply

    remote_traversal = self.remote_connection.submit(traversal.bytecode)

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 54, in submit

    results = result_set.all().result()

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/concurrent/futures/_base.py", line 429, in result

    return self.__get_result()

  File "/Users/cliu/.virtenv/lib/python2.7/site-packages/concurrent/futures/_base.py", line 381, in __get_result

    raise exception_type, self._exception, self._traceback

gremlin_python.driver.protocol.GremlinServerError: 500: Cannot modify unmodifiable vertex: v[4300]



Robert Dale

Eric Cyliu

unread,
Dec 8, 2017, 8:58:11 AM12/8/17
to Gremlin-users
I created two new "people" and found them using g.V().has(.......).next(). And then add edge. It worked.
So I am thinking that the error might relate to indexing as shogun_id and content_id were indexed and unique. But there is nothing wrong with indexing vertices.

I am also attaching the schema.  

user_1 = g.addV('people').property('name','yu').next()

user_2 = g.addV('people').property('name','liu').next()


user_1 = g.V().has('name','xx').next()

user_2 = g.V().has('name','yy').next()

knows_1_2 = g.V(user_1).addE('read').to(__.V(user_2) ).next()


Schema

Vertex Label                   | Partitioned | Static

------------                   | ----------- | ------

people                         |       false |  false

user                           |       false |   true

article                        |       false |  false

------------                   | ----------- | ------


Edge Name                      |            Type |        Directed |     Unidirected |    Multiplicity

---------                      |            ---- |        -------- |     ----------- |    ------------

know                           |            Edge |            true |           false |           MULTI

knows                          |            Edge |            true |           false |           MULTI

refer                          |            Edge |            true |           false |           MULTI

read                           |            Edge |            true |           false |           MULTI

---------                      |            ---- |        -------- |     ----------- |    ------------


PropertyKey Name               |            Type |     Cardinality |            Data Type

----------------               |            ---- |     ----------- |            ---------

category                       |     PropertyKey |          SINGLE | class java.lang.String

name                           |     PropertyKey |          SINGLE | class java.lang.String

shogun_id                      |     PropertyKey |          SINGLE | class java.lang.String

content_id                     |     PropertyKey |          SINGLE | class java.lang.String

ts                             |     PropertyKey |          SINGLE | class java.util.Date

----------------               |            ---- |     ----------- |            ---------


Graph Index                    |      Type |          Element | Unique |       Backing |     PropertyKey |               Status

-----------                    |      ---- |          ------- | ------ |       ------- |     ----------- |               ------

index_by_shogun_id             | Composite | JanusGraphVertex |   true | internalindex |                 |                     

                               |           |                  |        |               |       shogun_id |            INSTALLED

index_by_content_id            | Composite | JanusGraphVertex |   true | internalindex |                 |                     

                               |           |                  |        |               |      content_id |            INSTALLED

----------                     |      ---- |          ------- | ------ |       ------- |     ----------- |               ------


Relation Index                 |                 Type |  Direction |   Sort Key | Sort Order |               Status

--------------                 |                 ---- |  --------- | ---------- |   -------- |               ------

--------------                 |                 ---- |  --------- | ---------- |   -------- |               ------


Robert Dale

Robert Dale

unread,
Dec 8, 2017, 5:41:23 PM12/8/17
to Gremlin-users
I'm going to guess that you're using JanusGraph.  While you've added some 'people', your query looks for vertexes of different names. I'm guessing that those queries are actually returning 'user's. Your 'user's are static which are not modifiable.

Eric Cyliu

unread,
Dec 9, 2017, 4:23:31 PM12/9/17
to Gremlin-users
Yes. You are right. I am using JanusGraph. My fault that I let you guess. I was trying to mask my name "liu" and "yu" using 'xx' and 'yy'. So the query return vertex with label 'people'. 

Even if vertices are static, it should not be problem to add edge between vertices. If it is impossible to add edges to vertices with static label, what's the point having static label? 



On Saturday, December 9, 2017 at 12:41:23 AM UTC+2, Robert Dale wrote:
I'm going to guess that you're using JanusGraph.  While you've added some 'people', your query looks for vertexes of different names. I'm guessing that those queries are actually returning 'user's. Your 'user's are static which are not modifiable.


On Friday, December 8, 2017 at 8:58:11 AM UTC-5, Eric Cyliu wrote:
I created two new "people" and found them using g.V().has(.......).next(). And then add edge. It worked.
So I am thinking that the error might relate to indexing as shogun_id and content_id were indexed and unique. But there is nothing wrong with indexing vertices.

I am also attaching the schema.  

user_1 = g.addV('people').property('name','xx').next()

user_2 = g.addV('people').property('name','yy').next()

Reply all
Reply to author
Forward
0 new messages