首页 文章

Gremlin获取多个顶点类型和管道的不同阶段的结果集

提问于
浏览
3

我试图获得一个结果集,其中包括gremlin管道的不同阶段的顶点 . 例如,考虑以下示例图:

城市名称=纽约

CAR模型=特斯拉,颜色=白色

CAR模型=丰田,颜色=红色

--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个顶点,所有顶点都链接到一个城市节点并链接到两个不同的汽车节点 .

如何在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中使用遍历查询实现的类似 .

1 回答

  • 4

    有多种方法可以做到这一点 . 如果你真的想在一个列表中混合人员和汽车,你可以使用商店管道:

    list = []
    pipe.has("name", "NY").in("lives").has("gender", "male").store(list).out('owns').store(list).iterate()
    list
    

    如果你想保持人与他们(也许是多辆)汽车之间的关系,那么我建议你创建一个人车 Map :

    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);
      }
    }
    

相关问题