MapReduce函数的JavaScript模拟实现

转载自 D瓜哥
对于像我一样的菜鸟理解MapReduce模型非常有用。
上代码~~!

 

<html>
<head>
<title>MapReduce JS test</title>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<body>
<script>
var Job = { 
//待处理的数据 
data : [ 
"We are glad to see you here. This site is dedicated to", 
"poetry and to the people who make poetry possible", 
"poets and their readers. FamousPoetsAndPoems.com is", 
"a free poetry site. On our site you can find a large", 
"collection of poems and quotes from over 631 poets", 
"Read and Enjoy Poetry", 
"I, too, sing America", 
"I am the darker brother", 
"They send me to eat in the kitchen", 
"When company comes", 
"But I laugh", 
"And eat well", 
"And grow strong", 
"Tomorrow", 
"Ill be at the table", 
"When company comes", 
"Nobodyll dare", 
"Say to me", 
"Eat in the kitchen", 
"Then", 
"Besides", 
"Theyll see how beautiful I am", 
"And be ashamed", 
"I, too, am America" 
], 
//将数据中的每行字符串用空格分隔开, 
//map函数用于拆分数据
//并"重组"成诸如{key: 单词, value: 1}格式的对象,返回对象数组 
map : function(line) { 
var splits = line.split(" "); 
var temp = []; 
for(var i=0; i<splits.length; i++) { 
temp.push({key : splits[i], value : 1}); 
} 
return temp; 
}, 
//计算每个单词在"数据"(data)中出现的次数 
//reduce函数用于聚合归并数据
reduce : function(allSteps) { 
var result = {}; 
for(var i=0; i<allSteps.length; i++) { 
var step = allSteps[i]; 
result[step.key] = result[step.key] ? (result[step.key] + 1) : 1; 
} 
return result; 
}, 
//初始化,同时是运行的入口。 
init : function() { 
var allSteps = []; 
for(var i=0; i<Job.data.length; i++) { 
//如果这里能多线程调用Job.map函数就更逼真了。?? 
//用循环代替多线程并发,对每一“行”数据进行拆分
allSteps = allSteps.concat(Job.map(Job.data[i])); 
} 
//美中不足,这里不能多线程调用Job.reduce函数?? 
var result = Job.reduce(allSteps) 
console.log(JSON.stringify(result)); 
} 
}; // Job 
//开始执行 
Job.init(); 

</script>
	
hadoop MapReduce test

</body>
</html>

 

 

请在Firefox里面运行以上代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值