Tinkerpop/Gremlin合并顶点(和边) [英] Tinkerpop/gremlin merge vertices (and edges)
问题描述
是否有一种简单的方法来替换或合并顶点并保持/合并现有边?还是只是手动从顶点复制所有属性,并重新创建现有的边线和所有(元)属性,然后删除多余的顶点?
Is there an easy way to replace or merge vertices and keep/merge existing edges? Or just manually copy all properties from the vertex and recreate existing edges and all (meta-)properties and then drop the superfluous vertex?
推荐答案
好的,如上面的评论所述,您将在OLTP中进行匹配.这意味着您可能会有一个具体的切入点.让我们组成一个简单的示例图:
Alright, as mentioned in the comments above, you're going to do the matching in OLTP. That means you'll likely have a concrete entry point. Let's make up a simple sample graph:
g = TinkerGraph.open().traversal()
// Stackoverflow data
g.addV("user").property("login", "user3508638").as("a").
addV("user").property("login", "dkuppitz").property("age", 35).as("b").
addV("question").property("title", "Tinkerpop/gremlin merge vertices (and edges)").as("c").
addE("posted").from("a").to("c").
addE("commented").from("b").to("c").property("time", 123).iterate()
// Github data
g.addV("user").property("login", "dkuppitz").property("name", "Daniel Kuppitz").as("a").
addV("project").property("title", "TinkerPop").as("b").
addE("contributed").from("a").to("b").iterate()
根据登录名dkuppitz
匹配顶点并将其合并为单个用户顶点:
To match vertices based on login dkuppitz
and merge them into a single user vertex:
g.V().has("login", "dkuppitz").
fold().filter(count(local).is(gt(1))).unfold().
sideEffect(properties().group("p").by(key).by(value())).
sideEffect(outE().group("o").by(label).by(project("p","iv").by(valueMap()).by(inV()).fold())).
sideEffect(inE().group("i").by(label).by(project("p","ov").by(valueMap()).by(outV()).fold())).
sideEffect(drop()).
cap("p","o","i").as("poi").
addV("user").as("u").
sideEffect(
select("poi").select("p").unfold().as("kv").
select("u").property(select("kv").select(keys), select("kv").select(values))).
sideEffect(
select("poi").select("o").unfold().as("x").
select("u").sideEffect { u ->
u.path("x").getValue().each { x ->
def e = u.get().addEdge(u.path("x").getKey(), x.get("iv"))
x.get("p").each { p ->
e.property(p.getKey(), p.getValue())
}
}
}).
sideEffect(
select("poi").select("i").unfold().as("x").
select("u").sideEffect { u ->
u.path("x").getValue().each { x ->
def e = x.get("ov").addEdge(u.path("x").getKey(), u.get())
x.get("p").each { p ->
e.property(p.getKey(), p.getValue())
}
}
}).iterate()
我知道,查询非常复杂,尤其是对于嵌套很深的lambda.但不幸的是,由于我们没有addE(<traversal>)
重载,因此无法绕过lambda(我创建了
I know, the query is crazy complicated, especially with the deeply nested lambdas. But unfortunately there's no way around the lambdas, since we don't have an addE(<traversal>)
overload (I created a ticket though). Anyway, after executing the query above, the graph looks like this:
gremlin> g.V().valueMap()
==>[login:[user3508638]]
==>[title:[Tinkerpop/gremlin merge vertices (and edges)]]
==>[title:[TinkerPop]]
==>[name:[Daniel Kuppitz],login:[dkuppitz],age:[35]]
gremlin> g.V().has("login", "dkuppitz").bothE()
==>e[19][15-commented->5]
==>e[20][15-contributed->12]
gremlin> g.V().has("login", "dkuppitz").bothE().valueMap(true)
==>[label:commented,time:123,id:19]
==>[label:contributed,id:20]
将两个dkuppitz
顶点合并为一个(具有name
和age
属性),并相应地重新创建了2条边.
Both dkuppitz
vertices were merged into one (name
and age
properties are present) and the 2 edges were recreated accordingly.
更新:
使用 TINKERPOP-1793 ,我们可以消除所有的lambda:
With TINKERPOP-1793 we can get rid of all lambdas:
g.V().has("login", "dkuppitz").
fold().filter(count(local).is(gt(1))).unfold().
sideEffect(properties().group("p").by(key).by(value())).
sideEffect(outE().group("o").by(label).by(project("p","iv").by(valueMap()).by(inV()).fold())).
sideEffect(inE().group("i").by(label).by(project("p","ov").by(valueMap()).by(outV()).fold())).
sideEffect(drop()).
cap("p","o","i").as("poi").
addV("user").as("u").
sideEffect(
select("poi").select("p").unfold().as("kv").
select("u").property(select("kv").select(keys), select("kv").select(values))).
sideEffect(
select("poi").select("o").unfold().as("x").select(values).
unfold().addE(select("x").select(keys)).from(select("u")).to(select("iv"))).
sideEffect(
select("poi").select("i").unfold().as("x").select(values).
unfold().addE(select("x").select(keys)).from(select("ov")).to(select("u"))).iterate()
这篇关于Tinkerpop/Gremlin合并顶点(和边)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!