问题
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