I am trying to write a gremlin query to get all the edges between a list of vertices.
Data redacted to protect privacy. User A(42651832) -reports_to-> User
B(28729440) -reports_to-> User C(19546208)
ids = [19546208, 28729440, 42651832]
I need to find all the edges between an arbitrary list of vertices
Seems simple, but I am unable to write a query that gives results I need.
gremlin> g.V(42651832).outE('reports_to').otherV().id()
==>28729440
gremlin> g.V(28729440).outE('reports_to').otherV().id()
==>19546208
gremlin> ids = [19546208, 28729440, 42651832]
==>19546208
==>28729440
==>42651832
gremlin> g.V(ids)
==>v[19546208]
==>v[28729440]
==>v[42651832]
gremlin> g.V(ids).bothE().where(otherV().hasId(ids))
gremlin> g.V(ids).bothE().where(otherV().hasId(within(ids)))
gremlin> g.V(ids).bothE().where(otherV().hasId(within(19546208, 28729440, 42651832)))
Apparently, I think there is a type conversion issue between gremlin console and JanusGraph.
quoting as string or converting to Long seems to work.
gremlin> g.V(ids).bothE().where(otherV().hasId(within("19546208", "28729440", "42651832")))
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
gremlin> g.V(ids).bothE().where(otherV().hasId(within(19546208L, 28729440L, 42651832L)))
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
gremlin>
gremlin> g.V(ids).bothE().where(otherV().hasId(within(19546208L, 28729440L, 42651832L))).dedup()
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
Any other suggestions. Not sure why JanusGraph works this way.
This isn't quite a full answer, but hopefully it gets you close enough. I used GraphOfTheGods to test it out.
This will get all the paths from the ids list to the quoted ids in hasID() and then output a list of all the edges traversed in each path. I added a limit for readability. You could easily add all the values to a set to get a deduped answer.
# Save all the graph of the gods vertex ids to a variable
ids = [4112,4128,4136,8232,12328,16424,20520,4296,4328,4344,8440,12536]
paths = g.V(ids).until(hasId("8440","12536")).repeat(bothE().aggregate("e").otherV().simplePath()).limit(3).select('e')
==>[e[74v-6ig-9hx-368][8440-battled->4112]]
==>[e[74v-6ig-9hx-368][8440-battled->4112],e[7xb-6ig-9hx-36o][8440-battled->4128],e[1l0-36o-b2t-9o8][4128-lives->12536],e[9vp-co8-bv9-36o][16424-pet->4128]]
==>[e[74v-6ig-9hx-368][8440-battled->4112],e[7xb-6ig-9hx-36o][8440-battled->4128],e[1l0-36o-b2t-9o8][4128-lives->12536],e[9vp-co8-bv9-36o][16424-pet->4128]]
What I was able to originally get was a full path with connecting vertexes which I am including in case it could potentially be helpful.
paths = g.V(ids).until(hasId("8440","12536")).repeat(bothE().otherV().simplePath()).path().limit(5)
==>[v[4112],e[74v-6ig-9hx-368][8440-battled->4112],v[8440]]
==>[v[4128],e[7xb-6ig-9hx-36o][8440-battled->4128],v[8440]]
==>[v[4128],e[1l0-36o-b2t-9o8][4128-lives->12536],v[12536]]
==>[v[4128],e[9vp-co8-bv9-36o][16424-pet->4128],v[16424],e[9hh-co8-b2t-9o8][16424-lives->12536],v[12536]]
==>[v[4128],e[9vp-co8-bv9-36o][16424-pet->4128],v[16424],e[8p1-co8-cnp-3co][16424-brother->4344],v[4344],e[6cf-6ig-7x1-3co][8440-father->4344],v[8440]]
Separately I did some checking with the GraphOfTheGods and the explain() step and it definitely seems like a bug. If I set a list to variable it is performing an equal step instead of a within step.
paths = g.V(ids).until(hasId(ids)).repeat(out().simplePath()).limit(10).path().explain()
...RepeatStep(until([HasStep([~id.eq([4112, 4128, ...])])]),
where as listing in quotes it will properly do a within check.
paths = g.V(ids).until(hasId("8440","12536")).repeat(outE().simplePath()).limit(10).path().explain()
...RepeatStep(until([HasStep([~id.within([8440, 12536])])])
Related
The JanusGraph version is 0.5.3
storage.backend=hbase
I do this query,I get the correct label
gremlin> g.V().has("eventid","3bbe").label()
==>Company
but I do the follow query,I get result nothing
gremlin> g.V().has("eventid","3bbe").hasLabel("Company")
gremlin>
but use other eventid, i can get correct result
gremlin> g.V().has("eventid","4bbe").label()
==>Company
gremlin> g.V().has("eventid","4bbe").hasLabel("Company")
==>v[1714175422812422000]
I agree with Kelvin and Binary suggestion stated in comments. You should check the "Company" string once in "g.V().has("eventid","3bbe")" vertex properties. You can check the same using following query,
g.V().has("eventid","3bbe").valueMap(true)
This will gives you "label" as property in the output value map.
Also, if then to you find that the String "Company" is same in both the query. Then there may be an issue in Indexing done on "eventid". (Assuming that some kind of index is created on this, as this can leads to this problem).
To solve this you can try reindexing. Steps are given as follows,
For Reindex:
mgmt = graph.openManagement()
i = mgmt.getGraphIndex('IndexName')
mgmt.updateIndex(i, SchemaAction.REINDEX)
mgmt.commit()
For Enable the index:
ManagementSystem.awaitGraphIndexStatus(graph, 'IndexName').status(SchemaStatus.ENABLED).call()
NOTE: if you get "false" in enabling the index, Try enabling it 2 3 times using the same command (ManagementSystem.awaitGraphIndexStatus(graph, 'IndexName').status(SchemaStatus.ENABLED).call()). It would work eventually.
Data: Tinkerpop Modern sample graph
Query:
gremlin> g.V(1).repeat(both()).until(hasLabel("person")).path().by("name")
==>[marko,vadas]
==>[marko,josh]
==>[marko,lop,marko]
==>[marko,lop,josh]
==>[marko,lop,peter]
What doc says:
...If until() comes after repeat() it is do/while looping. If until()
comes before repeat() it is while/do looping...
Doubt:
Shouldn't until terminate the query after first match marko-vadas? or am I missing something?
It terminates the traverser on this path, otherwise you would get [marko,vadas,marko] next. The other paths were found by other traversers. If you only care about the first path, add a .limit(1).
gremlin> g.V(1).repeat(both()).until(hasLabel("person")).limit(1).path().by("name")
==>[marko,vadas]
Just getting started with gremlin.
Printing out all the Vertex values worked out fine
gremlin> g.V().values()
==>testing 2
==>Cash Processing
==>Sales
==>Marketing
==>Accounting
I was able to find all the directly connected path between my Vertices.
gremlin> g.V().hasLabel('Process')
.repeat(both().simplePath())
.until(hasLabel('Process'))
.dedup().path()
==>[v[25],v[28]]
==>[v[25],v[26]]
==>[v[26],v[27]]
==>[v[26],v[25]]
Now am trying to print out the values in the path like ['Sales', 'Accounting'] instead of [v[25],v[28]]
Not been able to figure out a way yet
Already tried and failed with
Unfold: Does not get me 1-1 mapping
gremlin> g.V().hasLabel('Process').repeat(both().simplePath()).until(hasLabel('Process')).dedup().path().unfold().values()
==>Cash Processing
==>Accounting
==>Cash Processing
==>Sales
==>Sales
==>Marketing
==>Sales
==>Cash Processing
Path seems to be of a different data-type and does not support .values() function
gremlin> g.V().hasLabel('Process')
.repeat(both().simplePath())
.until(hasLabel('Process'))
.dedup().path().values()
org.apache.tinkerpop.gremlin.process.traversal.step.util.ImmutablePath cannot be cast to org.apache.tinkerpop.gremlin.structure.Element
Tried the following google searches and didnt get the answer
gremlin print a path
gremlin get values in a path
and few more word twisting
Found one at here that was for java but that didnt work for me
l = []; g.V().....path().fill(l)
(but cant create list, Cannot set readonly property: list for class: org.apache.tinkerpop.gremlin.structure.VertexProperty$Cardinality
)
I have running it on Gremlin console (running ./gremlin.sh)
You can use the by step to modulate the elements inside the path. For example by supplying valueMap(true) to by you get the properties of the vertices, together with the vertex labels and their ids:
gremlin> g.V().repeat(both().simplePath()).times(1).dedup().path().by(valueMap(true))
==>[[id:1,name:[marko],label:person,age:[29]],[id:3,name:[lop],lang:[java],label:software]]
==>[[id:1,name:[marko],label:person,age:[29]],[id:2,name:[vadas],label:person,age:[27]]]
==>[[id:1,name:[marko],label:person,age:[29]],[id:4,name:[josh],label:person,age:[32]]]
==>[[id:2,name:[vadas],label:person,age:[27]],[id:1,name:[marko],label:person,age:[29]]]
==>[[id:3,name:[lop],lang:[java],label:software],[id:6,name:[peter],label:person,age:[35]]]
==>[[id:4,name:[josh],label:person,age:[32]],[id:5,name:[ripple],lang:[java],label:software]]
I used the modern graph which is one of TinkerPop's toy graphs that are often used for such examples. Your output will look a bit different and you may want to use something else than valueMap(true) for the by modulator. The TinkerPop documentation of the path step itself contains two more advanced examples for path().by() that you might want to check out.
My scenario is to add multiple edges between vertices in a single query:
Assume the nodes below:
These are the labels and ids I have
Users:
4100
Songs:
4200
4355
4676
I have to establish edges between these vertices
4100 --> 4200,
4100 --> 4355,
4100 --> 4676.
We can do it normally by creating single edge between node.it is not a efficient method if we want to create edge between more than 50 vertices at a time. I am using Tinkerpop 3.0.1.
I had a similar problem. With the C# SDK I do it like this:
g.V('4100')
.addE('knows').to(g.V('4200')).outV()
.addE('knows').to(g.V('4355')).outV()
.addE('knows').to(g.V('4676'))
Using the latest Tinkerpop. You could do the following:
Create a sample graph:
gremlin> graph = TinkerGraph.open();
gremlin> graph.addVertex("User").property("id", 4100);
==>vp[id->4100]
gremlin> graph.addVertex("Song").property("id", 4200);
==>vp[id->4200]
gremlin> graph.addVertex("Song").property("id", 4355);
==>vp[id->4355]
gremlin> graph.addVertex("Song").property("id", 4676);
==>vp[id->4676]
Now add the edges in a single traversal:
gremlin> graph.traversal().V().hasLabel("User").as("a").
V().hasLabel("Song").
addE("edge to song").from("a");
==>e[8][0-edge to song->2]
==>e[9][0-edge to song->4]
==>e[10][0-edge to song->6]
This shows another example of using addE within a traversal as a side effect.
If you have the vertex ids, it is very efficient to lookup by id. If you are using Gremlin Server, each request to the Gremlin Server is treated as a single transaction. You can pass the multiple statements in a Gremlin query on a single request (with bindings) rather than sending multiple requests. Separate the statements in the Gremlin query with semicolons.
l=[4200, 4355, 4676]; v=graph.vertices(4100).next(); l.each { v.addEdge("knows", graph.vertices(it).next()) }
Try this
gremlin> songs = g.V().has("album","albumname")).toList();user = g.V().has('fullName','arunkumar').next(); songs.each{user.addEdge("in",it)}
gremlin> g.E() //check the edge
Hope this helps :)
The following query of the Titan example graph does not produce what I expected:
g.V.has("age", T.lte,1000).as('young').out('battled').has("name","cerberus").copySplit(
_().back('young'),
_()
).exhaustMerge
it gives me twice the cerberus vertex, instead of hercules and cerberus
It appears that back is not working after a copySplit. Is there a way around this limitation?
Already answered on the Gremlin users mailing list, but here we go again:
These 2 alternatives would still work in Gremlin3 (with a slightly different syntax, but the concept is the same):
gremlin> g.V().has("age", T.lte, 1000).as("young").out("battled").has("name", "cerberus").as("monster").select()
==>[young:v[24], monster:v[44]]
Or:
gremlin> g.V().has("age", T.lte, 1000).out("battled").has("name", "cerberus").path()
==>[v[24], v[44]]