3

I am trying to get modify vertices on a remote Gremlin Server using a traversal but it seems that only in the traversal in which a vertex is created I can also add properties, when starting a new traversal I properties are not added.

Scala/Java cluster connection setup code:

val mapper = GryoMapper.build()
val cluster = Cluster.build().serializer(new GryoMessageSerializerV1d0(mapper)).create
val client = cluster.connect[org.apache.tinkerpop.gremlin.driver.Client.ClusteredClient]()
val graph = EmptyGraph.instance()
val g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "g"))

this works:

val v1 = g.addV("person").property("name","stephen").next()

this does not:

g.V(v1.id()).property("age","27")

this does not either and even throws a java.lang.IllegalStateException (propertyAdditionNotSupported) because the vertex is a org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex:

v1.property("age","27")

If I use a Gremlin Console and remote connect to the Gremlin Server I do both without any issues.

:remote connect tinkerpop.server conf/remote.yaml
gremlin> :> g.addV('person').property('name','stephen')
==>v[82128]
gremlin> :> g.V(82128).property('age','27')
==>v[82128]
gremlin> :> g.V(82128).valueMap()
==>[name:[stephen],age:[27]]

Is the Java remote implementation bugged or am I missing something?

user3508638
  • 195
  • 1
  • 7

2 Answers2

2

I'm not sure what Graph implementation you are using but this works for me with TinkerGraph:

gremlin> graph = EmptyGraph.instance()
==>emptygraph[empty]
gremlin> cluster = Cluster.open()
==>localhost/127.0.0.1:8182
gremlin>  g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "g"))
==>graphtraversalsource[emptygraph[empty], standard]
gremlin> v = g.addV().property('name','stephen').next()
==>v[0]
gremlin> g.V(v.id()).property('favorite','red')
==>v[0]
gremlin> g.V().valueMap()
==>[name:[stephen],favorite:[red]]

I will note than in your case you don't next() out the vertex from:

val v1 = g.addV("person").property("name","stephen")

I assume that even in Gremlin Scala syntax you should have to do:

val v1 = g.addV("person").property("name","stephen").next()

otherwise v1 will just be a Traversal instance and you get the id() of the Traversal and not the Vertex. So I think that should fix your problem.

Note that v1.property("age","27") will not work for the reason you explained - that vertex is "detached" and you can't work with it directly except by passing it back into another traversal. You should also be able to do this on most graphs:

gremlin> g.V(v).property('favorite','red')
==>v[0]

without referencing the id().

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thanks for your reply. I used the correct vertex-id but stripped some alternative Scala code (I edited my original question and added .next()). Your example is from within Gremlin Console I assume and that works, I can also do this in the Gremlin Console. But a remote connection from within a Scala/Java program does not allow me to modify existing vertices. I tried both TinkerGraph and JanusGraph, both are not working so I assume it is in the Java implementation.. – user3508638 Jun 20 '17 at 13:26
0

You need to make sure to iterate the traversal. Stephen's answer makes some mention of this. The Gremlin Console automatically iterates the traversal, while you must explicitly do this yourself otherwise.

  • iterate() get zero result
  • next() get one result
  • toList() get many results

This tutorial is also a good read on result iteration http://tinkerpop.apache.org/docs/current/tutorials/the-gremlin-console/#result-iteration

Jason Plurad
  • 6,682
  • 2
  • 18
  • 37