Combiner应用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45526489/article/details/100847681
package MovieCount;

import Use.Rates.UserRateTopN;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.codehaus.jackson.map.ObjectMapper;


import java.io.IOException;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;


public class RateHotn {
    public static class RateHotMap extends Mapper<LongWritable, Text,Text, IntWritable>{
        //ObjectMapper是Jackson提供的一个类,作用是将java对象与json格式相互转化
        //ObjectMapper 通过 writeValue 系列方法 将 java 对 象序列化 为 json,并 将 json 存 储成不同的格式,String(writeValueAsString),Byte Array(writeValueAsString),Writer, File,OutStream 和 DataOutput。
        //ObjectMapper 通过 readValue 系列方法从不同的数据源像 String , Byte Array, Reader,File,URL, InputStream 将 json 反序列化为 java 对象。
        ObjectMapper objectMapper = new ObjectMapper();


        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            UserRateTopN userRateTopN = objectMapper.readValue(line, UserRateTopN.class);
            String movie  = userRateTopN.getMovie();
            context.write(new Text(movie),new IntWritable(1));
        }
    }
    public static class RateHotnReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
        TreeMap<IntWritable,Text>map;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            map = new TreeMap<>(new Comparator<IntWritable>(){

                @Override
                public int compare(IntWritable o1, IntWritable o2) {
                    return o2.compareTo(o1);
                }
            });
        }

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            Integer count = 0;
            for(IntWritable value:values){
                count = count + value.get();
            }
            map.put(new IntWritable(count),new Text(key));

        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            Configuration conf = context.getConfiguration();
            int suibian = conf.getInt("suibian",3);
             for(int i = 0;i<suibian;i++){
                 Map.Entry<IntWritable,Text> entry = map.pollFirstEntry();
                 IntWritable count = entry.getKey();
                 Text movie = entry.getValue();
                 context.write(movie,count);

             }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        conf.setInt("suibian",Integer.parseInt(args[0]));
        Job job = Job.getInstance(conf);
        job.setCombinerClass(RateHotnCombiner.class);
        job.setJarByClass(RateHotn.class);
        job.setMapperClass(RateHotMap.class);
        job.setReducerClass(RateHotnReduce.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,new Path(""));
        FileOutputFormat.setOutputPath(job,new Path(""));
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

package MovieCount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class RateHotnCombiner extends Reducer<Text, IntWritable,Text,IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        Integer count = 0;
        for(IntWritable value:values){
            count ++;

        }
        context.write(key,new IntWritable(count));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45526489/article/details/100847681