格莱姆林如何从管道不同阶段获取多个顶点类型的结果集

3

我正在尝试获取一个结果集,其中包括 Gremlin 流程的不同阶段的顶点。例如,考虑以下示例图形:

CITY 名称 = NY

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

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

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个顶点代表三个人,所有顶点与单个城市节点以及两个不同的汽车节点相连。

我该如何在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个回答

5

有多种方法可以实现这一点。如果您真的想将人和车辆混在一个列表中,您可以使用store管道:

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()

以下是一个完整的Java类,使用Tinkerpop图实现了所有三种方法。

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

谢谢回复,保罗。那很有道理。我能用Java实现这个吗?我发现它不允许我使用通用列表来实现。 - Rohit
不确定问题出在哪里 - Java 7 中没有闭包,因此您必须实现 PipeFunction。此外,我的两个示例都不返回管道,而是返回包含结果的集合(列表或映射),这很不幸,因为惰性评估是 Gremlin 的一个很好的特性。 - Paul Jackson
非常有用,Paul。感谢您提供详细的示例。 - Rohit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接