mongodb之map reduce

上次的mongodb之group http://my.oschina.net/jmacs/blog/97078

下面展示下map reduce

数据:

{ "_id" : "c09a50203a3b45408600bce2953a3a3c", "name" : "hexin0", "value" : 0, "group" : 0 }
{ "_id" : "799d75ae6bac4eca82a13460760b8317", "name" : "hexin1", "value" : 1, "group" : 1 }
{ "_id" : "cb802750f8014a638b0b8f9fe8bdc9fe", "name" : "hexin2", "value" : 2, "group" : 2 }
{ "_id" : "9cfbdcc1bfb74adc84a7eea233e52edc", "name" : "hexin3", "value" : 3, "group" : 0 }
{ "_id" : "d4067e0ea1624ec4ac983608d584b9d4", "name" : "hexin4", "value" : 4, "group" : 1 }
{ "_id" : "5c5acc23602048f08230679cdcf0c0fa", "name" : "hexin5", "value" : 5, "group" : 2 }
{ "_id" : "e0a72e0c494a44908acca4e342f87c72", "name" : "hexin6", "value" : 6, "group" : 0 }
{ "_id" : "1c3744a036ae432daeb5b01fd71e13ef", "name" : "hexin7", "value" : 7, "group" : 1 }
{ "_id" : "708ac5ee6f0a4cc0b0abbb96b4096299", "name" : "hexin8", "value" : 8, "group" : 2 }
{ "_id" : "f982e8971d91448286cee35165a2180f", "name" : "hexin9", "value" : 9, "group" : 0 }


要求用sql描述,通俗易懂。

要求一:
SELECT group ,sum(value),count(1),avg(value)
from xx
GROUP BY group
map函数:
StringBuilder mapfun = new StringBuilder();
mapfun.append("function (){");
mapfun.append("emit(this.group,{value : this.value, sum : 0 , avg : 0});");
mapfun.append("}");

reduce函数:

StringBuilder reduceFun = new StringBuilder();
reduceFun.append("function (key,vals){");
reduceFun.append("   var res = vals[0] ;");
reduceFun.append("   var sum = 0 ;");
reduceFun.append("   var count = 0 ;");
reduceFun.append("   for(var i in vals) {");
reduceFun.append("      count ++;");
reduceFun.append("      sum += vals[i].value;");
reduceFun.append("   }");
reduceFun.append("   res.sum = sum;");
reduceFun.append("   res.avg = sum/count;");
reduceFun.append("   return res;");
reduceFun.append("}");
打印输出:
List<Map<String, Object>> list = dao.mapReduce("c", mapfun.toString(), reduceFun.toString());
for (Map<String, Object> map : list) {
	System.out.println(map);
}
输出:
{_id=0.0, value={ "value" : 0.0 , "sum" : 18.0 , "avg" : 4.5}}
{_id=1.0, value={ "value" : 1.0 , "sum" : 12.0 , "avg" : 4.0}}
{_id=2.0, value={ "value" : 2.0 , "sum" : 15.0 , "avg" : 5.0}}


要求二:
select group ,sum(value),count(1),avg(value),max(value),min(value)
from xx
group by group
map函数:
StringBuilder mapfun = new StringBuilder();
mapfun.append("function (){");
mapfun.append("emit(this.group,{group:this.group ,value : this.value});");
mapfun.append("}");
reduce函数:
StringBuilder reduceFun = new StringBuilder();
reduceFun.append("function (key,vals){");
reduceFun.append("   var res = {} ;");
reduceFun.append("   var sum = 0 ;");
reduceFun.append("   var count = 0 ;");
reduceFun.append("   var avg = 0 ;");
reduceFun.append("   var max = vals[0].value;");
reduceFun.append("   var min = vals[0].value;");
reduceFun.append("   for(var i in vals) {");
reduceFun.append("      var value =  vals[i].value;");
reduceFun.append("      count ++;");
reduceFun.append("      sum += value;");
reduceFun.append("   	if(max < value){");
reduceFun.append("          max = value");
reduceFun.append("   	}");
reduceFun.append("   	if(min > value){");
reduceFun.append("          min = value");
reduceFun.append("   	}");
reduceFun.append("   }");
reduceFun.append("   res.group = vals[0].group;");
reduceFun.append("   res.sum = sum;");
reduceFun.append("   res.avg = sum/count;");
reduceFun.append("   res.max = max;");
reduceFun.append("   res.min = min;");
reduceFun.append("   return res;");
reduceFun.append("}");
打印出书:
List<Map<String, Object>> list = dao.mapReduce("c", mapfun.toString(), reduceFun.toString());
for (Map<String, Object> map : list) {
	System.out.println(map);
}
输出:
{_id=0.0, value={ "group" : 0.0 , "sum" : 18.0 , "avg" : 4.5 , "max" : 9.0 , "min" : 0.0}}
{_id=1.0, value={ "group" : 1.0 , "sum" : 12.0 , "avg" : 4.0 , "max" : 7.0 , "min" : 1.0}}
{_id=2.0, value={ "group" : 2.0 , "sum" : 15.0 , "avg" : 5.0 , "max" : 8.0 , "min" : 2.0}}


你可能感兴趣的:(mongodb之map reduce)