mapreduce(排序)

在mapreduce中默认排序(正序),本文通过新建keyCompartor实现倒序

可以发现其中方法可以对数据进行去重

sortLaunch

public class SortLaunch {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        //log4j中日志打印
        BasicConfigurator.configure();
        //通过job实例获取job对象
        Job job      = Job.getInstance();
        //设置启动类
        job.setJarByClass(SortLaunch.class);
        //设置名字
        job.setJobName("sort");
​
        //配置Mapper
        job.setMapperClass(SortMapper.class);
        //配置reduce
        job.setReducerClass(SortReduce.class);
        //配置key比较器
        job.setSortComparatorClass(KeyCompartor.class);
​
        //配置Mapper(key)输出类型
        job.setMapOutputKeyClass(IntWritable.class);
        //配置Mapper(value)输出类型
        job.setMapOutputValueClass(NullWritable.class);
        //配置key输出类型
        job.setOutputKeyClass(IntWritable.class);
        //配置value输出类型
        job.setOutputValueClass(NullWritable.class);
​
        //通过job配置获取FileSystem
        FileSystem fs = FileSystem.get(job.getConfiguration());
        //获取文件输出路径
        Path out      = new Path("D:/ideaProjects/hadoop_pro/sort1/output");
        if (fs.exists(out)){
            fs.delete(out,true);
        }
​
        //获取文件输入路径
        FileInputFormat.addInputPath(job,new Path("D:/ideaProjects/hadoop_pro/sort1/input"));
        //设置文件输出路径
        FileOutputFormat.setOutputPath(job,out);
​
        //设置reduce数量
        job.setNumReduceTasks(1);
        //提交job
        job.waitForCompletion(true);
​
    }
}

sortMapper

public class SortMapper extends Mapper<LongWritable, Text, IntWritable, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, IntWritable, NullWritable>.Context context) throws IOException, InterruptedException {
        IntWritable outKey = new IntWritable();
        String line        = value.toString();
        outKey.set(Integer.parseInt(line));
        context.write(outKey, NullWritable.get());
    }
}

sortReduce

public class SortReduce extends Reducer<IntWritable, NullWritable,IntWritable,NullWritable> {
    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Reducer<IntWritable, NullWritable, IntWritable, NullWritable>.Context context) throws IOException, InterruptedException {
        for (NullWritable value : values) {
            context.write(key,NullWritable.get());
        }
​
    }
}

keyCompartor

public class KeyCompartor extends WritableComparator {
    public KeyCompartor(){
        super(IntWritable.class,true);
    }
​
    /**
     *
     * @param a
     * @param b
     * @return  0 相等   1 大于   -1  小于
     */
    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        IntWritable left   = (IntWritable) a;
        IntWritable right  = (IntWritable) b;
​
        return -(left.compareTo(right));
    }
}

intput

15
46
42
1
32
33
3
3
4

output

46
42
33
32
15
4
3
3
1

去重

SortMapper
public class SortMapper extends Mapper<LongWritable, Text,IntWritable, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, IntWritable, NullWritable>.Context context) throws IOException, InterruptedException {
       IntWritable k =new IntWritable();
        String line = value.toString();
        k.set(Integer.parseInt(line));
        context.write(k,NullWritable.get());
    }
}
​
SortReduce
public class SortReduce extends Reducer<IntWritable, NullWritable,IntWritable,NullWritable> {
    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Reducer<IntWritable, NullWritable, IntWritable, NullWritable>.Context context) throws IOException, InterruptedException {
​
            context.write(key,NullWritable.get());
​
​
    }
}
​
​
SortLaunch
    
public class SortLaunch {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        BasicConfigurator.configure();
        Job job = Job.getInstance();
        job.setJarByClass(SortLaunch.class);
        job.setJobName("sort");
​
        job.setMapperClass(SortMapper.class);
        job.setReducerClass(SortReduce.class);
​
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(NullWritable.class);
​
        FileSystem fs = FileSystem.get(job.getConfiguration());
        Path out = new Path("D:/ideaProjects/hadoop_pro/quchong/output");
        if (fs.exists(out)){
            fs.delete(out,true);
        }
​
        FileInputFormat.addInputPath(job,new Path("D:/ideaProjects/hadoop_pro/quchong/input"));
        FileOutputFormat.setOutputPath(job,out);
​
        job.setNumReduceTasks(1);
        job.waitForCompletion(true);
​
    }
}
​

input

15
46
42
1
32
33
3
3
4

output

1
3
4
15
32
33
42
46


 

  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MapReduce中,排序是非常重要的。MapReduceMap和Reduce的两个阶段中都会执行排序操作。全局排序是指在一个MapReduce程序产生的输出文件中,所有的结果都是按照某个策略进行排序的,例如降序还是升序。在全局排序中,只有一个reduce任务可以保证数据的全局有序,但这样无法充分利用Hadoop集群的优势。 在MapReduce的shuffle过程中,通常会执行多次排序。首先是在Map输出阶段,根据分区和key进行快速排序。然后,在Map的合并溢写文件阶段,将同一个分区的多个溢写文件进行归并排序,合成一个大的溢写文件。最后,在Reduce输入阶段,将同一分区来自不同Map任务的数据文件进行归并排序。最后阶段使用了堆排作为最后的合并过程。 在MapReduce中,有两种排序方式,即快速排序和归并排序。快速排序是通过一趟排序将要排序的数据分割成独立的两部分,然后对这两部分数据分别进行快速排序,最终达到整个数据变成有序序列的目的。归并排序是建立在归并操作上的一种排序算法,通过将已有序的子序列合并,得到完全有序的序列。归并排序可以采用分治法的方式进行,将子序列逐步合并,最终得到整个序列的有序结果。 因此,MapReduce中的排序操作是通过多次排序和归并的方式来实现的,以确保数据的有序性。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值