最近有在学习MongoDB,看到了关于Map-Reduce,觉得蛮有意思的,所以在这里就记录下来作为学习笔记。

关于Map-Reduce的作用这里就引用一下官网以及另外一篇文章看到的,言简意赅。

1. 官网:http://docs.mongodb.org/manual/tutorial/map-reduce-examples/

The map-reduce operation is composed of many tasks, including:

  • reads from the input collection,
  • executions of the map function,
  • executions of the reduce function,
  • writes to the output collection.

2.另一篇文章:http://openmymind.net/2011/1/20/Understanding-Map-Reduce/

So what advantage does map reduce hold? The oft-cited benefit is that both the map and reduce operations can be distributed. So the code I've written above could be executed by multiple threads, multiple cpus, or even thousands of servers as-is. This is key when dealing with millions and billions of records, or smaller sets with more complex logic. For the rest of us though, I think the real benefit is the power of being able to write these types of transforms using actual programming languages, with variables, conditional statements, methods and so on. It is a mind shift from the traditional approach, but I do think even slightly complex queries are cleaner and easier to write with map reduce. We didn't look at it here, but you'll commonly feed the output of a reduce function into another reduce function - each function further transforming it towards the end-result.

好的,官网上说的是map-reduce在执行的时候包括哪些操作,步骤;另一篇文章说得是map-reduce有什么好处相对于传统的group by之类操作,而且还有他自己的见解,大家可以看看这篇文章。

接下来我就举个例子,加深对map-reduce的理解,考虑到我们有这样的一个表(说成表相对于传统数据库更好理解,NoSql里面称之为Collection),里面有字段_id,cusid,price(NoSql里面称保存字段的每条记录为Document),里面有数据如下:

Input:

=========================

{      cusid:1,    price:15      };
{      cusid:2,    price:30      };
{      cusid:2,    price:45      };
{      cusid:3,    price:45      };
{      cusid:4,    price:5        };
{      cusid:5,    price:65      };
{      cusid:1,    price:10      };
{      cusid:1,    price:30      };
{      cusid:5,    price:30      };
{      cusid:4,    price:100    };

=========================

但是我们想要得到的数据是根据cusid统计price的总和,这个可以利用group by来实现,但是前面的2个引用说了map-reduce的优势,尤其是大数据的时候,优势会很明显,那么我们就用map-reduce来实现,输出数据如下:

Output:

=========================

{      cusid:1,    price:55         };
{      cusid:2,    price:75         };
{      cusid:3,    price:45         };
{      cusid:4,    price:105      };
{      cusid:5,    price:95        };

=========================

基本的要求介绍完了,下面我们就一一实现,首先是

一. Mongo Shell 版本:

1. 首先我们编写map function来处理每一个Document(其实就是编写js脚本,但是又不同)。

红色方框里面的就是,上面的是我插入的记录数据,应该注意到了里面有一个emit函数,其作用就是做一个key-value匹配,这里就是将每一个Document的price匹配到对应的cusid中,很容易理解,是的。

2. 编写对应的reduce function,这里的functio有2个参数,key-values,对,不是key-value,values是一个数组,这里相当于做了一个group操作,全部对应一个cusid,cusid-prices。

reduceFunction主要的操作就是对每一个不同的cusid的price做求和,得到结果。

3. 执行map-reduce操作,并输出结果到一个临时的Collection中去。

红色部分即为我们要的结果,与Output结果一致。这里有一点疑问,就是cusid = 3的时候,结果的格式与其他的不一样,猜测可能是因为当cusid = 3的记录只有一条,所以就不会做类似group的操作,简言之就不会执行reduceFunction了,如果想要验证这个猜测,我们可以在插入一条cusid = 3的记录,看看结果是否会变化。

事实证明猜测是正确的,cusid = 3的结果和其他的一致了。~_~

未完待续,敬请期待!

MongoDB之Map-Reduce -- Mongo Shell版和C#版(上)的更多相关文章

  1. MongoDB - Introduction of the mongo Shell

    Introduction The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mong ...

  2. 记一次MongoDB Map&Reduce入门操作

    需求说明 用Map&Reduce计算几个班级中,每个班级10岁和20岁之间学生的数量: 需求分析 学生表的字段: db.students.insert({classid:1, age:14, ...

  3. MongoDB - The mongo Shell, mongo Shell Quick Reference

    mongo Shell Command History You can retrieve previous commands issued in the mongo shell with the up ...

  4. MongoDB - The mongo Shell, Data Types in the mongo Shell

    MongoDB BSON provides support for additional data types than JSON. Drivers provide native support fo ...

  5. MongoDB - The mongo Shell, Write Scripts for the mongo Shell

    You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform a ...

  6. MongoDB - The mongo Shell, Access the mongo Shell Help

    In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional inf ...

  7. MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell

    The db.collection.find() method returns a cursor. To access the documents, you need to iterate the c ...

  8. MongoDB基本增删改查操作-mongo shell

    基础 1.查看所有数据库: show dbs 2.选择数据库: use test 3.查看数据库中有哪些集合: show collections 如下图: 查询 1.查看集合中有哪些数据,其中abc为 ...

  9. 使用mongo shell和客户端连接至MongoDB Atlas

    MongoDB Atlas是Mongo官方的一个集群服务,也可以注册并创建一个免费的集群,但DB的大小只有500M,如果数据量不是很大的应用,可以选择该集群方案 需要注意的是,目前我使用的这个集群,服 ...

随机推荐

  1. DDD精彩

    MS STST 这难度太高了 有一个就很难的了 也许我工作的环境一般,能把SOLID简要描述一下的,都还没有遇到 SOLID还只属于OOD层次,OOA层面就更加没碰到了 Scrip 因为领域驱动设计的 ...

  2. 在浏览器中输入url地址 -> 显示主页的过程

    -来自<图解HTTP> 最近在进行前端面试方面的一些准备,看了网上许多相关的文章,发现有一个问题始终绕不开: 在浏览器中输入URL到整个页面显示在用户面前时这个过程中到底发生了什么.仔细思 ...

  3. Java基于数据源的数据库访问

    ☞ 概述 最早接触的Java访问数据库,是通过jdbc接口.后来工作之后,一般是在服务器(如weblogic)配置数据源,通过JNDI使用数据源:最近需要在程序中动态构造数据源,查了些资料,备录于此. ...

  4. Flask实战第45天:完成前台登录界面

    我们的注册页面和登录页面有很多相似之处,因此,也可以基于一个模板来实现. 首先创建一个模板html,命名为front_signbase.html, 然后修改注册页面front_signup.html, ...

  5. utf-8 长度

    作者:实现链接:https://www.zhihu.com/question/30945431/answer/91316302来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  6. 函数的扩展--ES6

    箭头函数 由于大括号被解释为代码块,所以如果箭头函数直接返回一个对象,必须在对象外面加上括号. var f = () => ({a:1}); f(); // 返回 {a: 1} 若写成: var ...

  7. luogu P1016 旅行家的预算

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  8. noip 2008 传纸条

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是 ...

  9. JZYZOJ1372 [noi2002]荒岛野人 扩展欧几里得

    http://172.20.6.3/Problem_Show.asp?id=1372 想法其实很好想,但是我扩展欧几里得还是用得不熟练,几乎是硬套模板,大概因为今天一个下午状态都不大好.扩展欧几里得算 ...

  10. 【AC自动机】HDU中模板题

    [HDU2222] 最纯粹的裸题,错误点详见注释. #include<iostream> #include<cstdio> #include<cstring> #i ...