dashu

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

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

/**
 * 作业:求最大的100个值
 *
 */
public class top100 {
    
    static final String INPUT = "hdfs://192.168.56.100:9000/input";
    static final String OUT = "hdfs://192.168.56.100:9000/out";
    
    static final Path INPUT_PATH = new Path(INPUT);
    static final Path OUT_PATH = new Path(OUT);
    
    static final int topNum = 100;
    
    public static void main(String[] args) throws Exception{
        
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(OUT),conf);
        if(fileSystem.exists(OUT_PATH)){
            fileSystem.delete(OUT_PATH,true);
        }
        
        Job job = new Job(conf,top100
        		.class.getSimpleName());
        FileInputFormat.setInputPaths(job, INPUT_PATH);
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(NullWritable.class);
        FileOutputFormat.setOutputPath(job, OUT_PATH);
        job.waitForCompletion(true);
    }
    
    static class MyMapper extends Mapper<LongWritable, Text, LongWritable, NullWritable>{
        @Override
        protected void map(
                LongWritable key,
                Text value,
                Mapper<LongWritable, Text, LongWritable, NullWritable>.Context context)
                throws IOException, InterruptedException {
            context.write(new LongWritable(Long.parseLong(value.toString())), NullWritable.get());
        }
    }
    
    static class MyReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable>{
        long[] topArray = new long[topNum];
        int count = 0;
        @Override
        protected void reduce(
                LongWritable k2,
                Iterable<NullWritable> v2s,
                Reducer<LongWritable, NullWritable, LongWritable, NullWritable>.Context context)
                throws IOException, InterruptedException {
            long num = Long.parseLong(k2.toString());
            if(count < topNum){
                topArray[count] = num;
                count++;
            }else{
                Arrays.sort(topArray);
                if(num > topArray[0]){
                    topArray[0] = num;
                }
            }
        }
        @Override
        protected void cleanup(
                Reducer<LongWritable, NullWritable, LongWritable, NullWritable>.Context context)
                throws IOException, InterruptedException {
            Arrays.sort(topArray);
            for(int i = topArray.length -1 ; i > -1 ; i--){                
                context.write(new LongWritable(topArray[i]), NullWritable.get());
            }
        }
    }
    
    
}

猜你喜欢

转载自blog.csdn.net/qq_38362772/article/details/80210098