get all the paths from a graph with a given criteria

1,808 views
Skip to first unread message

surendra salke

unread,
Sep 11, 2017, 8:59:14 AM9/11/17
to Gremlin-users
Hi All

I have a tree with single root vertex(id=1), 600+ intermediate vertices and 300+ leaves

I use the following query to get all the paths from a root node to the leaves

g.V(1).repeat(out()).until(outE().count().is(0)).path()

Can you please help me with a following.

if vertexList = [2,3,5,6]

Get all the path from root to leaves which has at least one of the vertex from a vertexList.
e.g path 1->2->4->6->9 should be selected
path 1->4->7>12->17->22 should not be selected

Get all such paths, where path contains at least half of the vertices from vertexList.
eng path 1->2->3->5->10 should be selected as this path has 5 virtices out of which 3 vertices(2,3,5) are from vertexList, (3/5 is greater than 0.5)
path 1->5->6>12->17->22 should not be selected as this path has 6 virtices out of which 2 vertices(5,6) are from vertexList, (2/6 is not greater than 0.5)

Daniel Kuppitz

unread,
Sep 11, 2017, 9:42:19 AM9/11/17
to gremli...@googlegroups.com
Get all the path from root to leaves which has at least one of the vertex from a vertexList.

g.V(1).repeat(out()).until(outE().count().is(0)).path().
  filter(unfold().is(within(vertexList)))


Get all such paths, where path contains at least half of the vertices from vertexList.

g.V(1).repeat(out()).until(outE().count().is(0)).path().
  filter(unfold().is(within(vertexList)).count().is(gte(vertexList.size() / 2)))

Cheers,
Daniel

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/64ed79e6-dd8b-4dd7-b683-65bd078050d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

surendra salke

unread,
Sep 12, 2017, 10:15:39 AM9/12/17
to Gremlin-users
Hi Daniel,

Thank you so much for an answer, But looks like something is not right.

Can you please help me with this?

Please see, I am receiving a response as in attached payload(1.json) for
g.V(128237704).repeat(out()).until(outE().count().is(0)).path()
when queried on germline servers Http channel. 



On Monday, September 11, 2017 at 7:12:19 PM UTC+5:30, Daniel Kuppitz wrote:
Get all the path from root to leaves which has at least one of the vertex from a vertexList.

g.V(1).repeat(out()).until(outE().count().is(0)).path().
  filter(unfold().is(within(vertexList)))


Get all such paths, where path contains at least half of the vertices from vertexList.

g.V(1).repeat(out()).until(outE().count().is(0)).path().
  filter(unfold().is(within(vertexList)).count().is(gte(vertexList.size() / 2)))

Cheers,
Daniel
On Mon, Sep 11, 2017 at 5:13 AM, surendra salke <surendr...@gmail.com> wrote:
Hi All

I have a tree with single root vertex(id=1), 600+ intermediate vertices and 300+ leaves

I use the following query to get all the paths from a root node to the leaves

g.V(1).repeat(out()).until(outE().count().is(0)).path()

Can you please help me with a following.

if vertexList = [2,3,5,6]

Get all the path from root to leaves which has at least one of the vertex from a vertexList.
e.g path 1->2->4->6->9 should be selected
path 1->4->7>12->17->22 should not be selected

Get all such paths, where path contains at least half of the vertices from vertexList.
eng path 1->2->3->5->10 should be selected as this path has 5 virtices out of which 3 vertices(2,3,5) are from vertexList, (3/5 is greater than 0.5)
path 1->5->6>12->17->22 should not be selected as this path has 6 virtices out of which 2 vertices(5,6) are from vertexList, (2/6 is not greater than 0.5)

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
1.json

Daniel Kuppitz

unread,
Sep 12, 2017, 10:31:00 AM9/12/17
to gremli...@googlegroups.com
What's wrong with it? Looks like a proper serialization of paths to me. Are the paths unexpected? If so, can you provide a sample graph?

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/4e0aa1f4-7add-4cb0-b931-acd6a059724d%40googlegroups.com.

