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

gremlin python - add multiple but an unknown number of properties to a vertex

我想向一个顶点添加多个 属性,但从一开始就没有明确知道这些属性可能是什么。例如,假设要将一个人作为顶点添加到图中,我们有以下 属性 字典:

人 1

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

也许再补充一个顶点,一个人有以下属性:

人 2

{
    "id": 2,
    "first_name": "joe",
    "surname": "bloggs",
    "occupation": "lawyer",
    "birthday": "12 September"
}

有没有一种方法可以将两个人都添加到图中,而无需将 属性 键和值明确硬编码到 Gremlin 属性 函数中?

这个link provides an answer in the right direction. Further useful information can be found 。以下行反映了建议的解决方案,按预期执行,并在图中添加了一个新顶点。伟大的。

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 属性 函数?


更新:naive/inefficient解决方案

下面提供了一个解决方案,但它是一个糟糕的解决方案,因为它每次迭代都会查询 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()

回答

感谢丹尼尔的回答。我已经修改了他的答案(如下)以符合 gremlin_python 包。

: keys and values in the given context should import from the Column enum - in the source code here

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()

您可以将 maps/dictionaries 注入遍历,为每个字典创建一个顶点,然后遍历所有 dictionary/map-entries 并将它们设置为属性。在 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]]