使用java中的gremlin获取两个节点之间的所有路径

时间:2023-02-01 14:17:03

Hi I am kinda new to gremlin and trying to achieve some solve by finding all the paths between two nodes. In simple query on gremlin console I was able to do that using this query :

嗨,我是gremlin的新手,并试图通过找到两个节点之间的所有路径来实现一些解决方案。在gremlin控制台上的简单查询中,我能够使用此查询执行此操作:

(Name of the first Node).loop(1){it.loops<100}{true}.has('name', (Name of the second node)).path{it.name}

But while I was trying to fetch that from java methods I am into sea of problems, like:

但是当我试图从java方法中获取它时,我遇到了问题,例如:

-- no clue on where to put the query exactly ? -- what data structure will be right to receive the array of rows. -- how to collect the first node and second node from the graph.

- 没有关于确切放置查询的位置的线索? - 接收行数组的数据结构是什么。 - 如何从图中收集第一个节点和第二个节点。

Here I have tried to proceed with this, but no clue :

在这里,我试图继续这一点,但没有任何线索:

  Graph g = new OrientGraph(AppConstants.GRAPH_LOCATION);
         List<String> vertexList = new ArrayList<String>();
         try{
             for (Vertex v : g.getVertices()) {
                    String vertexName = v.getProperty("name");
                    vertexList.add(vertexName);
                }    
             return vertexList;
         } catch (final Exception ex) {
             throw new AppSystemException(ex);
         }finally{
             g.shutdown();

Thanks, Sagir

1 个解决方案

#1


Your query doesn't make much sense to me, but the description helps. Here's an example, using TinkerGraph, to find all paths between marko and lop:

您的查询对我没有多大意义,但描述有帮助。这是一个使用TinkerGraph来查找marko和lop之间所有路径的示例:

final Graph g = TinkerGraphFactory.createTinkerGraph();
List<List> names = new ArrayList<>();
new GremlinPipeline<Vertex, ArrayList<Vertex>>(g).V().has("name", "marko").as("x").out().loop("x",
        new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
            @Override
            public Boolean compute(LoopPipe.LoopBundle<Vertex> loopBundle) {
                return loopBundle.getLoops() < 100;
            }
        }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
            @Override
            public Boolean compute(LoopPipe.LoopBundle<Vertex> loopBundle) {
                return "lop".equals(loopBundle.getObject().getProperty("name"));
            }
        }
).has("name", "lop").path(new PipeFunction<Vertex, String>() {
    @Override
    public String compute(final Vertex vertex) {
        return vertex.getProperty("name");
    }
}).fill(names);

The names list will then be filled with the following 2 entries:

然后,名称列表将填入以下2个条目:

  1. [marko, lop]
  2. [marko, josh, lop]
  3. [marko,josh,lop]

If converting Groovy to Java is the biggest problem, you should definitely check this out: Converting Gremlin Groovy to Gremlin Java

如果将Groovy转换为Java是最大的问题,那么你一定要检查一下:将Gremlin Groovy转换为Gremlin Java

#1


Your query doesn't make much sense to me, but the description helps. Here's an example, using TinkerGraph, to find all paths between marko and lop:

您的查询对我没有多大意义,但描述有帮助。这是一个使用TinkerGraph来查找marko和lop之间所有路径的示例:

final Graph g = TinkerGraphFactory.createTinkerGraph();
List<List> names = new ArrayList<>();
new GremlinPipeline<Vertex, ArrayList<Vertex>>(g).V().has("name", "marko").as("x").out().loop("x",
        new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
            @Override
            public Boolean compute(LoopPipe.LoopBundle<Vertex> loopBundle) {
                return loopBundle.getLoops() < 100;
            }
        }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
            @Override
            public Boolean compute(LoopPipe.LoopBundle<Vertex> loopBundle) {
                return "lop".equals(loopBundle.getObject().getProperty("name"));
            }
        }
).has("name", "lop").path(new PipeFunction<Vertex, String>() {
    @Override
    public String compute(final Vertex vertex) {
        return vertex.getProperty("name");
    }
}).fill(names);

The names list will then be filled with the following 2 entries:

然后,名称列表将填入以下2个条目:

  1. [marko, lop]
  2. [marko, josh, lop]
  3. [marko,josh,lop]

If converting Groovy to Java is the biggest problem, you should definitely check this out: Converting Gremlin Groovy to Gremlin Java

如果将Groovy转换为Java是最大的问题,那么你一定要检查一下:将Gremlin Groovy转换为Gremlin Java