How to update several vertex properties on Gremlin?

无人久伴 提交于 2019-12-23 12:32:04

问题


I want to add several properties on a vertex. I could do:

g.v(1).firstname='Marko'
g.v(1).lastname='Rodriguez'

But how to add these properties with the following hash {firstname:'Marko', lastname:'Rodriguez'} in a single query?


回答1:


You can construct a SideEffect pipe that would work. In the simple case, do this:

g.v(1)._().sideEffect{it.firstname='Marko'; it.lastname='Rodriguez'}

Alternatively, if you need to work on just one node and have the map you can use the each method of a map:

m = [firstname:'Marko', lastname:'Rodriguez']
m.each{g.v(1).setProperty(it.key, it.value)}

Or you could do this inside of a pipe where you have a hash with values you wish to set. Once again, we'll use the sideEffect pipe. Because there is a closure inside of a closure we need to alias the value of it from the first closure to something else, in this case tn, short for "this node", so it is accessible in the second closure.:

g = new TinkerGraph()
g.addVertex()
g.addVertex()
m = ['0': [firstname: 'Marko', lastname: 'Rodriguez'], '1': [firstname: 'William', lastname: 'Clinton']]
g.V.sideEffect{tn = it; m[tn.id].each{tn.setProperty(it.key, it.value)}}

This will yield the following:

gremlin> g.v(0).map
==>lastname=Rodriguez
==>firstname=Marko
gremlin> g.v(1).map
==>lastname=Clinton
==>firstname=William

One potential gotcha with this method is that you need to remember that vertex id's are strings, not integers, so make sure to quote them appropriately.



来源:https://stackoverflow.com/questions/7677591/how-to-update-several-vertex-properties-on-gremlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!