1.首先建一个WCMapper 类:
package com.zhichao.wan.mr.wordcount1;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
//1.获取每一行的字符串
String sting = value.toString();
//2.开始切割字符串,成为一个个的单词
String[] words = StringUtils.split(sting, " ");
for (String word : words) {
context.write(new Text(word), new LongWritable(1));
}
}
}
2.然后建一个WCReducer 类:
package com.zhichao.wan.mr.wordcount1;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
@Override
protected void reduce(Text key, Iterable<LongWritable> values,Context context)
throws IOException, InterruptedException {
long count=0;
for (LongWritable value : values) {
count+=value.get();
}
context.write(key, new LongWritable(count));
}
}
3.最后
package com.zhichao.wan.mr.wordcount1;
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 WCRunner {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Job job=Job.getInstance();
job.setJarByClass(WCRunner.class);
job.setMapperClass(WCMapper.class);
job.setReducerClass(WCReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.setInputPaths(job, new Path("/wc/srcdata/"));
FileOutputFormat.setOutputPath(job, new Path("/wc/output"));
job.waitForCompletion(true);
}
}
5.将程序打成jar包,并拷贝到linux中。
6.建一个测试文件
7.运行jar包,指定主程序的地址
8.运行结果: