2

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

Sagiruddin Mondal
  • 5,407
  • 2
  • 29
  • 44

1 Answers1

3

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

Community
  • 1
  • 1
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • 2
    As far as "where your query goes" and "how to collect the data" - your "query" that's in the console is really just an `Iterator` - simply treat it as such in your java code. You could also create a groovy-based library with graph traversals in it. Then reference that from your java library. Or simply have your groovy graph traversal co-exist in `.groovy` files with your `.java` files. It's pretty easy to have a mixed project. – stephen mallette May 13 '15 at 13:01
  • Can you please mension what would be the value for as("x") and .out() and .loop("x" ... ?? I mean x = ? – Sagiruddin Mondal May 13 '15 at 13:35
  • 1
    `x` is the vertex where your looped traversal traversal starts from. In my example for the 2nd path it's `marko` in the first loop and `josh` in the second loop. It's basically the same as `.loop(1)`, but it uses a named step instead of a number of steps. – Daniel Kuppitz May 13 '15 at 15:41
  • Hi Daniel, the reply was greatly helpful. But what I am facing for a graph database which is having more than 10k nodes are not good with this loop. Can you please refer similar query without the loop ... Is it right ? g.v(100).out.loop(1) { it.object != g.v(200) }.path ? – Sagiruddin Mondal May 14 '15 at 05:53
  • having a issue where "marko" and "lop" is replaced by two String variable. it is giving blank array return ! – Sagiruddin Mondal May 18 '15 at 10:30
  • I don't know, maybe provide a Gist that contains the code you're trying to execute. – Daniel Kuppitz May 18 '15 at 19:57