Hadoop入门(二十二)Mapreduce的求平均值程序
生活随笔
收集整理的這篇文章主要介紹了
Hadoop入门(二十二)Mapreduce的求平均值程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、簡介
求平均值是統計中最常使用到的,現在使用Mapreduce在海量數據中統計數據的求平均值。
?
二、例子
(1)實例描述
給出三個文件,每個文件中都存儲了若干個數值,求所有數值中的求平均值。
樣例輸入: ???????????????????????????????????????????
1)file1: ?
2)file2: ?
3)file3: ?
?期望輸出:
14.952380952380953
?
(2)問題分析
實現統計海量數據的求平均值,不能將所有的數據加載到內存,計算只能使用類似外部排序的方式,加載一部分數據統計求和和統計個數,接著加載另一部分進行統計,最后相除取平均值。
(3)實現步驟
1)Map過程?
????首先使用默認的TextInputFormat類對輸入文件進行處理,得到文本中每行的偏移量及其內容。顯然,Map過程首先必須分析輸入的<key,value>對,得到數值,然后在mapper中統計單個分塊的求和和統計個數。
2)Reduce過程?
????經過map方法處理后,Reduce過程將獲取每個mapper的求和進行統計,分行統計出總的求和和統計個數,最后相除算平均值。
?
(3)關鍵代碼
package com.mk.mapreduce;import org.apache.commons.lang.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.*; 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;import java.io.IOException; import java.net.URI;public class AvgValue {public static class AvgValueMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> {private int sumValue = 0;private int count = 0;@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {if (StringUtils.isBlank(value.toString())) {System.out.println("空白行");return;}int v = Integer.parseInt(value.toString().trim());sumValue = sumValue + v;count++;}@Overrideprotected void cleanup(Context context) throws IOException, InterruptedException {context.write(new IntWritable(sumValue), new IntWritable(count));}}public static class AvgValueReducer extends Reducer<IntWritable, IntWritable, DoubleWritable, NullWritable> {private int sumValue = 0;private int count = 0;@Overrideprotected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int s = key.get();int c = 0;for (IntWritable v : values)c += v.get();sumValue = sumValue + s;count = count + c;}@Overrideprotected void cleanup(Context context) throws IOException, InterruptedException {double avg = sumValue;if(count!=0){avg = sumValue * 1.0 / count;}context.write(new DoubleWritable(avg), NullWritable.get());}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {String uri = "hdfs://192.168.150.128:9000";String input = "/avgValue/input";String output = "/avgValue/output";Configuration conf = new Configuration();if (System.getProperty("os.name").toLowerCase().contains("win"))conf.set("mapreduce.app-submission.cross-platform", "true");FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);Path path = new Path(output);fileSystem.delete(path, true);Job job = new Job(conf, "AvgValue");job.setJar("./out/artifacts/hadoop_test_jar/hadoop-test.jar");job.setJarByClass(AvgValue.class);job.setMapperClass(AvgValueMapper.class);job.setReducerClass(AvgValueReducer.class);job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(DoubleWritable.class);job.setOutputValueClass(NullWritable.class);FileInputFormat.addInputPaths(job, uri + input);FileOutputFormat.setOutputPath(job, new Path(uri + output));boolean ret = job.waitForCompletion(true);System.out.println(job.getJobName() + "-----" + ret);} }總結
以上是生活随笔為你收集整理的Hadoop入门(二十二)Mapreduce的求平均值程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑查看配置软件有哪些(电脑查看配置软件
- 下一篇: 微信拍了拍肩膀叫了声爸爸怎么设置 设置拍