from:https://my.oschina.net/chiyong/blog/289138
Mongodb 没有传统数据库的group函数,如果分组需要走MapReduce。这种MR与HadoopMR类似。下面看看Mongodb 的分组实现
现在又一张 表它的数据格式如下:
{
"_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是唯一的,是分组条件 这里把title传递过来。
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键就是要分组的title 。value是分组后的值):
{ "_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)的更多相关文章
-
Oracle 表分组 group by和模糊查询like
分组group by写法 select 字段名 from 表名 group by 字段名 查询这个字段名里的种类分组后可以加聚合函数select 字段名,聚合函数 from 表名 group by 字 ...
-
mysql分组GROUP BY常用sql
数据分组 GROUP BY GROUP BY可以根据一个或多个字段进行分组. 比如,根据prod_id分组: SELECT prod_id ,user_id FROM products GROUP B ...
-
第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; ...
-
spring data jpa条件分组查询及分页
原book对象 package com.shaying.domain; import javax.persistence.Column; import javax.persistence.Entity ...
-
sql-多表查询JOIN与分组GROUP BY
一.内部连接:两个表的关系是平等的,可以从两个表中获取数据.用ON表示连接条件 SELECT A.a,B.b FROM At AS A INNER JOINT Bt AS B ON A.m=B.n ...
-
js将数组根据条件分组
//将数组根据条件分组 function getTreeDateByParam(list, param, fun){ var data = {}; if(list && list.le ...
-
MySQL数据分组Group By 和 Having
现有以下的学生信息表: 若果现在想计算每个班的平均年龄,使用where的操作如下: SELECT Cno AS 班级, AVG(Sage) AS 平均年龄 FROM stu ; 这样的话,有多少个班就 ...
-
TFS二次开发10——分组(Group)和成员(Member)
TFS SDK 10 ——分组(Group)和成员(Member) 这篇来介绍怎样读取TFS服务器上的用户信息 首先TFS默认有如下分组(Group): SharePoint Web Applicat ...
-
【SQL】分组数据,过滤分组-group by , having
学习笔记,原文来自http://blog.csdn.net/robinjwong/article/details/24845125 创建分组 - GROUP BY 分组是在SELECT语句的GROUP ...
随机推荐
-
Spark MLlib - LFW
val path = "/usr/data/lfw-a/*" val rdd = sc.wholeTextFiles(path) val first = rdd.first pri ...
-
Regionals 2012 :: HangZhou
题目传送门排行榜 一个人做了12年北大出的题,自己还是太弱了,图论的知识忘光光,最小生成树裸题写不来,Dijkstra TLE不知道用SPFA. 简单几何(点到线段的距离) + 三分 B Steali ...
-
前端面试题之nina分享
HTML相关 1.<!DOCTYPE>标签的定义与用法. <!DOCTYPE>的定义: <!DOCTYPE>声明位于文档中的最前面的位置,处于<html> ...
-
[百度空间] [原] Empty base class optimization
最近遇到了一个诡异的问题, 数组的数据不对, 最后发现是两个类型的大小不一样导致的. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
-
CentOS 7上的性能监控工具
Linux中基于命令行的性能监控工具:dstat.top.netstat.vmstat.htop.ss.glances 1.dstat – 多类型资源统计工具(需配置epel源) 该命令整合了vmst ...
-
学习java应该了解一些html超文本标记语言(前端)
在自己学习的过程中遇到一些内容,怕忘记所以借助博客加深印象也方便查找! html超文本标记语言中,分行级元素和块级元素. 行级元素的含义:行级元素不独占一行,相邻的行级元素在一行排列:行级元素可以控制 ...
-
APP加固技术历程及未来级别方案:虚机源码保护
传统App加固技术,前后经历了四代技术变更,保护级别每一代都有所提升,但其固有的安全缺陷和兼容性问题始终未能得到解决.而下一代加固技术-虚机源码保护,适用代码类型更广泛,App保护级别更高,兼容性更强 ...
-
rapidjson对于json的序列化与反序列化
转载: https://blog.csdn.net/qq849635649/article/details/52678822 #include "rapidjson/stringbuffer ...
-
django.db.utils.OperationalError: (1045, ";Access denied for user &#39;ODBC&#39;@&#39;localhost&#39; (using password)
错误描述: 从SQLLITE数据库换为MYSQL数据库,执行 python manage.py migrate 命令时,报错:django.db.utils.OperationalError: (10 ...
-
Eclipse 02: 安装SVN插件
1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java EE Developers)版 如果没有安装的请到这里下载安装:http://ecli ...