使用Idea从HDFS下载成绩表,去除最高分和最低分,计算总分和平均分,并将结果上传回HDFS
使用Idea从HDFS下载成绩表,去除最高分和最低分,计算总分和平均分,并将结果上传回HDFS
本文将介绍如何使用Idea开发一个Hadoop程序,从HDFS下载成绩表,对每个同学的成绩去除最高分和最低分,计算总分和平均分,并将处理结果上传回HDFS。
数据示例:
10001 22 42 60 32 77
10002 35 70 65 31 90
处理结果:
10001 134 45
10002 170 57
实现步骤如下:
- 在Idea中创建一个Java项目,并在pom.xml中添加hadoop依赖。
- 编写代码,使用hadoop提供的API从hdfs中读取成绩表,对每个同学去除最高分和最低分,求总分和平均分。
- 将处理结果写入到hdfs中。
示例代码如下:
import java.io.IOException;
import java.util.Arrays;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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;
public class ScoreAnalyzer {
// Mapper类
public static class ScoreMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable score = new IntWritable();
private Text name = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
String[] scores = new String[5];
int i = 0;
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (i == 0) {
name.set(token);
} else {
scores[i - 1] = token;
}
i++;
}
Arrays.sort(scores);
int sum = 0;
for (i = 1; i < 4; i++) {
sum += Integer.parseInt(scores[i]);
}
score.set(sum);
context.write(name, score);
}
}
// Reducer类
public static class ScoreReducer extends Reducer<Text, IntWritable, Text, Text> {
private Text result = new Text();
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
int count = 0;
for (IntWritable val : values) {
sum += val.get();
count++;
}
int average = sum / count;
result.set(sum + ' ' + average);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// 创建一个Job实例
Job job = Job.getInstance(conf, 'Score Analyzer');
// 设置Job的主类
job.setJarByClass(ScoreAnalyzer.class);
// 设置Mapper和Reducer类
job.setMapperClass(ScoreMapper.class);
job.setReducerClass(ScoreReducer.class);
// 设置输出key和value的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// 设置输入和输出路径
Path inputPath = new Path('/input/score.txt');
Path outputPath = new Path('/output/score');
FileSystem fs = FileSystem.get(conf);
if (fs.exists(outputPath)) {
fs.delete(outputPath, true);
}
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
// 提交Job并等待完成
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
运行程序后,可以在hdfs的输出路径下查看处理结果。
注意:
- 代码中的输入路径
/input/score.txt和输出路径/output/score需要根据实际情况进行修改。 - 需要确保已经配置好Hadoop环境,并且能够连接到HDFS集群。
希望本文能够帮助您快速上手使用Hadoop进行数据处理。如果您有任何问题,欢迎留言讨论。
原文地址: https://www.cveoy.top/t/topic/oFcx 著作权归作者所有。请勿转载和采集!