[英]MongoDB mapreduce instead of aggregation
我是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
, sort
和limit
我猜。 我有如下功能:
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对象作为值发出,以便我可以将其映射化。 就像是 :
private String getMapFunction() {
//somecode here and then finally ..
emit(this.tenantId_V, object);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.