所有的数据处理都是map-reduce
用reduce求和
const sum = [1,2,3,4,5,6].reduce((v,t)=>{
return v+t;
//第一次v=0,t=1
//第二次v= 0+1,t=2
//第三次v=1+2,t=3......
},0)//reduce传入两个值,第一个是个函数,第二个是初始值
map处理字符串然后用reduce
const joined = 'this-is-an-example';
joined
.split('-')
.map( word => {//用map将每个单词的第一个字符变成大写
const [head,...others] = word
console.log(head,others);
return head.toUpperCase() + others.reduce((x,y) => x+y,'')
})
.reduce( (s,word)=>{
console.log('word=='+word);
return s+' '+word;
})
console.log(joined);
map/reduce处理sql
本质上所有数据处理过程(sql过程)都是map/reduce,所以可以使用map/reduce完成大量操作。而高阶函数让我们可以最大化复用这个计算过程
//所有数据处理都是sql,用map/reduce完成一些sql操作
//假设数据库中有一张成绩单,如下
let students = {
{id:1,name:'lucy',score:99,class:'a'},
{id:2,name:'lily',score:88,class:'b'}
}
let selected = students.map( student => {
return {name:student.name}//最终只会输出name这一字段的值
})
console.log(selected); //比如筛选所有分数大于80的学生 即select name from students where score>80
let selected2 = students.reduce( (list,student) => {
if(student.score > 80){
list = [...list,student]
}
return list;
},[])
console.log(selected2); /*===================================================*/ //对上述条件筛选的另一思路 map/reduce与高阶函数
//先把函数提取出来,此时并没有用到高阶函数
let selectScoreLargerThan80 = (list,student) => {
if(student.score > 80){
list = [...list,student];
}
return list;
}
//const selected_2 = students.reduce(selectScoreLargerThan80,[])
//定义一个高阶函数filter,用于生成筛选条件
const filter = (prediction) => {
return (list,item) => {
if(prediction(item)){
list = [...list,item];
}
}
}
//使用filter增加筛选条件
const selectScoreLargerThan80_2 = filter(item => item.score > 60)
const selected_2_2 = students.reduce(selectScoreLargerThan80,[]) /*=========================================*/
//按照分数排序(倒序)
//插入排序 select * from students order by score desc
const sorted = students.reduce( (list,student) => {
let i = 0;
for(;i<list.length;i++){//这里是插入的过程,排序条件是可变的,所以可以把这部分封装到一个高阶函数
if(list[i].score < student.score) break;
}
return [...list.slice(0,i),student,...list.slice(i,list.length)];//插入排序
},[]) /*========================================*/
//按照班级进行排序(groupby),然后求最大分数,插入排序
// select max(score) from students group by class //将学生按照class分组
const grouped = students.reduce( (groups,student) => {
if(!groups[student.class]){
groups[student.class] = {key:student.class,values:[]}
}
groups[student.class].values.push(student)
return groups
},{})
//提取为数组
const arrGroups = Object.values(grouped);
//计算每个分组的最大值
const final = arrGroups.map(group => {
return {
class:group.key,
max:group.values.reduce( (a,b) => {
return b.score > a ? b.score : a;
},0)
}
})
console.log(final); /*===========================*/
//使用原生的filter与find
const selected = students.filter(student => student.score > 60)
const student = students.find(student => student.id === 5)
const sorted = students.sort((student1,student2) => student1.score < student2.score )
整理自魏蒙老师视频
所有的数据处理都是map-reduce的更多相关文章
- 基于python的《Hadoop权威指南》一书中气象数据下载和map reduce化数据处理及其可视化
文档内容: 1:下载<hadoop权威指南>中的气象数据 2:对下载的气象数据归档整理并读取数据 3:对气象数据进行map reduce进行处理 关键词:<Hadoop权威指南> ...
- Map Reduce和流处理
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由@从流域到海域翻译,发表于腾讯云+社区 map()和reduce()是在集群式设备上用来做大规模数据处理的方法,用户定义一个特定的映射 ...
- Hadoop学习:Map/Reduce初探与小Demo实现
原文地址:https://blog.csdn.net/liyong199012/article/details/25423221 一. 概念知识介绍 Hadoop MapReduce是一个用于处 ...
- hadoop学习WordCount+Block+Split+Shuffle+Map+Reduce技术详解
转自:http://blog.csdn.net/yczws1/article/details/21899007 纯干货:通过WourdCount程序示例:详细讲解MapReduce之Block+Spl ...
- Google的分布式计算模型Map Reduce map函数将输入分割成key/value对
http://www.nowamagic.net/librarys/veda/detail/1768 上一篇 大规模分布式数据处理平台Hadoop的介绍 中提到了Google的分布式计算模型Map R ...
- Hadoop Map/Reduce
Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别的数据集.一个Map/Reduce ...
- MapReduce剖析笔记之三:Job的Map/Reduce Task初始化
上一节分析了Job由JobClient提交到JobTracker的流程,利用RPC机制,JobTracker接收到Job ID和Job所在HDFS的目录,够早了JobInProgress对象,丢入队列 ...
- python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))
1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...
- map reduce
作者:Coldwings链接:https://www.zhihu.com/question/29936822/answer/48586327来源:知乎著作权归作者所有,转载请联系作者获得授权. 简单的 ...
随机推荐
- CobaltStrike + Metasploit 联动使用
本节的知识摘要: 通过 beacon内置的 socks功能将本地 Msf直接代入目标内网 借助 CobaltStrike的外部 tcp监听器通过 ssh隧道直接派生一个 meterpreter到本地 ...
- 【NOIP2015模拟11.3】备用钥匙
题目 你知道Just Odd Inventions社吗?这个公司的业务是"只不过是奇妙的发明(Just Odd Inventions)".这里简称为JOI社. JOI社有N名员工, ...
- angularJS拖动marker时popup一直显示
$scope.$on('leafletDirectiveMarker.drag', function(event, arg) { arg.leafletObject.openPopup(); });
- node.js从入门到起飞
第一个node程序: 首先创建一个js文件,命名index.js(可随意),然后在文件里面输入 : console.log("Hello World"); 使用 Git Bash ...
- Java——类的继承、访问控制
[继承] <1>Java只支持单继承,不支持多继承. <2>继承父类的私有成员变量,只有所有权,没有使用权. [继承中的构造方法]
- $FFT/NTT/FWT$题单&简要题解
打算写一个多项式总结. 虽然自己菜得太真实了. 好像四级标题太小了,下次写博客的时候再考虑一下. 模板 \(FFT\)模板 #include <iostream> #include < ...
- getAttribute和getParameter
getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...
- SQL Server系列之 删除大量数据
一.写在前面 - 想说爱你不容易 为了升级数据库至SQL Server 2008 R2,拿了一台现有的PC做测试,数据库从正式库Restore(3个数据库大小夸张地达到100G+),而机器内存只有可怜 ...
- db2数据库表操作错误SQL0668N Operation not allowed for reason code "1" on table "表". SQLSTATE=57016的解决方法
错误sql Operation not allowed for reason code "1" on table "MARKET.PURE_USER".. SQ ...
- React-Native 之 GD (十七)小时风云榜按钮处理
小时风云榜按钮处理 在服务器返回给我们的 json 数据中,提供了 hasnexthour 字段,当这个字段返回为 1 的时候,表示后面还有内容,按钮可以点击,否则不能点击,按照这个思路,我们就来完成 ...