MapReduce编程实例

admin:Breeze

使用MapReduce来统计图书馆的书籍数量,并且按照数量从上至下进行排序。

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.TextInputFormat;  
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  
import java.io.IOException;  
  
public class LibraryBookCount {  
  
    public static class BookCountMapper extends Mapper<Object, Text, Text, IntWritable> {  
        private Text bookTitle = new Text();  
        private IntWritable count = new IntWritable(1);  
  
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {  
            // 解析每行文本,假设格式为 "书名 数量"  
            String[] parts = value.toString().split("\\s+");  
            if (parts.length == 2) {  
                bookTitle.set(parts[0]);  
                count.set(Integer.parseInt(parts[1]));  
                context.write(bookTitle, count);  
            }  
        }  
    }  
  
    public static class BookCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {  
        private IntWritable result = new IntWritable();  
  
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
            int sum = 0;  
            for (IntWritable val : values) {  
                sum += val.get();  
            }  
            result.set(sum);  
            context.write(key, result);  
        }  
    }  
  
    public static void main(String[] args) throws Exception {  
        Configuration conf = new Configuration();  
        Job job = Job.getInstance(conf, "Library Book Count");  
        job.setJarByClass(LibraryBookCount.class);  
  
        // 设置Mapper和Reducer类  
        job.setMapperClass(BookCountMapper.class);  
        job.setCombinerClass(BookCountReducer.class);  
        job.setReducerClass(BookCountReducer.class);  
  
        // 设置输入和输出格式  
        job.setInputFormatClass(TextInputFormat.class);  
        job.setOutputFormatClass(TextOutputFormat.class);  
  
        // 设置输出键值对类型  
        job.setOutputKeyClass(Text.class);  
        job.setOutputValueClass(IntWritable.class);  
  
        // 设置输入和输出路径  
        TextInputFormat.addInputPath(job, new Path(args[0]));  
        TextOutputFormat.setOutputPath(job, new Path(args[1]));  
  
        // 提交作业并等待完成  
        System.exit(job.waitForCompletion(true) ? 0 : 1);  
    }  
}  

BookCountMapper 类:解析每行文本,将书名作为键,数量作为值输出。

BookCountReducer 类:对相同书名的数量进行汇总。
main 方法:设置作业的各种参数,包括输入路径、输出路径、Mapper、Reducer等。

发表回复

Breeze Wang

A student majoring in Software Engineering at Central South University has an understanding of software development techniques, software architecture, and is able to use Godot to develop game projects. I am currently in the Game Development Laboratory at Central South University. I have experience participating in Global Game Jam. Loving game development.