ITPub博客

首页 > 应用开发 > IT综合 > Mongodb MapReduce使用

Mongodb MapReduce使用

原创 IT综合 作者:百联达 时间:2016-05-26 09:04:26 0 删除 编辑
一: 背景

在利用mongodb的Group进行数据分析的时候,发现是有限制的,非唯一索引记录数不能大于20000,进而考虑使用MapReduce.

二:MapReduce的命令语法
db.runCommand(
 { mapreduce : 字符串,集合名,
   map : 函数,见下文
   reduce : 函数,见下文
   [, query : 文档,发往map函数前先给过渡文档]
   [, sort : 文档,发往map函数前先给文档排序]
   [, limit : 整数,发往map函数的文档数量上限]
   [, out : 字符串,统计结果保存的集合]
   [, keeptemp: 布尔值,链接关闭时临时结果集合是否保存]
   [, finalize : 函数,将reduce的结果送给这个函数,做最后的处理]
   [, scope : 文档,js代码中要用到的变量]
   [, jsMode : 布尔值,是否减少执行过程中BSON和JS的转换,默认true] //注:false时 BSON-->JS-->map-->BSON-->JS-->reduce-->BSON,可处理非常大的mapreduce,<br>                                    //true时BSON-->js-->map-->reduce-->BSON
   [, verbose : 布尔值,是否产生更加详细的服务器日志,默认true]
 }
);

三:Spring mongodb MapReduce代码实例

实例用来统计某个用户在一定时间端内访问某个系统某个接口的次数,已经花费的总时间。

请参见代码:


点击(此处)折叠或打开

  1. Log.info("=================gmap 日志收集开始===================================");
  2.         try {
  3.             Date date = new Date();
  4.             Date fromDate = GbdDateUtils.getBeginDate(date);
  5.             Date endDate = GbdDateUtils.getEndDate(date);
  6.             Criteria criteria = Criteria.where("date").gte(fromDate).lt(endDate);
  7.             Query query=new Query(criteria);
  8.            
  9.             String mapFunction = "function Map() {emit({\"userId\":this.userId,\"system\":this.system,\"url\":this.url},{count: 1, time: this.time});}";
  10.             String reduceFunction = "function Reduce(key, values) {var reduced = {count:0, time:0};values.forEach(function(val) {reduced.count += val.count;reduced.time += val.time;});return reduced;}";
  11.             MapReduceResults<LogValueObject> mapReduce = mongoTemplate.mapReduce(query, "logs", mapFunction, reduceFunction,new MapReduceOptions().outputCollection("results"), LogValueObject.class);
  12.    
  13.             logger.info(mapReduce.getCounts());
  14.             //清除已有的日志记录
  15.             gmapSystemLogBo.removeCurDateLogs(fromDate);
  16.             
  17.            
  18.             List<GmapSystemLog> systemLogs = new ArrayList<>();
  19.             
  20.             int i=0;
  21.             for(LogValueObject log:mapReduce)
  22.             {
  23.                 
  24.                 GmapSystemLog systemLog = new GmapSystemLog();
  25.                 systemLog.setUserId(log.get_id().getUserId().intValue());
  26.                 systemLog.setUrl(log.get_id().getUrl());
  27.                 systemLog.setSystem(log.get_id().getSystem());
  28.                 systemLog.setCount(log.getValue().getCount().intValue());
  29.                 systemLog.setTime(log.getValue().getTime().intValue());
  30.                 systemLog.setDate(fromDate);
  31.                 systemLog.setCreateDate(date);
  32.                 systemLogs.add(systemLog);
  33.                 i++;
  34.                 if (0 < i && (i % 1000 == 0)) {
  35.                     logger.info("==========保存到数据库===============");
  36.                     gmapSystemLogBo.batchInsertLogs(systemLogs);
  37.                     systemLogs.clear();
  38.                 }
  39.             }
  40.             
  41.         
  42.             if (0 < systemLogs.size()) {
  43.                 logger.info("==========保存到数据库(结束页)===============");
  44.                 gmapSystemLogBo.batchInsertLogs(systemLogs);
  45.             }
  46.             Log.info("=================gmap 日志收集结束===================================");

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28624388/viewspace-2107068/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 主数据
请登录后发表评论 登录
全部评论
15年以上互联网经验,先后从事过制造业,证券业,物业行业和物流行业信息系统和互联网产品的设计落地实施,8年系统架构经验。最近关注企业数字化转型、财务数字化服务共享、大语言模型、Kubernetes微服务架构和Istio微服务治理框架。

注册时间:2013-02-05

  • 博文量
    332
  • 访问量
    1265614