我目前正尝试用Gmongo驱动程序在groovy中运行一个批处理作业,这个集合大约是8g我的脚本试图加载内存中的所有东西,理想情况下,我希望能够批量处理这个任务,就像Spring批处理一样,但是在groovy脚本中。
我已经尝试过batchSize(),但是这个函数仍然将整个集合检索到内存中,只是将它应用到批处理中的逻辑中。
这是我的例子
momngoDb.collection.find().collect() it -> {
//logic
}发布于 2019-05-01 16:05:05
经过深思熟虑,我发现这个解决方案最好的原因如下。
下面的代码是有效和轻的资源,这取决于您的批处理大小。
def skipSize = 0
def limitSize = Integer.valueOf(1000) batchSize (if your going to hard code the batch size then you dont need the int convertion)
def dbSize = Db.collectionName.count()
def dbRunCount = (dbSize / limitSize).round()
dbRunCount.times { it ->
dstvoDsEpgDb.schedule.find()
.skip(skipSize)
.limit(limitSize)
.collect { event ->
//run your business logic processing
}
//calculate the next skipSize
skipSize += limitSize
}发布于 2019-01-21 15:04:39
据官方医生说:
https://docs.mongodb.com/manual/tutorial/iterate-a-cursor/#read-operations-cursors
def myCursor = db.collection.find()
while (myCursor.hasNext()) {
print( myCursor.next() }
}https://stackoverflow.com/questions/54290453
复制相似问题