十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
這篇文章主要介紹Hadoop如何實(shí)現(xiàn)求平均成績(jī),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供博羅網(wǎng)站建設(shè)、博羅做網(wǎng)站、博羅網(wǎng)站設(shè)計(jì)、博羅網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、博羅企業(yè)網(wǎng)站模板建站服務(wù),十多年博羅做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
//思路根據(jù)hadoop原理歸并相同人名,以人名為key,以各科成績(jī)?yōu)関alue容器元素,計(jì)算容器值的和,除以科目數(shù)。 public class AverageScore { public static class TokenizerMapper extends Mapper
public static class IntSumReducer extends Reducer{ private IntWritable result = new IntWritable();
public void reduce(Text key, Iterablevalues, 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, "word count"); 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); } }
以上是“Hadoop如何實(shí)現(xiàn)求平均成績(jī)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!