Mongodb 带终结器的gmongo MapReducer
我不知道在使用gmongo时如何设置终结器。 我的“reduce”包含要在“Finalize”中使用的数组 Map和Reduce如下所示Mongodb 带终结器的gmongo MapReducer,mongodb,mongodb-java,gmongo,Mongodb,Mongodb Java,Gmongo,我不知道在使用gmongo时如何设置终结器。 我的“reduce”包含要在“Finalize”中使用的数组 Map和Reduce如下所示 String map=""" function map(){ key = this.vendor var inSec=Math.round(this.timeTook/1000*100)/100 value= {response_time:[inSec]} emit(key, value) } """ String reduce = """
String map="""
function map(){
key = this.vendor
var inSec=Math.round(this.timeTook/1000*100)/100
value= {response_time:[inSec]}
emit(key, value)
}
"""
String reduce = """
function reduce(key,values){
var call_list={response_time:[]}
var count = 0
var total=0
for (var i in values){
call_list.response_time=values[i].response_time.concat(call_list.response_time)
}
call_list.response_time= call_list.response_time.sort(function(a,b){return a-b})
return call_list
}
"""
String collection="mapreduceresult"
当我像下面那样调用mapreduce时,我得到一个错误-查询太大
MapReduceCommand cmd = new MapReduceCommand(logCollection, map,reduce,null,MapReduceCommand.OutputType.MERGE,null)
cmd.setOutputDB(collection)
cmd.setFinalize(finalizer) //- wanted to use Finalize as the reducer returns an Array
logCollection.mapReduce(cmd) //This is giving an error - Query is too large..
只有这是我的工作,但不知道如何设置
logCollection.mapReduce(map,reduce,collection,[:])
看看这个工作示例,finalize函数只是将数字转换为字符串:
@Grab(group='com.gmongo', module='gmongo', version='1.2')
import com.gmongo.GMongo
import com.mongodb.MapReduceCommand
import com.mongodb.BasicDBObject
def mongo = new GMongo()
def db = mongo.getDB("gmongo")
def words = ['foo', 'bar', 'baz']
def rand = new Random()
db.words.drop()
1000.times {
db.words << [word: words[rand.nextInt(3)]]
}
assert db.words.count() == 1000
def map = """
function map() {
emit(this.word, {count: 1})
}
"""
def reduce = """
function reduce(key, values) {
var count = 0
for (var i = 0; i < values.length; i++)
count += values[i].count
return {count: count}
}
"""
def finalize = """
function finalize(key, reducedValue) {
return {count: reducedValue.count.toString()}
}
"""
def command = new MapReduceCommand(db.words, map, reduce, 'mrresult', MapReduceCommand.OutputType.REPLACE, new BasicDBObject())
command.setFinalize(finalize)
db.words.mapReduce(command)
println db.mrresult.findOne()
我传递给MapReduceCommand的参数不正确。我用参数作为例子,但是在“缩小”期间仍然得到“查询没有记录太大”。我接受这个答案——因为它回答了我最初的问题——怎么做?