How to update a property of a specific edge using Gremlin / Titan / TinkerPop3?

purpose

I have a fairly simple task: to set the weight of a certain edge property. Take this scenario as an example:

Graph structure example

I would like to update the value weight.

Additional requirements

  • If an edge does not exist, it must be created.
  • Between two nodes no more than one edge of the same type can exist (that is, between Joey and Pizza there cannot be several "vote_for" ribs type"there are".
  • The task should be solved using the Java Titan API (which includes Gremlin as part of TinkerPop 3 ).

What i know

I have the following information:

  • Vertex is marked as "user"
  • Edge votes_for
  • edge type ( "" )
  • name "" ( "" ), , Vertex.

, - :

  • Joey
  • ( 1) votes_for, type "" , "" name "".
  • weight .

:

//vertex is Joey in this case
g.V(vertex.id())
    .outE("votes_for")
    .has("type", "eat")
    //... how do I filter by .outV so that I can check for "pizza"?
    .property(Cardinality.single, "weight", 0.99);
    //... what do I do when the edge doesn't exist?

, . ? - / , ? vote_for label + type, vote_for_eat?

!

+4
3

. .

, , , , .

g.V(vertex.id()).
  outE("votes_for").has("type", "eat").as("e").
  inV().has("name", "pizza").
  select("e").property("weight", 0.99d).
  iterate()

Gremlin:

gremlin> Titan.version()
==>1.0.0
gremlin> Gremlin.version()
==>3.0.1-incubating
gremlin> graph = TitanFactory.open('inmemory'); g = graph.traversal()
==>graphtraversalsource[standardtitangraph[inmemory:[127.0.0.1]], standard]
gremlin> vertex = graph.addVertex(T.label, 'user', 'given_name', 'Joey', 'family_name', 'Tribbiani')
==>v[4200]
gremlin> pizza = graph.addVertex(T.label, 'meal', 'name', 'pizza')
==>v[4104]
gremlin> votes = vertex.addEdge('votes_for', pizza, 'type', 'eat', 'weight', 0.8d)
==>e[1zh-38o-4r9-360][4200-votes_for->4104]
gremlin> g.E(votes).valueMap(true)
==>[label:votes_for, weight:0.8, id:2rx-38o-4r9-360, type:eat]
gremlin> g.V(vertex.id()).outE('votes_for').has('type','eat').as('e').inV().has('name','pizza').select('e').property('weight', 0.99d).iterate(); g.E(votes).valueMap(true)
==>[label:votes_for, weight:0.99, id:2rx-38o-4r9-360, type:eat]

Titan?

Joey node vertex id, Titan . :

g.V().has("given_name", "Joey")

- / , ?

TinkerPop, , :

vote_ label + type, vote_for_eat?

, , , vote_for_eat, . :

g.V(vertex.id()).outE('vote_for_eat', 'vote_for_play', 'vote_for_sleep')

Titan, , ONE2ONE. , votes_for_eat Joey pizza.

+3

. :

, .

, . , , / weight:

g.V(vertex.id()).outE('votes_for').has('type', 'eat')
    .where(__.inV().hasLabel('meal').has('name','pizza')) // filter for the edge to update
    .tryNext()                                            // select the edge if it exists
    .orElseGet({g.V(vertex.id()).next()
        .addEdge('votes_for', g.V(pizzaId).next(), 'type', 'eat')}) // otherwise, add the edge
    .property('weight', 0.99)               // finally, update / add the 'weight' property

Gremlin.Bin.

+2

In Gremlin, a rib request can be used to update a rib. Let Joey Tribbiani node have id = "123", and pizza id = "234", and the updated weight will be 0.9

g.V('123').addE('votes_for').to(g.V('234')).property('weight','0.9')

Satisfaction Additional Requirements :

  • If the edge does not exist, it will be created.
  • Duplication of the same edge will not occur

In addition, it will also save other properties.

+1
source

Source: https://habr.com/ru/post/1661607/


All Articles