surendra salke

unread,
Sep 12, 2017, 11:59:24 AM9/12/17
to Gremlin-users
Hi Daniel, 

Please find the graphson file in the attachment(sampleGraph.json).

curl -X POST -d "{\"gremlin\":\"VId=[20,21,24,25]\\n g.V(1).repeat(out()).until(outE().count().is(0)).path().filter(unfold().is(within(VId)))\", \"language\":\"gremlin-groovy\"}" "http://localhost:8182"

above query returns no path.

Thanks!
SampleGraph.json

Daniel Kuppitz

unread,
Sep 12, 2017, 4:21:01 PM9/12/17
to gremli...@googlegroups.com
That's an invalid GraphSON file.

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(IoCore.graphson()).readGraph("SampleGraph.json")
Edge with id already exists: 1

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/49a3cd05-6472-4607-8522-a30e75c92345%40googlegroups.com.

surendra salke

unread,
Sep 13, 2017, 3:47:16 AM9/13/17
to Gremlin-users
Looks like gremlin has a different behavior on shell and HttpChannel. ReadGraph is successful on HTTPChannel.  attach an updated graph (root id=128958600)

$ curl -X POST -d "{\"gremlin\":\"graph.io(IoCore.graphson()).readGraph('SampleGraph.json')\", \"language\":\"gremlin-groovy\"}" "http://localhost:8182" | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   253  100   138  100   115    492    410 --:--:-- --:--:-- --:--:--   492
{
    "requestId": "e43978bf-87f8-4819-a18e-d9bb3432590f",
    "result": {
        "data": [
            null
        ],
        "meta": {}
    },
    "status": {
        "attributes": {},
        "code": 200,
        "message": ""
    }
}
SampleGraph_v2.json

Daniel Kuppitz

unread,
Sep 13, 2017, 8:58:19 AM9/13/17
to gremli...@googlegroups.com
This one was loaded properly.

gremlin> g.V(128958600)
==>v[128958600]

...obviously worked too. What is your new VId collection?

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/4a4a8124-29cd-4a81-843d-4605148b4ceb%40googlegroups.com.

Daniel Kuppitz

unread,
Sep 13, 2017, 10:08:57 AM9/13/17
to gremli...@googlegroups.com
Oh, I just noticed a bug while playing with the dataset. Change the query to:

g.V(1).repeat(out()).until(outE().count().is(0)).path().
  filter(unfold().is(within(vertexList)).count().is(gte(Math.ceil(vertexList.size() / 2))))

Cheers,
Daniel

surendra salke

unread,
Oct 31, 2017, 10:27:04 AM10/31/17
to Gremlin-users
Hi Daniel, 

sincere apology for the delayed response. 

Daniel, I have no clue as to why I am not able to see filtered paths. Can you please help me understand if I am messing up during filter/unfold?
This is how my console looks...

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(IoCore.graphson()).readGraph("SampleGraph_v2.json")
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:27 edges:26], standard]
gremlin> g.V(128950464).repeat(out()).until(outE().count().is(0)).path()
==>[v[128950464], v[252170352], v[251764808]]
==>[v[128950464], v[340029664], v[128958656]]
==>[v[128950464], v[251756616], v[251768904]]
==>[v[128950464], v[340025568], v[340230176]]
gremlin> vertexList= [252170352, 340029664]
==>252170352
==>340029664
gremlin> g.V(128950464).repeat(out()).until(outE().count().is(0)).path().filter(unfold().is(within(vertexList))) 
gremlin> g.V(128950464).repeat(out()).until(outE().count().is(0)).path()
==>[v[128950464], v[252170352], v[251764808]]
==>[v[128950464], v[340029664], v[128958656]]
==>[v[128950464], v[251756616], v[251768904]]
==>[v[128950464], v[340025568], v[340230176]]
gremlin> g.V(128950464).repeat(out()).until(outE().count().is(0)).path().filter(unfold().is(within(vertexList)))
gremlin> 

gremlin> 

