用Hadoop分析学生成绩的代码
时间: 2024-05-01 09:16:05 浏览: 85
以下是一个简单的用Hadoop分析学生成绩的代码示例,假设输入文件格式为每行包含一个学生的姓名和分数,以逗号分隔。
Mapper类:
```
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class ScoreMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
private Text name = new Text();
private LongWritable score = new LongWritable();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] parts = value.toString().split(",");
name.set(parts[0]);
score.set(Long.parseLong(parts[1]));
context.write(name, score);
}
}
```
Reducer类:
```
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class ScoreReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
private LongWritable result = new LongWritable();
public void reduce(Text key, Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException {
long sum = 0;
int count = 0;
for (LongWritable val : values) {
sum += val.get();
count++;
}
long avg = sum / count;
result.set(avg);
context.write(key, result);
}
}
```
Driver类:
```
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 ScoreAnalyzer {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: ScoreAnalyzer <input path> <output path>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(ScoreAnalyzer.class);
job.setJobName("Score Analyzer");
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(ScoreMapper.class);
job.setReducerClass(ScoreReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
```
在以上示例中,Mapper类将输入文件中的每行转换为一个键值对,其中键为学生姓名,值为分数。Reducer类将相同姓名的所有分数求平均值后输出。Driver类设置了输入输出路径、Mapper和Reducer类以及输出键值对类型等相关参数,并运行MapReduce作业。
阅读全文