我正在尝试获取一个包含gremlin管道不同阶段顶点的结果集。例如,考虑以下示例图:
CITY name = NY
CAR model =特斯拉, color = white
CAR model =丰田, color = Red
PERSON
--lives --> City (NY)
--owns --> Car (Tesla)
name = xyz
gender = male
人
--lives --> City (NY)
--owns --> Car (Toyota)
name = abc
gender = male
人
--lives --> City (NY)
--owns --> Car (Tesla)
name = def
gender = female
上图,因为它不是很清楚,包含3个人的3个顶点,所有顶点都链接到一个城市节点并链接到两个不同的汽车节点。
如何在gremlin中编写查询,返回居住在纽约的男性人员名单以及他们拥有的汽车。
到目前为止,我有一个这样做的管道:
GremlinPipeline pipe = new GremlinPipeline(graph.getVerticesOfClass("City"));
pipe.has("name", "NY").in("lives").has("gender", "male")
这让我只回到了纽约顶点的男性人物,而不是他们的汽车。
以下归还汽车,但不是人。
pipe.has("name", "NY").in("lives").has("gender", "male").out("owns")
是否有一种很好的方法可以将这两种顶点类型作为单个查询结果的一部分。这类似于我在Orient SQL中使用遍历查询实现的功能。
答案 0 :(得分:5)
有多种方法可以做到这一点。如果你真的想把人和车混合在一个列表中,你可以使用商店管道:
list = []
pipe.has("name", "NY").in("lives").has("gender", "male").store(list).out('owns').store(list).iterate()
list
如果您希望保持人与他们(可能是多辆)汽车之间的关系,那么我建议您创建汽车人员地图:
pipe.has("name", "NY").in("lives").has("gender", "male").groupBy{it.name}{it.out('owns')}.cap()
这是一个完整的类,它使用Tinkerpop图实现Java中的所有三种方法。
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.util.PipesFunction;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Foo
{
private static final PipesFunction<Vertex, String> NAME_FUNCTION = new PipesFunction<Vertex, String>()
{
@Override
public String compute(Vertex vertex)
{
return vertex.getProperty("name");
}
};
private static final PipesFunction<Vertex, Iterable<String>> OWNS_NAME_FUNCTION = new PipesFunction<Vertex, Iterable<String>>()
{
@Override
public Iterable<String> compute(Vertex vertex)
{
return new GremlinPipeline(vertex).out("owns").property("name");
}
};
public static void main(String[] args)
{
Graph graph = new TinkerGraph();
Vertex boy1 = graph.addVertex(1);
Vertex boy2 = graph.addVertex(2);
Vertex girl = graph.addVertex(3);
Vertex ny = graph.addVertex(4);
Vertex toyota = graph.addVertex(5);
Vertex tesla = graph.addVertex(6);
boy1.setProperty("type", "Person");
boy1.setProperty("name", "xyz");
boy1.setProperty("gender", "male");
boy2.setProperty("type", "Person");
boy2.setProperty("name", "abc");
boy2.setProperty("gender", "male");
girl.setProperty("type", "Person");
girl.setProperty("name", "def");
girl.setProperty("gender", "female");
ny.setProperty("type", "City");
ny.setProperty("name", "NY");
toyota.setProperty("type", "Car");
toyota.setProperty("name", "toyota");
toyota.setProperty("color", "red");
tesla.setProperty("type", "Car");
tesla.setProperty("name", "tesla");
toyota.setProperty("color", "white");
boy1.addEdge("lives", ny);
boy1.addEdge("owns", tesla);
boy2.addEdge("lives", ny);
boy2.addEdge("owns", toyota);
girl.addEdge("lives", ny);
girl.addEdge("owns", tesla);
// Reading a pipe
GremlinPipeline pipe = new GremlinPipeline(graph.getVertices("type", "City"));
pipe = pipe.has("name", "NY").in("lives").has("gender", "male");
for (Object o : pipe)
{
System.out.println(o.toString());
}
// Reading a list
List list = new ArrayList();
pipe = new GremlinPipeline(graph.getVertices("type", "City"));
pipe.has("name", "NY").in("lives").has("gender", "male").store(list, NAME_FUNCTION).out("owns").store(list, NAME_FUNCTION).iterate();
System.out.println(list);
// Reading a map
pipe = new GremlinPipeline(graph.getVertices("type", "City"));
Map map = (Map) pipe.has("name", "NY").in("lives").has("gender", "male").groupBy(NAME_FUNCTION, OWNS_NAME_FUNCTION).cap().next();
System.out.println(map);
}
}