Reputation: 434
I have an orientdb database with a few million vertices and a few hundred million edges. Some vertices have hundreds of thousands of edges associated with them.
I want to execute random walks on this graph. We'd be content, for now, to get simple random walks working.
To achieve this, my goal is to be able to pick a random edge attached to a specific vertex. What is the best way to do this?
Say I have a highly connected vertex of class "metadata" at #17:0.
I have a bunch of lightly connected vertex class "documents".
I have an edge class "metadata_of"
The metadata #17:0 has 200,000 "metadata_of" edges connecting it to 200,000 different document vertices.
I want to go from a metadata object, through a randomly selected metadata_of object, to the corresponding document object.
I had hoped to be able to run a random order sort to be able to get back a single random edge, but random functionality appears to be a pending enhancement filed back in January of 2014 - https://github.com/orientechnologies/orientdb/issues/1946 and there has been no apparent activity on it since June of 2015.
It seems like a potential way to go about this would be to retrieve the size of the inE value (in my case), then generate a random integer i between the size 0 and len(inE). From there, I want to retrieve edge[i] from the set of inE for a given vertex. I thought I had something like this working conveniently in Gremlin but on re-evaluation it doesn't appear to be working at speed - rather, it appears to be traversing the inE list until it reaches index X. Usually better than retrieving all 200k edges, but not ideal for performance.
gremlin> g = new OrientGraph("remote:localhost/mydb");
Oct 06, 2015 11:03:54 PM com.orientechnologies.common.log.OLogManager log
==>orientgraph[remote:localhost/activeint]
gremlin> v1 = g.v("#17:0")
==>v(concept)[#17:0]
gremlin> v1.inE[554] (this took about 4 seconds)
==>e[#18:8628863][#13:305536-metadata_of->#17:0]
What is the most sensible way to, given a specific vertex, select random edge attached to it?
Upvotes: 1
Views: 197
Reputation: 2632
I've created this following function in javascript with @rid like parameter:
var g=orient.getGraph();
var c=g.command("sql","select out('metadata_of').size() as num from "+rid);
var rand=Math.floor((Math.random() * c[0].getProperty('num')-1) + 0);
var pick=g.command("sql","select expand(out('metadata_of')['"+rand+"']) from "+rid);
return pick;
You can call the function in studio in this way:
select expand(getRandomEdge(12:0)) from (select getRandomEdge(12:0))
P.S Pay attention to insert a valid @rid
Upvotes: 1