1

让我们假设我有来自不同农场树木的苹果。所以树结苹果,农场有树。我想要一份苹果列表,其中还包含对它们来自的农场的引用。

g = new TinkerGraph();

// apples
a1 = g.addVertex("a1");
a1.setProperty("type", "apple");

a2 = g.addVertex("a2");
a2.setProperty("type", "apple");

a3 = g.addVertex("a3");
a3.setProperty("type", "apple");


// trees
t1 = g.addVertex("t1");
t1.setProperty("type", "tree");

t2 = g.addVertex("t2");
t2.setProperty("type", "tree");


// farms
f1 = g.addVertex("f1");
f1.setProperty("type", "farm");
f1.setProperty("uid", "f1");

f2 = g.addVertex("f2");
f2.setProperty("type", "farm");
f2.setProperty("uid", "f2");

g.addEdge(t1, a1, "bears");
g.addEdge(t1, a2, "bears");
g.addEdge(t2, a3, "bears");

g.addEdge(f1, t1, "has");
g.addEdge(f2, t2, "has");

我想遍历和图表来报告每个苹果,并在其中包含农场 ID。我试过这样的事情:

g.V.has("type", "apple").copySplit(_().in("bears").in("has").map("uid"),
                                  _().map()).fairMerge

我得到的输出是:

==>{uid=f1}
==>{type=apple}
==>{uid=f1}
==>{type=apple}
==>{uid=f2}
==>{type=apple}

我想要的是:

==>{uid=f1, type=apple}
==>{uid=f1, type=apple}
==>{uid=f2, type=apple}
4

1 回答 1

5

假设 TinkerPop 2.x,您可以使用以下命令执行此操作transform

gremlin> g.V.has("type", "apple").transform{
gremlin>   m = [:] 
gremlin>   m<<it.in("bears").in("has").map("uid").next(); 
gremlin>   m<<it.map() }
==>{uid=f1, type=apple}
==>{uid=f1, type=apple}
==>{uid=f2, type=apple}

语法的<<行为就像“m”putAll()上的a。Map

TinkerPop 3.x中(除非你有充分的理由,否则你应该使用它),一个粗略的近似值是:

graph = TinkerGraph.open()
a1 = graph.addVertex(label, 'apple')
a2 = graph.addVertex(label, 'apple')
a3 = graph.addVertex(label, 'apple')
t1 = graph.addVertex(label, 'tree')
t2 = graph.addVertex(label, 'tree')
f1 = graph.addVertex(label, 'farm', 'uid', 'f1')
f2 = graph.addVertex(label, 'farm', 'uid', 'f2')
t1.addEdge('bears',a1)
t1.addEdge('bears',a2)
t2.addEdge('bears',a3)
f1.addEdge('has',t1)
f2.addEdge('has',t2)

和:

gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:7 edges:5], standard]
gremlin> g.V().hasLabel("apple").as('type').in('bears').in('has').as('uid').select('type','uid').by(label()).by('uid')
==>[type:apple, uid:f1]
==>[type:apple, uid:f1]
==>[type:apple, uid:f2]
于 2016-03-26T12:17:28.390 回答