问题
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:
- [marko, lop]
- [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