使用Hadoop MapReduce实现各省学生总分降序排序,根据省份分出输出到不同文件
使用Hadoop MapReduce实现各省学生总分降序排序,根据省份分出输出到不同文件
本文将展示如何使用Hadoop MapReduce对一组学生成绩数据进行处理,将各省的学生成绩按总分降序排序并按照省份进行分区将结果分别输出到不同的文件中。
数据样例
我们将使用以下格式的数据:
实现步骤
我们将通过以下步骤来实现这一目标:
**1、Mapper类:**解析每一行数据,提取省份和总分,并输出为键值对。
**2、Reducer类:**对每个省份的数据按总分降序排序后输出到相应的文件中。
**3、Partitioner类:**确保同一省份的数据被发送到同一个Reducer。
**4、Driver类:**配置并运行MapReduce作业。
代码实现
Mapper类
Mapper类将每一行数据解析为省份和总分,并输出为键值对,键是省份,值是总分和学生信息的组合。
package org.example.mapReduce;import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;public class ProvinceScoreMapper extends Mapper<Object, Text, Text, Text> {@Overrideprotected void map(Object key, Text value, Context context) throws IOException, InterruptedException {String line = value.toString();// Skip the header lineif (line.startsWith("考号")) {return;}String[] fields = line.split(" ");String province = fields[11];String totalScore = fields[10];context.write(new Text(province), new Text(totalScore + "," + line));}
}
Reducer类
Reducer类将每个省份的数据按总分降序排序后输出,使用MultipleOutputs将每个省的数据写入单独的文件。
package org.example.mapReduce;import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;public class ProvinceScoreReducer extends Reducer<Text, Text, Text, Text> {private MultipleOutputs<Text, Text> multipleOutputs;@Overrideprotected void setup(Context context) throws IOException, InterruptedException {multipleOutputs = new MultipleOutputs<>(context);}@Overrideprotected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {List<String> students = new LinkedList<>();for (Text val : values) {students.add(val.toString());}// Sort students by total score in descending orderCollections.sort(students, (a, b) -> {int scoreA = Integer.parseInt(a.split(",")[0]);int scoreB = Integer.parseInt(b.split(",")[0]);return Integer.compare(scoreB, scoreA);});for (String student : students) {String[] parts = student.split(",", 2);multipleOutputs.write(new Text(parts[1]), null, key.toString() + "/part");}}@Overrideprotected void cleanup(Context context) throws IOException, InterruptedException {multipleOutputs.close();}
}
Partitioner类
Partitioner类确保同一省份的数据被发送到同一个Reducer。
package org.example.mapReduce;import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;public class ProvincePartitioner extends Partitioner<Text, Text> {@Overridepublic int getPartition(Text key, Text value, int numPartitions) {String province = key.toString();return (province.hashCode() & Integer.MAX_VALUE) % numPartitions;}
}
Driver类
Driver类配置并运行MapReduce作业。
package org.example.mapReduce;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;public class ProvinceScoreSorter {public static void main(String[] args) throws Exception {if (args.length != 2) {System.err.println("Usage: ProvinceScoreSorter <input path> <output path>");System.exit(-1);}Configuration conf = new Configuration();Job job = Job.getInstance(conf, "Province Score Sorter");job.setJarByClass(ProvinceScoreSorter.class);job.setMapperClass(ProvinceScoreMapper.class);job.setPartitionerClass(ProvincePartitioner.class);job.setReducerClass(ProvinceScoreReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);FileInputFormat.addInputPath(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));MultipleOutputs.addNamedOutput(job, "province", FileOutputFormat.class, Text.class, Text.class);System.exit(job.waitForCompletion(true) ? 0 : 1);}
}
运行MapReduce作业
总结
通过以上步骤,我们实现了一个Hadoop MapReduce作业来对各省的学生总分进行降序排序,并将结果写入不同的文件中。
如有遇到问题可以找小编沟通交流哦。另外小编帮忙辅导大课作业,学生毕设等。不限于MapReduce, MySQL, python,java,大数据,模型训练等。 hadoop hdfs yarn spark Django flask flink kafka flume datax sqoop seatunnel echart可视化 机器学习等