日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hadoop入门(十九)Mapreduce的最大值程序

發布時間:2023/12/3 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hadoop入门(十九)Mapreduce的最大值程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、簡介

最大值是統計中最常使用到的,現在使用Mapreduce在海量數據中統計數據的最大值。

?

二、例子

(1)實例描述
給出三個文件,每個文件中都存儲了若干個數值,求所有數值中的最大值。

樣例輸入: ???????????????????????????????????????????
1)file1: ?

1 2 3 7 9 -99 2


2)file2: ?

11 2 23 17 9 199 22


3)file3: ?

21 12 3 17 2 39 12

期望輸出:

199

(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.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; 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 MaxValue {public static class MaxValueMapper extends Mapper<LongWritable, Text, IntWritable, NullWritable> {private Integer maxValue = null;@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {if (StringUtils.isBlank(value.toString())) {System.out.println("空白行");return;}Integer v = Integer.valueOf(value.toString().trim());if(maxValue==null||maxValue < v){maxValue = v;}}@Overrideprotected void cleanup(Context context) throws IOException, InterruptedException {if (maxValue != null)context.write( new IntWritable(maxValue), NullWritable.get());}}public static class MaxValueReducer extends Reducer< IntWritable, NullWritable,IntWritable, NullWritable> {private Integer maxValue = null;@Overrideprotected void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {Integer v = key.get();if(maxValue==null||maxValue < v){maxValue = v;}}@Overrideprotected void cleanup(Context context) throws IOException, InterruptedException {if (maxValue != null)context.write( new IntWritable(maxValue), NullWritable.get());}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {String uri = "hdfs://192.168.150.128:9000";String input = "/maxValue/input";String output = "/maxValue/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, "MaxValue");job.setJar("./out/artifacts/hadoop_test_jar/hadoop-test.jar");job.setJarByClass(MaxValue.class);job.setMapperClass(MaxValueMapper.class);job.setReducerClass(MaxValueReducer.class);job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(NullWritable.class);job.setOutputKeyClass(IntWritable.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的最大值程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。