package xlzx.mapreduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;
import java.io.IOException;
public class WordCount {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException{
if(args.length < 2){
System.out.println("There can be only two input and output parameters");
}
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setReducerClass(WordReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
public static class WordMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
protected void map(LongWritable key, Text value1, Context context) throws
IOException, InterruptedException {
String line = value1.toString();
String[] words = line.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 reduce(Text key, Iterable<IntWritable> value2, Context context) throws
IOException, InterruptedException{
int sum = 0;
for(IntWritable v3 :value2){
sum += v3.get();
}
context.write(key, new IntWritable(sum));
}
}
}
MapReduce实例WordCount
最新推荐文章于 2025-05-13 10:11:00 发布
本文详细介绍了如何使用Hadoop的MapReduce框架实现经典的WordCount算法。通过解析代码,读者可以了解MapReduce的工作原理,包括Mapper和Reducer的实现细节,以及如何设置Job参数来运行WordCount任务。
1万+

被折叠的 条评论
为什么被折叠?



