实现TOP K(选做):统计sogou500w中,发关键字次数最多的 *前20名用户UID和发关键字次数。

package day0917;

import java.io.IOException;
import java.util.TreeMap;
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;

public class TopK {
    /*实现TOP K(选做):统计sogou500w中,发关键字次数最多的
     *前20名用户UID和发关键字次数。
     *
     * 思路:
     *(1)得到每个关键字的次数
     *(2)对其进行排序
     *(3)选出Top20
     *关键如何选出前二十
      *使用reduce中cleanup()方法能得出是较为快捷的解决方式
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
         Path inputPath = new Path(args[0]);
         //args[1]="http:/master:9000:/out-1";
         Path outputPath = new Path(args[1]);
        
         @SuppressWarnings("deprecation")
         Job  job = new Job(new Configuration(),TopK.class.getName());
         //FileSystem  fs = new FileSystem(new Configuration(),, encoding)
         job.setJarByClass(TopK.class);
         job.setMapperClass(TopMapper.class);
         job.setReducerClass(TopReducer.class);
        
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(LongWritable.class);

         FileInputFormat.addInputPath(job, inputPath);
         FileOutputFormat.setOutputPath(job, outputPath);
        
         job.waitForCompletion(true);
    }
    
    public static class TopMapper  extends Mapper<LongWritable, Text, Text, LongWritable>{
        Text text =new Text();
        
        @Override
        protected void map(LongWritable key, Text value,Context context)
                throws IOException, InterruptedException {
           String[] line= value.toString().split("\t");
           String keys = line[2];
           text.set(keys);
           context.write(text,new LongWritable(1));
        }
    }
    
    public static class TopReducer extends Reducer< Text,LongWritable, Text, LongWritable>{
        Text text = new Text();
        TreeMap<Integer,String > map = new TreeMap<Integer,String>();
        @Override
        protected void reduce(Text key, Iterable<LongWritable> value, Context context)
                throws IOException, InterruptedException {    
            
            int sum=0;//key出现次数
            
            for (LongWritable ltext : value) {
                sum+=ltext.get();
            }
            map.put(sum,key.toString());    
            //去前20条数据
            if(map.size()>20){
                map.remove(map.firstKey());
            }
        }
        
        @Override
        protected void cleanup(Context context)
                throws IOException, InterruptedException {
            // TODO Auto-generated method stub
            for(Integer count:map.keySet()){
                context.write(new Text(map.get(count)), new LongWritable(count));
            }
        }    
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值