如何在Gremlin上更新几个顶点属性? [英] How to update several vertex properties on Gremlin?
问题描述
我想在一个顶点上添加几个属性.我可以做到:
I want to add several properties on a vertex. I could do:
g.v(1).firstname='Marko'
g.v(1).lastname='Rodriguez'
但是如何在单个查询中使用以下哈希值{firstname:'Marko',lastname:'Rodriguez'}添加这些属性?
But how to add these properties with the following hash {firstname:'Marko', lastname:'Rodriguez'} in a single query?
推荐答案
您可以构建一个有效的SideEffect管道.在简单的情况下,请执行以下操作:
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'}
或者,如果您只需要在一个节点上工作并拥有地图,则可以使用地图的each
方法:
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)}
或者您可以在管道中执行此操作,在该管道中您具有要设置的值的散列.再一次,我们将使用sideEffect管道.因为闭包内部有一个闭包,所以我们需要将it
的值从第一个闭包别名到其他名称(在本例中为tn
,是此节点"的缩写),因此可以在第二个闭包中访问它. :
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)}}
这将产生以下结果:
gremlin> g.v(0).map
==>lastname=Rodriguez
==>firstname=Marko
gremlin> g.v(1).map
==>lastname=Clinton
==>firstname=William
使用此方法的一个潜在陷阱是,您需要记住顶点ID是字符串,而不是整数,因此请确保正确引用它们.
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.
这篇关于如何在Gremlin上更新几个顶点属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!