自定义-Hadoop自定义分组Group

自定义分组MyGroup:


主要是继承WritableComparator类,重写compare函数


我这里重写的是该源码函数:
  1. /** Compare two WritableComparables. 
  2.    * 
  3.    * <p> The default implementation uses the natural ordering, calling {@link 
  4.    * Comparable#compareTo(Object)}. */  
  5.   @SuppressWarnings("unchecked")  
  6.   public int compare(WritableComparable a, WritableComparable b) {  
  7.     return a.compareTo(b);  
  8.   }  


原始文件数据:
  1. hadoop  a  
  2. spark   a  
  3. hive    a  
  4. hbase   a  
  5. tachyon a  
  6. storm   a  
  7. redis   a  


代码:
  1. import java.io.IOException;  
  2.   
  3.   
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.LongWritable;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.io.WritableComparable;  
  9. import org.apache.hadoop.io.WritableComparator;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  16. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  17. import org.apache.hadoop.util.GenericOptionsParser;  
  18.   
  19.   
  20. public class MyGroup {  
  21.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
  22.         Configuration conf = new Configuration();  
  23.         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  24.         if(otherArgs.length!=2){  
  25.             System.err.println("Usage databaseV1 <inputpath> <outputpath>");  
  26.         }  
  27.           
  28.         Job job = Job.getInstance(conf, MyGroup.class.getSimpleName() + "1");  
  29.         job.setJarByClass(MyGroup.class);  
  30.         job.setMapOutputKeyClass(Text.class);  
  31.         job.setMapOutputValueClass(Text.class);  
  32.         job.setOutputKeyClass(Text.class);  
  33.         job.setOutputValueClass(Text.class);  
  34.         job.setMapperClass(MyMapper1.class);  
  35.         job.setGroupingComparatorClass(MyGroupComparator.class);  
  36.         job.setReducerClass(MyReducer1.class);  
  37.         job.setInputFormatClass(TextInputFormat.class);  
  38.         job.setOutputFormatClass(TextOutputFormat.class);  
  39.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  40.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  41.         job.waitForCompletion(true);  
  42.     }  
  43.     public static class MyMapper1 extends Mapper<LongWritable, Text, Text, Text>{  
  44.         @Override  
  45.         protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)  
  46.                 throws IOException, InterruptedException {  
  47.             String[] spl=value.toString().split("\t");  
  48.             context.write(new Text(spl[0].trim()), new Text(spl[1].trim()));  
  49.         }  
  50.     }  
  51.     public static class MyReducer1 extends Reducer<Text, Text, Text, Text>{  
  52.         @Override  
  53.         protected void reduce(Text k2, Iterable<Text> v2s, Reducer<Text, Text, Text, Text>.Context context)  
  54.                 throws IOException, InterruptedException {  
  55.             Long count=0L;  
  56.             for (@SuppressWarnings("unused") Text v2 : v2s) {  
  57.                 count++;  
  58.                 context.write(new Text("in--"+k2), new Text(count.toString()));  
  59.             }  
  60.             context.write(new Text("out--"+k2), new Text(count.toString()));  
  61.         }  
  62.     }  
  63.     public static class MyGroupComparator extends WritableComparator{  
  64.         public MyGroupComparator(){  
  65.             super(Text.class,true);  
  66.         }  
  67.         @SuppressWarnings("rawtypes")  
  68.         public int compare(WritableComparable a, WritableComparable b) {  
  69.             Text p1 = (Text) a;  
  70.             Text p2 = (Text) b;  
  71.             p1.compareTo(p2);  
  72.             return  0;  
  73.           }  
  74.     }  
  75. }  


默认分组结果是这样的:
  1. public static class MyGroupComparator extends WritableComparator{  
  2.         public MyGroupComparator(){  
  3.             super(Text.class,true);  
  4.         }  
  5.         @SuppressWarnings("rawtypes")  
  6.         public int compare(WritableComparable a, WritableComparable b) {  
  7.             Text p1 = (Text) a;  
  8.             Text p2 = (Text) b;  
  9.             return p1.compareTo(p2);  
  10.           }  
  11.     }  


相同Key为一组:
  1. in--hadoop      1  
  2. out--hadoop     1  
  3. in--hbase       1  
  4. out--hbase      1  
  5. in--hive        1  
  6. out--hive       1  
  7. in--redis       1  
  8. out--redis      1  
  9. in--spark       1  
  10. out--spark      1  
  11. in--storm       1  
  12. out--storm      1  
  13. in--tachyon     1  
  14. out--tachyon    1  


自定义后:
  1. public static class MyGroupComparator extends WritableComparator{  
  2.         public MyGroupComparator(){  
  3.             super(Text.class,true);  
  4.         }  
  5.         @SuppressWarnings("rawtypes")  
  6.         public int compare(WritableComparable a, WritableComparable b) {  
  7.             Text p1 = (Text) a;  
  8.             Text p2 = (Text) b;  
  9.             p1.compareTo(p2);  
  10.             return 0;  
  11.           }  
  12.     }  


所有key均为一组:
  1. in--hadoop      1  
  2. in--hbase       2  
  3. in--hive        3  
  4. in--redis       4  
  5. in--spark       5  
  6. in--storm       6  
  7. in--tachyon     7  
  8. out--tachyon    7 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值