user3508638
user3508638

Reputation: 325

Remote Gremlin Server graph mutation with Java / Scala

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?

Upvotes: 3

Views: 989

Answers (2)

Jason Plurad
Jason Plurad

Reputation: 6792

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

Upvotes: 0

stephen mallette
stephen mallette

Reputation: 46226

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

Upvotes: 2

Related Questions