Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to achieve average GPA in Hadoop

Shulou Source: shulou.com Published: 2022-05-31 21:31:45 10月01日 Update

This article mainly introduces how to achieve the average score of Hadoop, the article is very detailed, has a certain reference value, interested friends must read it!

/ / the idea merges the same person name according to the hadoop principle, takes the person name as key, takes each subject score as the value container element, calculates the sum of container values, divided by the number of families. Public class AverageScore {public static class TokenizerMapper extends Mapper {private Text word = new Text (); public void map (Object key, Text value, Context context) throws IOException, InterruptedException {/ / split by line StringTokenizer line = new StringTokenizer (value.toString (), "\ n"); while (line.hasMoreElements ()) {/ / split StringTokenizer lineBlock = new StringTokenizer (line.nextToken ()) by space; String stuName = lineBlock.nextToken () Int stuScore = Integer.parseInt (lineBlock.nextToken ()); word.set (stuName); context.write (word, new IntWritable (stuScore));}} public static class IntSumReducer extends Reducer {private IntWritable result = new IntWritable (); public void reduce (Text key, Iterable values, Context context) throws IOException, InterruptedException {int sum = 0; int count = 0; while (values.iterator (). HasNext ()) {sum+=values.iterator () .next (). Get (); count++ } int average = sum/count; result.set (average); context.write (key, result);}} public static void main (String [] args) throws Exception {Configuration conf = new Configuration (); String [] otherArgs = new GenericOptionsParser (conf, args). GetRemainingArgs (); if (otherArgs.length! = 2) {System.err.println ("Usage: wordcount"); System.exit (2);} Job job = new Job (conf, "wordcount"); job.setJarByClass (AverageScore.class) Job.setMapperClass (TokenizerMapper.class); job.setCombinerClass (IntSumReducer.class); job.setReducerClass (IntSumReducer.class); job.setOutputKeyClass (Text.class); job.setOutputValueClass (IntWritable.class); FileInputFormat.addInputPath (job, new Path (otherArgs [0])); FileOutputFormat.setOutputPath (job, new Path (otherArgs [1])); System.exit (job.waitForCompletion (true)? 0: 1);}} these are all the contents of the article "how Hadoop achieves grade point averages". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Grades content containers articles same names values elements interests principles guys buddies ideas more knowledge subjects blanks industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Redmi Shulou Technology Huawei