目标
我有一个足够简单的任务需要完成:设置特定边属性的权重。以这个场景为例:
我想做的是更新 weight
的值.
附加要求
type
“eat”的“votes_for”边。我知道的
我有以下信息:
votes_for
type
(在这种情况下,“吃”)name
标记为“meal”(在本例中为“pizza”)的顶点,因此还有它的 Vertex . 我想到的
我想我需要做如下事情:
votes_for
的出边(最多应为 1)有 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?
正如代码中所评论的那样,仍然存在问题。明确指定 Titan 模式有帮助吗?是否有任何我不知道的帮助程序/实用程序方法?有几个vote_for
更有意义吗?标签而不是一个标签 + type
属性,如 vote_for_eat
?谢谢你的帮助!
最佳答案
你走在正确的轨道上。查看 vertex steps documentation .
标记边缘,然后从边缘遍历到顶点进行检查,然后跳回边缘更新属性。
g.V(vertex.id()).
outE("votes_for").has("type", "eat").as("e").
inV().has("name", "pizza").
select("e").property("weight", 0.99d).
iterate()
完整的 Gremlin 控制台 session :
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
开始没有引用 vertex
的节点或其 id
,这将是 Titan composite index 的一个很好的用例.遍历将从以下开始:g.V().has("given_name", "Joey")
是否有任何我不知道的帮助程序/实用程序方法?
除了TinkerPop reference documentation ,您可以阅读以下几个教程:
使用多个 vote_for 标签而不是一个标签 + 类型属性(例如vote_for_eat)是否更有意义?
取决于您的图形模型或查询模式是什么,但更精细的标签如
vote_for_eat
可以很好地工作。您可以在遍历步骤中传递多个边标签:g.V(vertex.id()).outE('vote_for_eat', 'vote_for_play', 'vote_for_sleep')
更新
两个节点之间最多只能存在一条相同类型的边
你可以使用 Titan 模式来帮助解决这个问题,特别是定义一个 edge label with multiplicity
ONE2ONE
.如果创建多个 votes_for_eat
会抛出异常之间Joey
和 pizza
.
关于titan - 如何使用 Gremlin/Titan/TinkerPop3 更新特定的边属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40746954/