0

gmongo 使用時の Finalizer の設定方法がわかりません。私の「削減」には、「ファイナライズ」で使用したい配列が含まれています

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 = """
    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,[:])
4

1 に答える 1

0

この実際の例を見てください。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()
于 2014-07-24T17:40:20.623 に答える