首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有终结器的gmongo MapReducer

带有终结器的gmongo MapReducer
EN

Stack Overflow用户
提问于 2014-07-13 13:50:07
回答 1查看 335关注 0票数 0

我不知道在使用gmongo时如何设置终结器。我的“减缩”包含一个数组,我想在“Finalize”中使用它

地图和缩减如下所示

代码语言:javascript
复制
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时,会得到一个太大的错误查询

代码语言:javascript
复制
            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..

只是这样对我有用,但不知道如何设置Finalize。

代码语言:javascript
复制
   logCollection.mapReduce(map,reduce,collection,[:])
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-24 17:40:20

看看这个工作示例,finalize函数只是将数字转换成一个字符串:

代码语言:javascript
复制
@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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24723350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档