正确更新灯泡边缘的方法(neo4j或titan) [英] Proper way to update Edges in Bulbs (neo4j or titan)

查看:143
本文介绍了正确更新灯泡边缘的方法(neo4j或titan)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Bulbs与图形数据库进行交互. (生产将使用Titan,本地Neo4j似乎是进行实验的最佳选择.)

I'm experimenting with Bulbs to interface with a graph database. ( Production will use Titan, locally Neo4j seems best for experimenting ).

我不能为这个概念埋头苦脑...

I can't wrap my head around this concept...

灯泡展示了如何创建新的顶点...

Bulbs shows how to create new Vertices...

>>> james = g.vertices.create(name="James")
>>> julie = g.vertices.create(name="Julie")
>>> g.edges.create(james, "knows", julie)

深入到文档中,我也可以将其替换为获取或创建":

Digging into the docs, I can replace that with a "get or create" as well :

>>> james = g.vertices.get_or_create('name',"James",{'name':'james')

我不知道的是如何获得现有Edge.到目前为止,我的尝试最终以重新创建了数十个詹姆斯知道朱莉"的关系,而不是访问现有的关系来进行更新.

What I can't figure out, is how to get an existing Edge. My attempts so far have ended up with recreating dozens of "james knows julie" relationships, instead of accessing the existing one to update.

有人可以指出我正确的方向吗?

Can someone point me in the right direction?

推荐答案

您可以添加/更新边缘的属性,但是对于图形数据库,您无法更新使其成为边缘的属性,即,您无法更新其传入和传出的属性顶点ID或标签.相反,您删除边缘并添加一个新边缘.

You can add/update an edge's properties, but with graph databases, you cannot update the attributes that make it an edge, i.e. you cannot update its incoming and outgoing vertex IDs or label. Instead, you delete the edge and add a new one.

以下是获取和边缘以及更新其属性的不同方法.

Here are the different ways of getting and edge and updating its properties..

您可以通过其ID获得优势:

You can get an edge by its ID:

>>> from bulbs.rexster import Graph
>>> g = Graph()
>>> james = g.vertices.create(name="James")
>>> julie = g.vertices.create(name="Julie")
>>> edge = g.edges.create(james, "knows", julie)
>>> edge2 = g.edges.get(edge.eid)       # get the edge again
>>> assert edge == edge2
>>> edge2.someprop = "somevalue"
>>> edge2.save()

您可以通过边缘的属性(如果有的话)来查找边缘并对其进行索引:

You can look up an edge by its properties if it has any and they are indexed:

# will return an iterator or None edges (may return more than one)
>>> edges = g.edges.index.lookup(someprop="somevalue") 
>>> edge = edges.next()
>>> edge.someprop = "newvalue"
>>> edge.save()

# will return 1 or None edges (or an error if more than 1 edge found)
>>> edge = g.edges.index.get_unique(someprop="somevalue") 
>>> edge.someprop = "newvalue"
>>> edge.save()

您也可以通过遍历Gremlin的顶点来获得边缘:

You can also get an edge using Gremlin by traversing its vertices:

>>> from bulbs.rexster import Graph
>>> g = Graph()
>>> script = "g.V('name',name1).outE(label).as('e').inV.has('name',name2).back('e')"
>>> params = dict(name1="James", label="knows", name2="Julie")
>>> edges = g.gremlin.query(script, params)
>>> edge = edges.next()
>>> edge.someprop = "newvalue"
>>> edge.save()

请参阅Gremlin回溯模式...

See the Gremlin Backtrack Pattern...

  • https://github.com/tinkerpop/gremlin/wiki
  • https://github.com/tinkerpop/gremlin/wiki/Backtrack-Pattern
  • http://bulbflow.com/docs/api/bulbs/gremlin/
  • http://bulbflow.com/docs/api/bulbs/rexster/gremlin/

但是,当您不知道边缘ID时,更新边缘的最有效方法是通过查找边缘的Gremlin脚本来更新边缘(这样,您只有一次往返服务器的往返时间,而不是两次往返):

But the most efficient way to update an edge when you don't know its ID is to update the edge via the Gremlin script that looks it up (this way you only have one round trip to the server, not two):

>>> from bulbs.rexster import Graph
>>> g = Graph()
>>> script = "g.V('name',name1).outE(label).as('e').inV.has('name',name2).back('e').sideEffect{it.someprop = someprop}"
>>> params = dict(name1="James", label="knows", name2="Julie", someprop="somevalue")
>>> edges = g.gremlin.query(script, params)
>>> edge = edges.next()
>>> edge.someprop
'somevalue'

请参见 https://github.com/tinkerpop/gremlin/wiki/更新图

出于可读性考虑,我将Gremlin脚本放置在gremlin.groovy文件中,而不是在Python REPL中编写Gremlin衬板,如下所示:

And for readability, rather than writing Gremlin one liners in the Python REPL, I would put my Gremlin scripts in a gremlin.groovy file, as shown here:

http://bulbflow.com/docs/api/bulbs/groovy/

以下是使用Gremlin来获取或创建边缘的真实示例:

Here is a real-life example of using Gremlin to get or create an edge:

https://github.com/espeed/lightbulb /blob/master/lightbulb/gremlin.groovy#L88

代码的详细说明在这里:

And a detailed explanation of the code is here:

是否存在等同于在neo4j的bulbs框架中提交

这篇关于正确更新灯泡边缘的方法(neo4j或titan)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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