添加多个顶点,更改一个属性值(tinkerpop3 + python GLV) [英] Adding multiple vertices, changing one property-value (tinkerpop3 + python GLV)
问题描述
我正在尝试缩小代码长度,但是我需要几个这种类型的数据实例:
I'm trying to size down my code, but I need several instances of data of this type:
g.addV('A').property('a-type','thing-x').property('a-value',1).next()
g.addV('A').property('a-type','thing-x').property('a-value',2).next()
...
g.addV('A').property('a-type','thing-x').property('a-value',n).next()
"a值"最多为n(例如50).
Up to 'a-value' being n (for instance 50).
# I tried a version of a loop I found here
# https://stackoverflow.com/questions/40907529/create-multiple-edges-having-vertex-id-number-0-to-49
#
g.inject((0..<50).toArray()).as('i').addV('a-value',select('i')).iterate()
但是我得到一个错误:
g.inject((0..<50).toArray()).as('i').addV('a-value',select('i')).iterate()
^
SyntaxError: invalid syntax
执行此操作的正确方法是什么?
What would be the proper way to do this?
在尝试建立答案的基础上,我想补充一点,对于我的情况,在for循环内调用t.iterate()
可以得到预期的结果,但是如果在循环外调用则不然,如下所述.
Having tried to build upon the answer, I'd just like to add that for my case, calling t.iterate()
within the for-loop gives the expected result, but not so if this is called outside of the loop, as mentioned below.
推荐答案
您发现的示例在Gremlin控制台内部使用,因此使用了Groovy构造.在Python中,您可以执行以下操作:
The example you found was being used inside the Gremlin console and hence using Groovy constructs. From Python you would do something like:
for i in range(1,51):
g.addV('test').property('mykey',i).iterate()
但是那样一次可以添加一个顶点,所以最好以类似的方式将它们分批写入.
However that will add the vertices one at a time so would be better to write them in small batches in a way similar to this.
t = g.addV('test').property('mykey',1)
for i in range(2,51):
t.addV('test').property('mykey',i)
t.iterate()
这篇关于添加多个顶点,更改一个属性值(tinkerpop3 + python GLV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!