Hadoop自定义Writable实现二次排序

26 篇文章 0 订阅

输入数据集

20,75,cqu
25,90,cqnu
20,70,cqupt
24,80,cquk

二次排序功能

先按第一列数字排序,再按第二列数字排序

输出结果

20,70 cqupt
20,75 cqu
24,80 cquk
25,90 cqnu

实现原理

因为MapReduce的输出是按key排序的,所以,我们可以自定义一个key,这个key包含第一列和第二列。在实现compareTo方法时,先按第一列排序,再按第二列排序。

实现代码

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

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.io.WritableComparable;
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.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class TestJoin {

    public static class IntPairWritable implements WritableComparable<IntPairWritable>{
        private IntWritable first;
        IntWritable second;
        public IntPairWritable(){
            set(new IntWritable(), new IntWritable());
        }
        public void set(IntWritable first, IntWritable second){
            this.first = first;
            this.second = second;
        }
        @Override
        public void write(DataOutput out) throws IOException {
            this.first.write(out);
            this.second.write(out);
        }
        @Override
        public void readFields(DataInput in) throws IOException {
            this.first.readFields(in);
            this.second.readFields(in);
        }
        @Override
        public int compareTo(IntPairWritable o) {
            int result = this.first.compareTo(o.first);
            if(result != 0)
                return result;
            return this.second.compareTo(o.second);
        }
        public boolean equals(Object o){
            if(o instanceof IntPairWritable){
                IntPairWritable obj = (IntPairWritable)o;
                return this.first.equals(obj.first) && this.second.equals(obj.second);
            }
            return false;
        }
        public String toString(){
            return this.first.toString() + "," + this.second.toString();
        }
    }
    public static class SecondSortMapper extends Mapper<LongWritable, Text , IntPairWritable , Text>{
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
            String array[] = value.toString().split(",");
            IntPairWritable keyPair = new IntPairWritable();
            keyPair.set(new IntWritable(Integer.valueOf(array[0])),new IntWritable( Integer.valueOf(array[1])));
            context.write(keyPair, new Text(array[2]));
        }
    }
    public static class TestJoinReducer extends Reducer<IntPairWritable, Text, IntPairWritable, Text>{

        public void reduce(IntPairWritable key, Iterable<Text> value, Context context) throws IOException, InterruptedException{
            for(Text v : value){
                context.write(key, v);
            }
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if(args.length < 2){
            System.out.println("args must be three");return ;
        }
        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf,"TestJoin");
        job.setJarByClass(TestJoin.class);

        job.setMapperClass(SecondSortMapper.class);
        job.setReducerClass(TestJoinReducer.class);

        job.setOutputKeyClass(IntPairWritable.class);  //
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[2]));

        System.exit(job.waitForCompletion(true)?0:1);

    }

}

注意事项

在自定义Writable时,要实现的方法有read,write,compareTo三个方法。
除此之外,还要重写toString方法,以便于key可以输出内容。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值