MapReduce编程模型是Hadoop中最重要的组件之一,它是一种用于并行处理大规模数据的编程模型。在MapReduce中,任务被分为两个阶段:Map阶段和Reduce阶段。Map阶段负责将输入数据切分、映射为键值对,并进行排序;Reduce阶段负责对Map输出的键值对进行分组、聚合和计算。
以下是一个简单的MapReduce程序示例,用于统计文本文件中每个单词的出现次数:
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
context.write(key, new IntWritable(sum));
}
}
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.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);
以上代码演示了一个简单的WordCount程序,用于统计文本文件中每个单词的出现次数。在实际应用中,MapReduce程序可以根据具体需求进行复杂的数据处理和计算。希望这个简单的示例能帮助你更好地理解MapReduce编程模型。