我是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}]);
由于性能问题,参考:search_field_tag我正在创建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);
}