如何在OrientDB中选择随机文档
我写网络游戏。对于游戏逻辑,我需要从数据库中选择random usersetc。如何通过JavaAPI实现这一点?最有效的方法是什么如何在OrientDB中选择随机文档,orientdb,Orientdb,我写网络游戏。对于游戏逻辑,我需要从数据库中选择random usersetc。如何通过JavaAPI实现这一点?最有效的方法是什么 我可以使用类似于epseudocode的东西:从用户skiprandoum0、用户scont limit 1中进行选择,但是如何写入文档-skip的性能很差。我已经尝试过使用此代码 int numberRandom= 5; String string="["; int cluster= db.getMetadata().getSchema().getClass("
我可以使用类似于epseudocode的东西:从用户skiprandoum0、用户scont limit 1中进行选择,但是如何写入文档-skip的性能很差。我已经尝试过使用此代码
int numberRandom= 5;
String string="[";
int cluster= db.getMetadata().getSchema().getClass("User").getClusterIds()[0];
for(int i=0;i<numberRandom;i++){
int random=ThreadLocalRandom.current().nextInt(0, 96000);
if(i==(numberRandom-1))
string += cluster+":"+random + "]";
else
string += cluster+":"+random + ",";
}
Iterable<Vertex> result = g.command(new OCommandSQL("select from "+ string)).execute();
for(Vertex v:result)
System.out.println(v.getId());
让我知道它是否对您是一个好的解决方案我编写了两个java类,它们都从特定集群获得X个随机用户 第一个对我来说更快。大约0.8秒vs 1.2秒 testRandom.java testRandomSkip.java 希望能有帮助。
Ivan你可以解释得更好,我不明白你想做什么。@Alessandrororota我重新表述了我的问题如果你能等到OrientDB 2.1版出来,你可以这样查询。否则,你可以按照你的建议去做,等待结果。看起来不错,但如果记录没有被删除。按照我的逻辑,用户总是在添加和删除,所以随机数的rid可能是空的。
public class testRandom {
public static void main(String[] args) {
// TODO Auto-generated method stub
String nomeDb = "RandomUser";
try {
OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/"+nomeDb).connect("root", "root");
if(serverAdmin.existsDatabase()){ // il db esiste
//connessione a db
OrientGraph g = new OrientGraph("remote:localhost/"+nomeDb);
//------------------------------------------------
long Tbegin,Tend;
float millis;
Tbegin = System.currentTimeMillis();
int numberRandom= 5;
int random;
String cluster = "user";
Iterable<Vertex> vertices = g.command(new OCommandSQL("select from cluster:"+cluster)).execute();
List<Vertex> v_array = new ArrayList<Vertex>();
List<Vertex> res = new ArrayList<Vertex>();
for(Vertex v : vertices){
v_array.add(v);
}
int arraysize = v_array.size();
for(int i=0;i<numberRandom;i++){
random=ThreadLocalRandom.current().nextInt(0, arraysize);
res.add(v_array.get(random));
}
for(Vertex v : res){
System.out.println(v.getId());
}
Tend = System.currentTimeMillis();
millis = (Tend-Tbegin);
System.out.println("--Execution time: "+millis/1000+ "s\n");
//------------------------------------------------
//chiude db
g.shutdown();
}
else{
System.out.println("Il database '"+ nomeDb + "' non esiste");
}
serverAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class testRandom_skip {
public static void main(String[] args) {
// TODO Auto-generated method stub
String nomeDb = "RandomUser";
try {
OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/"+nomeDb).connect("root", "root");
if(serverAdmin.existsDatabase()){ // il db esiste
//connessione a db
OrientGraph g = new OrientGraph("remote:localhost/"+nomeDb);
//------------------------------------------------
long Tbegin,Tend;
float millis;
Tbegin = System.currentTimeMillis();
int numberRandom= 5;
int random;
String cluster = "user";
List<Vertex> res = new ArrayList<Vertex>();
Iterable<Vertex> q_count_V = g.command(new OCommandSQL("select count(*) from cluster:"+cluster)).execute();
Long count_V = 0l;
for(Vertex v : q_count_V){
count_V=v.getProperty("count");
break;
}
for(int i=0;i<numberRandom;i++){
random=(int)ThreadLocalRandom.current().nextLong(0, count_V);
Iterable<Vertex> vertex = g.command(new OCommandSQL("select from cluster:"+cluster+" skip "+random+" limit 1")).execute();
for(Vertex v : vertex){
res.add(v);
break;
}
}
for(Vertex v : res){
System.out.println(v.getId());
}
Tend = System.currentTimeMillis();
millis = (Tend-Tbegin);
System.out.println("--Execution time: "+millis/1000+ "s\n");
//------------------------------------------------
//chiude db
g.shutdown();
}
else{
System.out.println("Il database '"+ nomeDb + "' non esiste");
}
serverAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}