Я новичок в работе MR в mongodb.У меня агрегатная функция выглядит так:
db.acollection.aggregate([{$match:{ "userId" : { "$eq" : "raghu" }}},
{$group:{ "_id" : { "region":"$region", "shipMode" : "$shipMode"}, "sales" : { "$sum" : "$sales"}}},
{"$sort" : { "_id.region" : 1, "sales" : 1 }}, { "$limit" : 1000}]);
Из-за проблем с производительностью ref: 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);
}