MapReduce WordCount编程实例
一、MapReduce 原理简述
和HDFS一样,MapReduce也是采用Master/Slave的架构
它主要由Client、JobTracker、TaskTracker以及Task4个部分组成
采用“分而治之”的策略
共分为两个阶段map(映射)阶段和reduce(归纳)阶段
1、Map阶段
以处理word.txt文本为例
word.txt文本内容如下:
hello world hadoop
hello ok hive
hadoop hello
并行读取文本,对每个单词执行自定义的map()函数,形成<key,value>对。
具体过程如下:
1)读取第一行“hello world hadoop”,执行map()函数,分割单词形成Map:
<hello,1> <world,1> <hadoop,1>
2) 读取第二行“hello ok hive”,执行map()函数,分割单词形成Map:
<hello,1> <ok,1> <hive,1>
3)读取第三行“hadoop hello”,执行map()函数,分割单词形成Map:
<hadoop,1> <hello,1>
2、Reduce阶段
Reduce阶段操作是对Map的结果进行排序、合并,最后得出词频结果。
reduce()函数执行前会经过Shuffle(混排)等过程,将形成Map中间结果中相同的key组合成value数组,结果如下:
<hadoop,1,1>
<hello,1,1,1>
<hive,1>
<ok,1>
<world,1>
然后Reduce端循环执行Reduce(K,V[]),分别统计每个单词出现的次数,得到结果如下:
<hadoop,2>
<hello,3>
<hive,1>
<ok,1>
<world,1>
二、WordCount实例代码实现
工具:MyEclipse Professional 2014
代码执行环境:CentOS7
1、编写代码
共编写三个类:
Map阶段 WordCountMapper
Reduce阶段 WordReducer
主函数 RunJob
1)WordCountMapper类
package WordCount;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
/**
* 循环调用的方法,一行调用一次。
* 输入的数据格式(默认的格式)
* @param key : 该行的下标
* @param value :该行的内容
*
*/
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
for(String w :words){
context.write(new Text(w), new IntWritable(1));
}
}
}
2)WordReducer类
package WordCount;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
/**
* 每组数据调用一次该方法。
*/
//I 1
//am 1
//rod 1
protected void reduce(Text arg0, Iterable<IntWritable> arg1,Context arg2)
throws IOException, InterruptedException {
int sum = 0;
for(IntWritable i: arg1){
sum = sum + i.get();
}
arg2.write(arg0, new IntWritable(sum));
}
}
3)RunJob驱动类
package WordCount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class RunJob {
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://master:8020");
try {
FileSystem fs = FileSystem.get(conf);
Job job = Job.getInstance(conf,"wc");
job.setJarByClass(RunJob.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//输入文件地址 /user/input/word.txt
//输出文件地址 /user/output/
FileInputFormat.addInputPath(job, new Path("/user/input/word.txt"));
Path output = new Path("/user/output/");
if(fs.exists(output)){
fs.delete(output, true);
}
FileOutputFormat.setOutputPath(job, output);
boolean f= job.waitForCompletion(true);
if(f){
System.out.println("job Finished");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、打包运行
1) 在MyEclipse选项卡选择File -> Export 选项,导出jar包。
2) 在HDFS上根据代码创建文件夹并上传数据文件:
[root@master ~]# hdfs dfs -mkdir /user/input
[root@master ~]# hdfs dfs -put /bigdata/source/word.txt /user/input
3) 运行jar包:
[root@master ~]# hadoop jar WordCountTest.jar WordCount.RunJob /user/output
4) 查看结果:
[root@master ~]# hdfs dfs -ls /user/output
[root@master ~]# hdfs dfs -cat /user/output/part-r-00000
#结果如下:
hadoop 2
hello 3
hive 1
ok 1
world 1