用Hadoop的MapReduce求最大最小值

36 篇文章 1 订阅
12 篇文章 0 订阅

最近在系统学习大数据知识,学了没有记录过几天又忘光了,所以把学习内容记录下来,方便以后查看 

找出数据中的最大值和最小值

输入数据格式:

   4568
    2
    6598
    2222222
    8899
    3
    7
    9
    1
    0
    56
    96
    564
    145
    231
    8899
    8899 

输出数据格式:

编程示例:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;
/**
 * FileName: MaxAndMinValue
 * Author:   hadoop
 * Email:    3165845957@qq.com
 * Date:     18-10-6 下午5:24
 * Description:
 * 求数据的最大值和最小值
 */
public class MaxAndMinValue {
    /**
     * 使用Mapper将数据文件中的数据本身作为Mapper输出的key直接输出
     */

    public static class MaxAndMinValueMapper extends Mapper<Object, Text, Text, LongWritable> {
        private LongWritable data = new LongWritable(0);
        private  Text keyForReduce = new Text();
        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            data.set(Long.parseLong(value.toString()));
            context.write(keyForReduce,data);
        }
    }


    /**
     * 使用Reducer将输入的key本身作为key直接输出
     */


    public static class MaxAndMinValueReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
        private long maxValue = Long.MIN_VALUE;
        private long minValue = Long.MAX_VALUE;

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            for (LongWritable item : values){
                if (item.get() > maxValue){
                    maxValue = item.get();
                }
                if (item.get() < minValue){
                    minValue = item.get();
                }
            }
            context.write(new Text("MaxValue"),new LongWritable(maxValue));
            context.write(new Text("MinValue"),new LongWritable(minValue));
        }
    }


    public static void main(String[] args) throws Exception {


        Configuration conf = new Configuration(); //设置MapReduce的配置
        String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
        if(otherArgs.length < 2){
            System.out.println("Usage: MaxAndMinValue <in> [<in>...] <out>");
            System.exit(2);
        }

        //设置作业
        //Job job = new Job(conf);
        Job job = Job.getInstance(conf);
        job.setJarByClass(MaxAndMinValue.class);
        job.setJobName("MaxAndMinValue");
        //设置处理map,reduce的类
        job.setMapperClass(MaxAndMinValueMapper.class);
        job.setReducerClass(MaxAndMinValueReducer.class);
        //设置输入输出格式的处理
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //设定输入输出路径
        for (int i = 0; i < otherArgs.length-1;++i){
            FileInputFormat.addInputPath(job,new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length-1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }
}

 

 

  • 5
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MapReduce是一种分布式计算框架,它可以将大规模数据分成小块,分布式地进行处理,并将结果合并起来。而combiner是MapReduce中的一种优化技术,它可以在Map阶段输出数据之前对其进行中间合并,减少Map输出数据量,从而提高整体的性能。 针对你的问题,可以按照以下步骤使用MapReduce和Combiner来最大、最小和总和: 1. Map阶段:读取每个文本文件,并将每行数字转化为一个键对,其中键为固定的字符串(如"max"、"min"、"sum"),为当前行的数字。 2. Combiner阶段:对每个键进行中间合并,例如对于"max"键,找出所有中的最大;对于"min"键,找出所有中的最小;对于"sum"键,计算所有的总和。 3. Reduce阶段:对每个键的最终结果进行汇总,输出到最终结果文件中。 以下是一个Java实现的示例代码: ```java public class MaxMinSum { public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); int number = Integer.parseInt(line.trim()); // 输出最大 context.write(new Text("max"), new IntWritable(number)); // 输出最小 context.write(new Text("min"), new IntWritable(number)); // 输出总和 context.write(new Text("sum"), new IntWritable(number)); } } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int result = 0; if (key.toString().equals("max")) { result = Integer.MIN_VALUE; for (IntWritable val : values) { result = Math.max(result, val.get()); } } else if (key.toString().equals("min")) { result = Integer.MAX_VALUE; for (IntWritable val : values) { result = Math.min(result, val.get()); } } else if (key.toString().equals("sum")) { for (IntWritable val : values) { result += val.get(); } } context.write(key, new IntWritable(result)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "maxminsum"); job.setJarByClass(MaxMinSum.class); job.setMapperClass(Map.class); job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在运行以上代码时,需要在命令行中指定输入文件和输出文件的路径。例如: ``` $ hadoop jar MaxMinSum.jar input output ``` 其中,input为包含数字的文本文件路径,output为输出结果的文件夹路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值