I am expecting the following path to be filtered.
==>[v[128950464], v[252170352], v[251764808]]
==>[v[128950464], v[340029664], v[128958656]]


Daniel Kuppitz

unread,
Oct 31, 2017, 12:41:13 PM10/31/17
to gremli...@googlegroups.com
You are comparing vertices with ids. Either do this:

vertexList = [252170352, 340029664]
g.V(128950464).repeat(out()).until(outE().count().is(0)).path().filter(unfold().hasId(within(vertexList)))

or that:

vertexList = g.V(252170352, 340029664).toLists()
g.V(128950464).repeat(out()).until(outE().count().is(0)).path().filter(unfold().is(within(vertexList))) 

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/1f526cb1-b9f0-4ce3-943b-a378c4e12907%40googlegroups.com.

surendra salke

unread,
Oct 31, 2017, 2:08:17 PM10/31/17
to Gremlin-users
Thank you! Daniel.

Apparently, the titan version I was using throwing "ImmutablePath cannot be cast to org.apache.tinkerpop.gremlin.structure.Element " on running a query you suggested. Whereas query is running fine on Janusgraph.

Thanks!

surendra salke

unread,
Nov 1, 2017, 4:31:17 AM11/1/17
to Gremlin-users
Daniel,

Sorry to bother you again, Daniel, path filtering query works fine on local gremlin shell, but the same is not happening when I am connecting to a remote shell.
Am I missing some configuration while starting gremlin server. 

$ ./bin/gremlin.sh
         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
plugin activated: janusgraph.imports
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/surendra/work/misc/Titan/janusgraph-0.1.1-hadoop2/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/surendra/work/misc/Titan/janusgraph-0.1.1-hadoop2/lib/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
13:28:31 WARN  org.apache.hadoop.util.NativeCodeLoader  - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
plugin activated: tinkerpop.hadoop
plugin activated: tinkerpop.spark
plugin activated: tinkerpop.tinkergraph
gremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Configured localhost/127.0.0.1:8182
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182] - type ':remote console' to return to local mode

gremlin> g.V(630824).repeat(out()).until(outE().count().is(0)).path()
==>[v[630824], v[618536], v[41590888]]
==>[v[630824], v[622632], v[41586792]]
==>[v[630824], v[41611368], v[41255048]]
==>[v[630824], v[41627792], v[303272]]
gremlin> g.V(630824).repeat(out()).until(outE().count().is(0)).path().filter(unfold().hasId(within(630824)))
gremlin>

gremlin>
gremlin> :remote console
==>All scripts will now be evaluated locally - type ':remote console' to return to remote mode for Gremlin Server - [localhost/127.0.0.1:8182]
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.io(IoCore.graphson()).readGraph("SampleGraph_v2.json")
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:27 edges:26], standard]
gremlin>  g.V(128950464).repeat(out()).until(outE().count().is(0)).path()
==>[v[128950464],v[252170352],v[251764808]]
==>[v[128950464],v[340029664],v[128958656]]
==>[v[128950464],v[251756616],v[251768904]]
==>[v[128950464],v[340025568],v[340230176]]
gremlin> 
gremlin>  g.V(128950464).repeat(out()).until(outE().count().is(0)).path().filter(unfold().hasId(within(252170352, 340029664)))
==>[v[128950464],v[252170352],v[251764808]]
==>[v[128950464],v[340029664],v[128958656]]
gremlin> 

Thank You!
remote.yaml
gremlin-server.yaml

Daniel Kuppitz

unread,
Nov 1, 2017, 9:46:33 AM11/1/17
to gremli...@googlegroups.com
Be sure to use Long ids in Janus.

g.V(630824L).repeat(out()).until(outE().count().is(0)).path().filter(unfold().hasId(within(630824L)))

Cheers,
Daniel

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/d0fe3e6e-17d1-443b-b457-788911a63018%40googlegroups.com.

surendra salke

unread,
Nov 1, 2017, 11:57:21 AM11/1/17
to Gremlin-users
Daniel, you Genius !! Thanks
Reply all
Reply to author
Forward
0 new messages