Fetching all the paths between two nodes using gremlin in java

↘锁芯ラ 提交于 2019-12-11 13:17:48

问题


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 :

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

-- 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:


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:

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:

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

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



来源:https://stackoverflow.com/questions/30212204/fetching-all-the-paths-between-two-nodes-using-gremlin-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!