Proper way to update Edges in Bulbs (neo4j or titan) -
i'm experimenting bulbs
interface graph database. ( production use titan, locally neo4j seems best experimenting ).
i can't wrap head around concept...
bulbs shows how create new vertices...
>>> james = g.vertices.create(name="james") >>> julie = g.vertices.create(name="julie") >>> g.edges.create(james, "knows", julie)
digging docs, can replace "get or create" :
>>> james = g.vertices.get_or_create('name',"james",{'name':'james')
what can't figure out, how existing edge. attempts far have ended recreating dozens of "james knows julie" relationships, instead of accessing existing 1 update.
can point me in right direction?
you can add/update edge's properties, graph databases, cannot update attributes make edge, i.e. cannot update incoming , outgoing vertex ids or label. instead, delete edge , add new one.
here different ways of getting , edge , updating properties..
you can edge id:
>>> 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) # edge again >>> assert edge == edge2 >>> edge2.someprop = "somevalue" >>> edge2.save()
you can edge properties if has , indexed:
# return iterator or none edges (may return more one) >>> edges = g.edges.index.lookup(someprop="somevalue") >>> edge = edges.next() >>> edge.someprop = "newvalue" >>> edge.save() # return 1 or none edges (or error if more 1 edge found) >>> edge = g.edges.index.get_unique(someprop="somevalue") >>> edge.someprop = "newvalue" >>> edge.save()
you can edge using gremlin traversing vertices:
>>> 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()
see 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/
but efficient way update edge when don't know id update edge via gremlin script looks (this way have 1 round trip server, not two):
>>> 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'
see https://github.com/tinkerpop/gremlin/wiki/updating-a-graph
and readability, rather writing gremlin 1 liners in python repl, put gremlin scripts in gremlin.groovy
file, shown here:
http://bulbflow.com/docs/api/bulbs/groovy/
here real-life example of using gremlin or create edge:
https://github.com/espeed/lightbulb/blob/master/lightbulb/gremlin.groovy#l88
and detailed explanation of code here:
Comments
Post a Comment