首页 > 解决方案 > MongoDB mapreduce而不是聚合

问题描述

我是 mongodb 中的 MR 工作的新手。我有一个聚合函数,如下所示:

db.acollection.aggregate([{$match:{ "userId" : { "$eq" : "raghu" }}}, 
{$group:{ "_id" : { "region":"$region", "shipMode" : "$shipMode"}, "sales" : { "$sum" : "$sales"}}}, 
{"$sort" : { "_id.region" : 1, "sales" : 1 }}, { "$limit" : 1000}]);

由于性能问题参考:MongoDB 在负载下执行缓慢我正在创建一个 MRjob。所以我应该得到所有相关的文件,Map所有的文件Reduce应该是groupby,我猜。我有如下功能:sortlimit

final MongoDatabase mongoDatabase = MongoUtils.getMongoDatabase(model);
BasicDBObject obj = pipeline.get(1);
MapReduceIterable<Document> list = mongoDatabase.getCollection(collectionName).mapReduce(getMapFunction(obj.getString("userId")), getReduceFunction());
// the above code is the main call but am mostly thinking about the map and reduce functions.

private String getMapFunction(String whereCondition) {

StringBuilder map= new StringBuilder();
map.append("function() {"
        + "var key=whereCondition;"
        + "if(this.userId==key)"
// how to get all the documents for this key ?
        + "}");
}


    private String getReduceFunction() {

        String reduce="";
// what should go here ?
        return reduce;
    }

不确定如何获得 JavaScript 代码,我想将完整的 JSON 对象作为值发出,以便可以 mapreduce 它。就像是 :

private String getMapFunction() {

//somecode here and then finally ..
  emit(this.tenantId_V, object);
}

标签: javascriptjavamongodbmapreduce

解决方案


推荐阅读