MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算。

主要由Split、Map、Partition、Sort、Combine(需要自己写)、Merge、Reduce组成,一般来说Split、Partition、Sort、Merge不需要工程师编程但是可以改写,主要是写出Map和Reduce中对数据的操作。

概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数式编程语言里借来的,还有从矢量编程语言里借来的特性。它极大地方便了编程人员在不会分布式并行编程的情况下,将自己的程序运行在分布式系统上。 当前的软件实现是指定一个Map(映射)函数,用来把一组键值对映射成一组新的键值对,指定并发的Reduce(归约)函数,用来保证所有映射的键值对中的每一个共享相同的键组。

统计单词个数

有Combine

无Combine

代码:

WordCount.java

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { /**
* @param args
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
Job job=Job.getInstance(conf,"WordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordMapper.class);
//job.setCombinerClass(WordCount)
job.setReducerClass(WordReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job,new Path("/input"));
FileOutputFormat.setOutputPath(job, new Path("/output")); System.exit(job.waitForCompletion(true)?0:1);
} public static class WordMapper extends Mapper<Object ,Text, Text, IntWritable>{
protected void map(Object key, Text value ,Mapper<Object ,Text, Text, IntWritable>.Context context) throws IOException, InterruptedException{
String[] words = value.toString().split(" ");
for (String word:words){
context.write(new Text(word),new IntWritable(1));
}
}
} public static class WordReducer extends Reducer<Text, IntWritable,Text, IntWritable>{
protected void reducer(Text key, Iterable<IntWritable> nums ,Reducer<Text, IntWritable,Text, IntWritable>.Context context) throws IOException, InterruptedException{
int sum=0;
for (IntWritable num:nums){
sum+=num.get();
}
context.write(key,new IntWritable(sum));
}
} }

  

最新文章

  1. FastCgi与PHP-fpm关系
  2. 移动端web开发 问题
  3. c# Graphics使用方法(画圆写字代码)
  4. PHP自动解压上传的rar文件
  5. C++11的一些新特性
  6. webserver&lt;1&gt;
  7. 万圣节福利:红孩儿3D引擎开发课程《3ds max导出插件初步》
  8. jQuery Ajax跨域问题简易解决方案
  9. SpringBoot当中如何整合mybatis和注入
  10. 【Java】a++,++a 区分记忆
  11. Python NLTK 自然语言处理入门与例程(转)
  12. python开发工具
  13. CSS expression属性
  14. WebService的几种验证方式
  15. Postman简明教程
  16. MySQL学习笔记:时间差
  17. java web中读取properties文件时的路径问题
  18. docker为什么适合devops?
  19. HashSet,TreeSet和LinkedHashSet
  20. &quot;&quot;.indexOf() &quot;&quot;,replace(&quot;,&quot;,&quot;&quot;)的应用

热门文章

  1. [算法]移除指定元素&amp;strSr()的实现
  2. [noip模拟]心&lt;并查集&gt;
  3. 读者来信 | 刚搭完HBase集群,Phoenix一启动,HBase就全崩了,是什么原因?(已解决)
  4. E - 不爱学习的lyb HDU - 1789(贪心策略)
  5. linux下zip/unzip详解
  6. Windows命令help的基本使用
  7. node.js模拟学校教务处登录
  8. awk扩展应用
  9. 《mysql 必知必会》 速查指南
  10. 【PHP】PHP基本语法