【问题标题】:Gremlin Tinkerpop returns an object Path, how do you access the individual verticesGremlin Tinkerpop 返回一个对象路径,你如何访问各个顶点
【发布时间】:2020-04-14 11:32:58
【问题描述】:

我可以看到这个问题已被问过几次,但我找不到适合我的答案。

我在 Java 应用程序中使用它,并且我想返回一个 ArrayList,其中包含顶点或仅包含顶点 ID 的字符串。我必须使用 Path() 步骤,因为我需要以正确的顺序返回顶点,但是我只得到一个包含 2 个对象的列表,看起来像源顶点作为对象和整个路径作为对象,我可以不操纵或使用数据?

我的遍历是:

List<Object> routeList;

routeList = g.V(sourceID).shortestPath().with(ShortestPath.edges, Direction.OUT)
                .with(ShortestPath.distance, "weight").with(ShortestPath.target, hasId(targetId))
                .with(ShortestPath.includeEdges, false).path().unfold().toList();

列表的输出是:

v[L00-041]
path[v[L00-041], v[L00-042], ...etc.... v[L00-043], v[L00-031]]

我真的需要以可用的格式返回路径。我确信以前有人担任过这个职位,可以帮助我。谢谢。

【问题讨论】:

    标签: gremlin tinkerpop tinkerpop3


    【解决方案1】:

    一般而言,对于 path() 查询,Gremlin 会返回一个 Path 对象给您,您可以对其进行迭代。这是 Gremlin 控制台中的一个示例。

    gremlin> p = g.V(1).out().limit(1).path().next()
    ==>v[1]
    ==>v[135]
    gremlin> p.class
    ==>class org.apache.tinkerpop.gremlin.process.traversal.step.util.ImmutablePath
    gremlin> p[1]
    ==>v[135]
    gremlin> p[0]
    ==>v[1]       
    gremlin> p.size()
    ==>2
    gremlin> for (e in p) {println(e)}
    v[1]
    v[135]    
    

    相关的JavaDoc在这里:

    https://tinkerpop.apache.org/javadocs/current/full/org/apache/tinkerpop/gremlin/process/traversal/step/util/ImmutablePath.html

    https://tinkerpop.apache.org/javadocs/current/full/org/apache/tinkerpop/gremlin/process/traversal/Path.html

    使用 Java 你可以做这样的事情。

    List<Path> p = g.V("1").out().limit(1).path().toList();
    p.get(0).forEach(e -> System.out.println(e));
    

    但是,当使用 shortestPath() 步骤时,返回的结果是一个 ReferencePath,其中包含起始顶点和经过的路径。您可以按如下方式从控制台访问它,也可以通过 Java 执行与上述示例类似的操作。和 Path 一样,ReferencePath 也是可迭代的。

    gremlin> routeList.class
    ==>class org.apache.tinkerpop.gremlin.structure.util.reference.ReferencePath
    gremlin> routeList[0].class
    ==>class org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex
    gremlin> routeList[1].class
    ==>class org.apache.tinkerpop.gremlin.structure.util.reference.ReferencePath
    gremlin> for (v in routeList[1]) {println v}
    v[3]
    v[8]
    v[44]
    

    这是 ReferencePath 的 JavaDoc https://tinkerpop.apache.org/javadocs/current/full/org/apache/tinkerpop/gremlin/structure/util/reference/ReferencePath.html

    【讨论】:

    • 您好,感谢您向我展示示例,这似乎对我不起作用 - 路径 routeList; routeList = g.V(sourceID).shortestPath().with(ShortestPath.edges, Direction.OUT) .with(ShortestPath.distance, "weight").with(ShortestPath.target, hasId(targetId)) .with(ShortestPath.includeEdges , 假).path().next();如果我尝试遍历 Path 对象,我收到 indexoutofbounds 说 Path 的大小只有 2。它仍然是同一个问题。
    • 我将添加一个简单的 Java 示例 - 你能确认这至少对你有用吗?
    • 请注意,您的特定查询返回的是一个 ReferencePath 对象,因此您需要先从中提取 Path。
    • 是的,当我打印出路径的类和大小时,我得到的信息与你得到的不同:I/System.out:class org.apache.tinkerpop.gremlin。 structure.util.reference.ReferencePath I/System.out: 2
    • 我想我们当时在同一个波长上!你知道我是怎么做到的吗,不用担心我可以查一些文档吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多