Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve average GPA in Hadoop

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report