gremlin python - 给顶点添加多个但未知数量的属性

9
我希望在顶点上添加多个属性,但一开始并不明确知道这些属性可能是什么。例如,假设要将一个人作为顶点添加到图中,我们有以下属性字典:

人1

{
    "id": 1,
    "first_name": "bob",
    "age": 25,
    "height": 177
}

也许要添加另一个顶点,一个人具有以下属性: 人物2
{
    "id": 2,
    "first_name": "joe",
    "surname": "bloggs",
    "occupation": "lawyer",
    "birthday": "12 September"
}

有没有一种方法可以将两个人添加到图形中,而不需要将属性键和值明确硬编码到 Gremlin property 函数中?
这个 链接 提供了正确方向的答案。更多有用的信息可以在 这里 找到。下面这行代码反映了提议的解决方案,按预期执行,并将一个新顶点添加到图形中。太好了。
g.addV("person").property("id", 1, "first_name", "bob", "age", 25, "height", 177).next()

但是,它只能在输入被硬编码的情况下工作。我已将属性字典转换为值元组,形式为(k1, v1, k2, v2, ..., kn, vn),但我无法以编程方式传递这些值。例如:

tup_vals = ("id", 1, "first_name", "bob", "age", 25, "height", 177)

但出于某些原因,我无法调用:
g.addV("person").property(*tup_vals).next()

上面的代码行不会抛出异常,只是无法按预期执行(即属性未被传递)。
有人知道如何以计算方式将这些属性字典传递到Gremlin属性函数吗?

更新:天真/低效的解决方案

下面提供了一种解决方案,但它是一个糟糕的解决方案,因为每次迭代它都会查询gremlin服务器。理想情况下,我希望一次添加所有属性。只有在id唯一时才能按预期工作。

g.addV("person").property('id', id).next()

for k,v in property_dictionary[id].items():
     g.V().has('id', id).property(k, v).iterate()

答案

感谢Daniel提供的答案。我已经根据gremlin_python包进行了适应(如下所示)。

这个答案的重要提示:在给定的上下文中,keysvalues 应该从Column枚举中导入 - 在源代码 这里 中。

from gremlin_python.process.graph_traversal import __
from gremlin_python.process.traversal import Column

persons = [{"id":1,"first_name":"bob","age":25,"height": 177}, {"id":2,"first_name":"joe","surname":"bloggs","occupation":"lawyer","birthday":"12 September"}]    

g.inject(persons).unfold().as_('entity').\
    addV('entity').as_('v').\
        sideEffect(__.select('entity').unfold().as_('kv').select('v').\
                   property(__.select('kv').by(Column.keys),
                            __.select('kv').by(Column.values)
                            )
                  ).iterate()

如果我将顶点ID作为属性之一,如何明确地添加顶点ID呢?相当于 g.addV("label_1").property(T.id, '12334') - Joseph
1个回答

5

您可以将您的映射/字典注入遍历中,为每个字典创建一个顶点,然后遍历所有字典/映射条目并将它们设置为属性。在Gremlin中,代码如下:

g.inject(persons).unfold().as('person').
  addV('person').as('v').
  sideEffect(select('person').unfold().as('kv').
             select('v').
               property(select('kv').by(keys), select('kv').by(values))).
  iterate()

例子:

gremlin> persons = [["id":1,"first_name":"bob","age":25,"height": 177]
......1>           ,["id":2,"first_name":"joe","surname":"bloggs",
                       "occupation":"lawyer","birthday":"12 September"]]
==>[id:1,first_name:bob,age:25,height:177]
==>[id:2,first_name:joe,surname:bloggs,occupation:lawyer,birthday:12 September]

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.inject(persons).unfold().as('person').
......1>   addV('person').as('v').
......2>   sideEffect(select('person').unfold().as('kv').
......3>              select('v').
......4>                property(select('kv').by(keys), select('kv').by(values))).
......5>    valueMap()
==>[id:[1],first_name:[bob],age:[25],height:[177]]
==>[birthday:[12 September],occupation:[lawyer],surname:[bloggs],id:[2],first_name:[joe]]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接