第24章:MongoDB-聚合操作--MapReduce

①MapReduce

在MongoDB的聚合框架中,还可以使用MapReduce,它非常强大和灵活,但具有一定的复杂性,专门用于实现一些复杂的聚合功能。

MongoDB中的MapReduce使用JavaScript来作为查询语言,因此能表达任意的逻辑,但是它运行非常慢,不应该用在实时的数据分析中。

MapReduce是整个大数据的精髓所在(实际中别用),所谓的MapReduce就是分为两步处理数据:

· Map:将数据分别取出;

· Reduce:负责数据的最后的处理。

可是要想在MongoDB里面实现MapReduce处理,那么复杂度是相当高的。

③更多MapReduce可选的键

1:finalize:function :可以将reduce的结果发送到finalize,这是整个处理的最后一步

2:keeptemp:boolean :是否在连接关闭的时候,保存临时结果集合

3:query:document :在发送给map前对文档进行过滤

4:sort:document :在发送给map前对文档进行排序

5:limit:integer :发往map函数的文档数量上限

6:scope:document :可以在javascript中使用的变量

7:verbose:boolean :是否记录详细的服务器日志

④范例

--按照职位分组,取得每个职位的人名

 

建立一组雇员数据

db.emps.insert("name":"张三",age": 31,"sex": "男", job": "CLERK", "salary": 2000);

db.emps.insert("name":"李四",age": 31,"sex": "女", job": "CLERK", "salary": 3000);

db.emps.insert("name":"王五",age": 31,"sex": "男", job": "MANAGER", "salary": 4000);

db.emps.insert("name":"赵六",age": 31,"sex": "女", job": "MANAGER", "salary": 5000);

db.emps.insert("name":"孙七",age": 31,"sex": "男", job": "CLERK", "salary": 6000);

db.emps.insert("name":"王八",age": 31,"sex": "女", job": "PRESIDENT", "salary": 8000);

使用MapReduce操作最终会将处理结果保存在一个单独的集合里面,而最终的处理效果如下。

 

 

第一步:编写分组的定义

var jobMaapFUN = function(key,value)[

emit("job":key,"names":value);//按照job分组,取出name

};

第二步:编写reduce操作;

var jobReduceFUN = function(key,value)[

return("job":key,"names":value);

};

第三步:针对于MapReduce处理完成的数据实际上也可以执行一个最后处理。

var jobFinalizeFun=function(key, values){

if (key == "PRESIDENT"){

 return{"job":key," names": values,"info":"公司的老大"};

}

 return{"job":key," names": values,"info":"打工仔"};

}

 

进行操作的整合:

db. runComumand({

    "mapreduce":"emps",

    "map": jobMapFun,

    "reduce":jobReduceFun,

    "out":"t_job_emp",

    finalize": jobFinalizeFun}

);

现在执行之后,所有的处理结果都保存在了“t_job_emp”集合里面。

db.t_job_emp .find().pretty();

 

 

范例
--统计出各性别的人数、平均工资、最低工资、雇员姓名
虽然大数据的时代提供有最强悍的MapReduce支持,但是从现实的开发来讲,真的不可能使用起来。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Lucky-stars/p/10555319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值