mongodb mapredReduce 多个条件分组(group by)


现在又一张 表它的数据格式如下:

{

"_id" : ObjectId("53b224e0a1ae72328a57702c"),

"title" : "SECJ0118E",

"criteria" : "未找到对应的错误码",

"actual" : "1",

"effect" : "可能引起重大问题",

"suggestion" : "请专家提供意见",

"severity" : "Normal",

"status" : "NotOK",

"rtype" : "FormLoginExte",

"comment" : "[8/2/12 17:28:21:231 GMT+08:00] 0000001e FormLoginExte E SECJ0118E: Authentication error during authentication for user rpt",

"category" : "logs",

"time" : "0008-02-12 17:28:21"

}

{

"_id" : ObjectId("53b224e0a1ae72328a577052"),

"title" : "",

"criteria" : "未找到对应的错误码",

"actual" : "1",

"effect" : "可能引起重大问题",

"suggestion" : "请专家提供意见",

"severity" : "Normal",

"status" : "NotOK",

"rtype" : "servlet",

"comment" : "[8/2/12 19:04:41:891 CST] 0000000b servlet E com.ibm.ws.webcontainer.servlet.ServletWrapper init Uncaught.init.exception.thrown.by.servlet",

"category" : "logs",

"time" : "0008-02-12 19:04:41"

}

{

"_id" : ObjectId("53b224e0a1ae72328a576fdc"),

"title" : "系统资源设置[processes]",

"criteria" : "unlimited",

"actual" : "unlimited",

"effect" : "如果对用户资源做了limits限制,有可能造成应用运行失败或系统性能下降。",

"suggestion" : "建议修改/etc/security/limits,编辑root相关参数部分都为-1。",

"severity" : "None",

"status" : "OK",

"rtype" : "系统参数设置检查",

"comment" : "",

"category" : "params"

}

1:单个条件分组

(1) 现在我们根据title进行分组 并且统计每个组的数量

db.runCommand({ mapreduce: "check_result",

map : function Map() {

//emit 函数中的key是唯一的,是分组条件

emit(

this.title,

{count: 1}

);

},

reduce : function Reduce(key, values) {

total=0;//定义一个变量total , values是一个数组

for( var i in values){

total +=values[i].count

}

return {"count":total};

},

finalize : function Finalize(key, reduced) {

return reduced;

},

out : { inline : 1 }

});

结果如下:

{ "_id" : "" , "value" : { "count" : 113.0}}

{ "_id" : "/tmp是否设置了t标志位" , "value" : { "count" : 21.0}}

{ "_id" : "ASYN0080W" , "value" : { "count" : 120.0}}

{ "_id" : "AppServer的JVM堆最大值" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的JVM堆最小值" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的JVM标准输出日志切换周期" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的JVM标准输出日志回滚类型" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的JVM标准错误日志切换周期" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的JVM标准错误日志回滚类型" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的WebContainer线程池最大值" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的WebContainer线程池最小值" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的通用JVM参数" , "value" : { "count" : 6.0}}

{ "_id" : "AppServer的通用JVM参数-SystemGC" , "value" : { "count" : 6.0}}

{ "_id" : "Audit是否打开" , "value" : { "count" : 21.0}}

{ "_id" : "CWPKI0041W" , "value" : { "count" : 65.0}}

{ "_id" : "CWPMC0017W" , "value" : { "count" : 7.0}}

{ "_id" : "CWSAA0037W" , "value" : { "count" : 13.0}}

{ "_id" : "Could not invoke an operation on object" , "value" : { "count" : 21.0}}

{ "_id" : "DCSV0000W" , "value" : { "count" : 4.0}}

{ "_id" : "DCSV1115W" , "value" : { "count" : 137.0}}

2:多个条件分组

(1) 现在我们根据title,status,severity进行分组 并且统计每个组的数量

db.runCommand({ mapreduce: "check_result",

map : function Map() {

//emit 函数中的key是唯一的,是分组条件

emit(

{"title":this.title,"status":this.status,"serverity":this.severity}

,

{count: 1}

);

},

reduce : function Reduce(key, values) {

total=0;//定义一个变量total , values是一个数组

for( var i in values){

total +=values[i].count

}

return {"count":total};

},

finalize : function Finalize(key, reduced) {

return reduced;

},

out : { inline : 1 }

});

输出结果如下格式化:

{ "_id" : { "title" : "" , "status" : "NotOK"} , "value" : { "count" : 113.0}}

{ "_id" : { "title" : "/tmp是否设置了t标志位" , "status" : "NotOK"} , "value" : { "count" : 21.0}}

{ "_id" : { "title" : "ASYN0080W" , "status" : "NotOK"} , "value" : { "count" : 120.0}}

{ "_id" : { "title" : "AppServer的JVM堆最大值" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的JVM堆最小值" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的JVM标准输出日志切换周期" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的JVM标准输出日志回滚类型" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的JVM标准错误日志切换周期" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的JVM标准错误日志回滚类型" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的WebContainer线程池最大值" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的WebContainer线程池最小值" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的通用JVM参数" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "AppServer的通用JVM参数-SystemGC" , "status" : "NotOK"} , "value" : { "count" : 6.0}}

