MapReduce案例(数据中获取最大值TopN)

案例:

案列:

data.txt

10 9 8 7 6 5 1 2 3 4 11 12 13 14 15 20 19 18 17 16 

package squencefile;

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

public class TopN {
    public static class MyMapper extends Mapper<LongWritable,Text,NullWritable,LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String words = value.toString();
            String[] wordArr = words.split(" ");
            for(String word:wordArr){
                context.write(NullWritable.get(),new LongWritable(Long.parseLong(word)));
            }
        }
    }
    public static class MyReducer extends Reducer<NullWritable,LongWritable,NullWritable,LongWritable>{
        @Override
        protected void reduce(NullWritable key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            //使用TreeMap按照key进行排序
            TreeMap<Long,String> treeMap=new TreeMap<>();
            for(LongWritable valTmp:values){
                Long value = valTmp.get();
                //将<数字,"">放入treeMap中进行排序
                treeMap.put(value,"");
                if(treeMap.size()>3){
                    //因为treeMap默认是按照key升序排序,所以第一项就是小值,直接删除第一项即可
                    treeMap.remove(treeMap.firstKey());
                }
            }
            //输出treeMap中的前三个
            for(Long word:treeMap.keySet()){
                context.write(NullWritable.get(),new LongWritable(word));
            }
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //创建一个job,也就是一个运行环境
        Configuration conf=new Configuration();
        //集群运行
//        conf.set("fs.defaultFS","hdfs://hadoop:8088");
        //本地运行
        Job job=Job.getInstance(conf,"TopN");
        //程序入口(打jar包)
        job.setJarByClass(TopN.class);

        //需要输入文件:输入文件
        FileInputFormat.addInputPath(job,new Path("F:\\filnk_package\\hadoop-2.10.1\\data\\test7\\data.txt"));
        //编写mapper处理逻辑
        job.setMapperClass(TopN.MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //shuffle流程
        //对局部进行排序,结果交给reducer进行处理
//        job.setCombinerClass(MyReducer.class);
        //编写reduce处理逻辑
        job.setReducerClass(TopN.MyReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //输出文件
        FileOutputFormat.setOutputPath(job,new Path("F:\\filnk_package\\hadoop-2.10.1\\data\\test7\\out"));

        //运行job,需要放到Yarn上运行
        boolean result =job.waitForCompletion(true);
        System.out.print(result?1:0);

    }
}

 

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值