gremlin python-向顶点添加多个但数量未知的属性 [英] gremlin python - add multiple but an unknown number of properties to a vertex

查看:106
本文介绍了gremlin python-向顶点添加多个但数量未知的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add more than one property to a vertex, but from the outset do not explicitly know what those properties might be. For example, say for one person to be added as vertex to the graph, we have the following property dictionary:

人员1

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

也许要添加另一个顶点,一个人具有以下属性:

Maybe for another vertex to be added, a person has the following properties:

人员2

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

是否可以在不显式地将属性键和值硬编码到Gremlin 属性函数中的情况下,将两个人都添加到图中?

Is there a way to add both people into the graph without explicitly hardcoding the property keys and values into the Gremlin property function?

链接提供了正确方向的答案.可以找到更多有用的信息这里.下一行反映了建议的解决方案,按预期执行,并向图中添加了一个新顶点.伟大的.

This link provides an answer in the right direction. Further useful information can be found here. The following line mirrors the proposed solution, executes as expected, and adds a new vertex into the graph. Great.

g.addV("person").property("id", 1, "first_name", "bob", "age", 25, "height", 177).next()

但是,仅当输入经过硬编码时,它才会尝试工作.我已经将属性字典转换为(k1,v1,k2,v2,...,kn,vn)形式的值元组,但是无法以编程方式传递值.例如

BUT, it only seeks to work if the inputs are hardcoded. I've transformed the property dictionaries to a tuple of values in the form (k1, v1, k2, v2, ..., kn, vn), but I'm unable to programmatically pass in the values. E.g.

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

但是无论出于什么原因,我都不能打电话给

But for whatever reason, I cannot call:

g.addV("person").property(*tup_vals).next()

上面的行没有引发异常,只是没有按预期执行(即未传递属性)

The above line doesn't throw an Exception, it just doesn't execute as expected (i.e. the properties are not being passed in)

有人对如何将这些属性字典以计算方式传递给Gremlin属性函数有任何见识吗?

Does anyone have any insight on how to pass these property dictionaries into the Gremlin property function in a computational way?

更新:幼稚/效率低下的解决方案

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

A solution is provided below, but it's a bad solution because each iteration it queries the gremlin server. Ideally I want to add all properties at the same time. And only really works as expected if the id is unique.

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软件包.

Thanks to Daniel for the answer. I've adapted his answer (below) to conform with the gremlin_python package.

此答案的重要提示:<给定上下文中的c0>和values应该从列枚举中导入-在源代码中

Important note from this answer: 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()

推荐答案

您可以将地图/字典注入到遍历中,为每个字典创建一个顶点,然后遍历所有字典/地图条目并将它们设置为属性.在Gremlin中,看起来像这样:

You can inject your maps/dictionaries into the traversal, create a vertex for each dictionary and then go through all dictionary/map-entries and set them as properties. In Gremlin that looks like this:

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]]

这篇关于gremlin python-向顶点添加多个但数量未知的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