{ "_id" : { "title" : "Audit是否打开" , "status" : "NotOK"} , "value" : { "count" : 21.0}}

{ "_id" : { "title" : "CWPKI0041W" , "status" : "NotOK"} , "value" : { "count" : 65.0}}

{ "_id" : { "title" : "CWPMC0017W" , "status" : "NotOK"} , "value" : { "count" : 7.0}}

{ "_id" : { "title" : "CWSAA0037W" , "status" : "NotOK"} , "value" : { "count" : 13.0}}

{ "_id" : { "title" : "Could not invoke an operation on object" , "status" : "NotOK"} , "value" : { "count" : 21.0}}

{ "_id" : { "title" : "DCSV0000W" , "status" : "NotOK"} , "value" : { "count" : 4.0}}

{ "_id" : { "title" : "DCSV1115W" , "status" : "NotOK"} , "value" : { "count" : 137.0}}

mongodb mapredReduce 多个条件分组(group by)

时间: 2024-10-12 02:20:53

mongodb mapredReduce 多个条件分组(group by)的相关文章

第17课-数据库开发及ado.net 聚合函数,模糊查询like,通配符.空值处理.order by排序.分组group by-having.类型转换-cast,Convert.union all; Select 列 into 新表;字符串函数;日期函数

第17课-数据库开发及ado.net 聚合函数,模糊查询like,通配符.空值处理.order by排序.分组group by-having.类型转换-cast,Convert.union all;  Select 列 into 新表;字符串函数;日期函数 SQL聚合函数 MAX(最大值).MIN(最小值).AVG(平均值).SUM(和).COUNT(数量:记录的条数) 聚合函数对null不计算.如果一行数据都是null,count(*)包含对空值行.重复行的统计. --聚合函数演示 selec

sql分别用日期、月、年 分组 group by 分组,datepart函数

标签: datepart函数sql分别用日期月年 分组group by 分组 2013-12-26 15:31 20764人阅读 评论(1) 收藏 举报 分类: SQL Server(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. [sql] view plain copy --以2013-12-10 12:56:55为例 --convert(nvarchar(10),CreateDate,120) => 2013-12-10 --DATEPART(month,CreateDate

Android-通讯录:向分组Group添加联系人

long groupId = Constant.createGroupId; ContentValues values = new ContentValues(); for(int i=0,len=Constant.contactList.size();i<len;i++) { values.clear(); values.put(Data.RAW_CONTACT_ID, queryForRawContactId(getContentResolver(), Constant.contactLis

170.分组-group、permission、user的操作

分组 1.Group.objects.create(group_name):创建分组. 2.group.permissions:某个分组上的权限.多对多关系. (1)group.permissions.add():添加权限. (2)group.permissions.remove():删除权限. (3)group.permissions.clear():清除所有权限. (4)user.get_group_permissions()获取用户所属组的权限. 3.user.groups.某个用户上的所

MySQL分组条件,group by order by limit 顺序

having 中如果没有用聚合函数(必须sum,min),涉及到的字段名称必须在select 中有对应字段名称才可以,用到聚合函数可以不必在select中有相应字段名称的 limit 2,3:2表示从第三条记录开始,3表示要查找的是三条记录.

mysql - 分组group by、having

1. 分组:group by语句 group by必须跟在where子句里的条件之后,在order by之前(如果有的话),如下: select column1, column2 from table1, table2 where conditions group by column1, column2 order by column1, column2 1)被选中的字段(查询中select了的字段,如column1,column2)才能而且必须要出现在group by子句中,除了汇总函数:如果

mongodb 常用命令 日期, 分组

日期分组 db.msds_accessrecord.group({  keyf : function(doc){  var date = new Date(doc.addtime);  var dateKey = ""+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();  return {'day':dateKey}; //33},   initial : {"c

高级分组group by 、 group by cube、 group by rollup 使用说明

1.分别以job和deptno分组求和select sum(sal),job,deptno from emp group by grouping sets(job,deptno); 2.2^n-1分组(n表示列的数量,如果有括号里面的括起来的多个列算一个列 举例:cube((a,b),c)n=2;cube(a,b) n=2;select sum(sal) ,deptno,job from emp group by cube(deptno,job);cube__>2^n      --group

Android开发 - 掌握ConstraintLayout(九)分组(Group)

使用ConstraintLayout后我们的布局是没有层级关系的,各个View之间都是平级关系,但是如果根据某个业务条件来控制多个View的显示与否,我们需要分别对每个View进行控制,需要调用多次setVisibility().这样就显得非常不方便. 本文所介绍的Group就是解决这个问题的. Group就是一个分组,可以关联多个View,从而只需要对这个分组进行控制就可以实现这样的场景. 我们下面来介绍它的使用. 添加分组 首先,我们添加一个Group: 关联元素 然后将页面上的元素拖动到这