Hadoop自定义排序和自定义数据类型使用(setSortComparatorClass和setGroupingComparatorClass)

1    Mapper

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class SortMapper extends Mapper<Object, Text, TextInt, IntWritable>{  
  2.   
  3.     public TextInt textInt = new TextInt();  
  4.     public IntWritable intp = new IntWritable(0);  
  5.       
  6.     @Override  
  7.     protected void map(Object key, Text value,  
  8.             Context context)  
  9.             throws IOException, InterruptedException {  
  10.         int i =  Integer.parseInt(value.toString());  
  11.           
  12.         textInt.setStr(key.toString());  
  13.         textInt.setValue(i);  
  14.         intp.set(i);  
  15.         context.write(textInt,intp);  
  16.     }  
  17.   
  18. }  

2   Partitioner

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class SortPartitioner extends Partitioner<TextInt, IntWritable>{  
  2.   
  3.     @Override  
  4.     public int getPartition(TextInt textInt, IntWritable value, int numReducers) {  
  5.             return textInt.getStr().hashCode() & Integer.MAX_VALUE % numReducers;  
  6.     }  
  7. }  

3    Reducer

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class SortReducer extends Reducer<TextInt, IntWritable, Text, Text>{  
  2.   
  3.     @Override  
  4.     protected void reduce(TextInt textInt, Iterable<IntWritable> values,  
  5.             Context context)  
  6.             throws IOException, InterruptedException {  
  7.           
  8.         StringBuffer stringCombine = new StringBuffer();          
  9.         Iterator<IntWritable> itr = values.iterator();  
  10.         while(itr.hasNext())  
  11.         {  
  12.             int value = itr.next().get();  
  13.             stringCombine.append(value + ",");  
  14.         }  
  15.         int length = stringCombine.length();  
  16.         if(length > 0)  
  17.             stringCombine.deleteCharAt(length - 1);  
  18.         context.write(new Text(textInt.getStr()), new Text(stringCombine.toString()));  
  19.     }  
  20. }  

4    自定义数据类型TextInt

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class TextInt implements WritableComparable<TextInt> {  
  2.       
  3.     private String str;  
  4.     private int value;  
  5.       
  6.     public String getStr() {  
  7.         return str;  
  8.     }  
  9.     public void setStr(String str) {  
  10.         this.str = str;  
  11.     }  
  12.     public int getValue() {  
  13.         return value;  
  14.     }  
  15.     public void setValue(int value) {  
  16.         this.value = value;  
  17.     }  
  18.     @Override  
  19.     public void readFields(DataInput in) throws IOException {  
  20.         str = in.readUTF();  
  21.         value = in.readInt();  
  22.     }  
  23.     @Override  
  24.     public void write(DataOutput out) throws IOException {  
  25.         out.writeUTF(str);  
  26.         out.writeInt(value);  
  27.     }  
  28.     @Override  
  29.     public int compareTo(TextInt o) {  
  30.         return o.getStr().compareTo(this.getStr());  
  31.     }  
  32.       
  33. }  

6    自定义的Mapper端的排序比较类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class TextIntComparator extends WritableComparator{  
  2.   
  3.     public TextIntComparator(){  
  4.         super(TextInt.classtrue);  
  5.     }  
  6.       
  7.     @Override  
  8.     @SuppressWarnings("all")  
  9.     public int compare(WritableComparable a, WritableComparable b) {  
  10.         TextInt o1 = (TextInt) a;  
  11.         TextInt o2 = (TextInt) b;  
  12.           
  13.         if(! o1.getStr().equals(o2.getStr()))  
  14.             return o1.getStr().compareTo(o2.getStr());  
  15.         else   
  16.             return o1.getValue() - o2.getValue();  
  17.     }  
  18. }  

7    自定义的Reducer端的排序比较类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class TextComparator extends WritableComparator{  
  2.     public TextComparator(){  
  3.         super(TextInt.classtrue);  
  4.     }  
  5.   
  6.     @Override  
  7.     @SuppressWarnings("all")  
  8.       
  9.     public int compare(WritableComparable a, WritableComparable b) {  
  10.         TextInt o1 = (TextInt) a;  
  11.         TextInt o2 = (TextInt) b;  
  12.         return o1.getStr().compareTo(o2.getStr());  
  13.     }  
  14. }  


8    驱动程序

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class SortMain  {  
  2.     public static void main(String[] args) throws IOException{  
  3.           
  4.         Configuration conf = new Configuration();  
  5.         String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();  
  6.         if(otherArgs.length !=2 )  
  7.         {  
  8.             System.err.println("Usage:sort <int><out>");  
  9.             System.exit(2);  
  10.         }  
  11.         Job job = new Job(conf,"sort");  
  12.         job.setJarByClass(SortMain.class);  
  13.         job.setInputFormatClass(KeyValueTextInputFormat.class);  
  14.         job.setMapperClass(SortMapper.class);  
  15.         job.setPartitionerClass(SortPartitioner.class);  
  16.         job.setMapOutputKeyClass(TextInt.class);  
  17.         job.setMapOutputValueClass(IntWritable.class);  
  18.         job.setReducerClass(SortReducer.class);  
  19.         job.setOutputKeyClass(Text.class);  
  20.         job.setOutputValueClass(Text.class);  
  21.         job.setSortComparatorClass(TextIntComparator.class);  
  22.         job.setGroupingComparatorClass(TextComparator.class);  
  23.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  24.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  25.         try {  
  26.             System.exit(job.waitForCompletion(true) ? 0 : 1);  
  27.         } catch (InterruptedException e) {  
  28.             e.printStackTrace();  
  29.         } catch (ClassNotFoundException e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  

9    运行和效果

注意:必须得在驱动程序中设置setMapperOutputKey和setMApperOutputValue,默认的是mapper输出value和key类型是Text和Text。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值