我想在orientdb中遍历一大堆记录。 因此,结果并没有填满我机器的内存,我试图实现分页查询,但我似乎要回来了
docs中列出的原始Java方法如下:
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>("select from Customer LIMIT 20");
for (List<ODocument> resultset = database.query(query); !resultset.isEmpty(); resultset = database.query(query)) {
...
}
我已将其实现为scala:
val query = new OSQLSynchQuery[ODocument]("select from Thing LIMIT 5")
var resultset = db.query[OResultSet[ODocument]](query)
while (!resultset.isEmpty()) {
// process result set here
resultset = db.query(query)
}
这是完整的例子
def makeThing(x:Int) ={
val doc = new ODocument("Thing")
doc.field("x",x)
doc
}
val db: ODatabaseDocumentTx = new ODatabaseDocumentTx("memory:jsondb")
db.create()
db.set(MINIMUMCLUSTERS, 3)
db.set(CLUSTERSELECTION, "round-robin")
db.set(CONFLICTSTRATEGY, "content")
db.set(CHARSET, "UTF-8")
println("SAVING--------")
for (x <- 0 until 12) {
val doc:ODocument = makeThing(x)
val saved = db.save[ODocument](doc)
println(saved)
}
println("\n\nQUERYING--------")
val query = new OSQLSynchQuery[ODocument]("select from Thing LIMIT 5")
var resultset = db.query[OResultSet[ODocument]](query)
while (!resultset.isEmpty()) {
resultset.toArray.foreach(println)
resultset = db.query(query)
println("---------")
}
但这是输出:
SAVING--------
Thing#9:0{x:0} v1
Thing#10:0{x:1} v1
Thing#11:0{x:2} v1
Thing#9:1{x:3} v1
Thing#10:1{x:4} v1
Thing#11:1{x:5} v1
Thing#9:2{x:6} v1
Thing#10:2{x:7} v1
Thing#11:2{x:8} v1
Thing#9:3{x:9} v1
Thing#10:3{x:10} v1
Thing#11:3{x:11} v1
QUERYING--------
Thing#9:0{x:0} v1
Thing#9:1{x:3} v1
Thing#9:2{x:6} v1
Thing#9:3{x:9} v1
Thing#10:0{x:1} v1 # So far, so good...
---------
Thing#9:0{x:0} v1 # Already seen this. I might have expected that last item of the previous set, but not the first. Perhaps I'm supposed to skip this?
Thing#10:1{x:4} v1
Thing#10:2{x:7} v1
Thing#10:3{x:10} v1
Thing#11:0{x:2} v1
---------
Thing#9:0{x:0} v1 # Already seen this
Thing#11:1{x:5} v1
Thing#11:2{x:8} v1
Thing#11:3{x:11} v1 # Page cut short. Is because reached end of my data? Should I detect this as the end?
---------
Thing#9:0{x:0} v1 # Already seen this! Only 1 item again.
---------
Thing#9:1{x:3} v1 # Hmm... longer again. OK...?
Thing#9:2{x:6} v1
Thing#9:3{x:9} v1
Thing#10:0{x:1} v1
Thing#10:1{x:4} v1
... goes on forever...
请注意,数据库位于内存中,没有人同时写入数据库。
使用ODB客户端2.1.19