[转帖]Hadoop二次排序_Hadoop,ERP及大数据讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Hadoop,ERP及大数据讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3035 | 回复: 0   主题: [转帖]Hadoop二次排序        下一篇 
jie.liang
注册用户
等级:少校
经验:1003
发帖:77
精华:0
注册:2013-10-11
状态:离线
发送短消息息给jie.liang 加好友    发送短消息息给jie.liang 发消息
发表于: IP:您无权察看 2013-10-17 17:08:39 | [全部帖] [楼主帖] 楼主

Hadoop二次排序:

  1. import java.io.IOException;    


  1. import org.apache.Hadoop.conf.Configuration; 
  2. import org.apache.Hadoop.fs.Path; 
  3. import org.apache.Hadoop.io.IntWritable; 
  4. import org.apache.Hadoop.io.LongWritable; 
  5. import org.apache.Hadoop.io.Text; 
  6. import org.apache.Hadoop.io.WritableComparable; 
  7. import org.apache.Hadoop.io.WritableComparator; 
  8. import org.apache.Hadoop.mapreduce.Job; 
  9. import org.apache.Hadoop.mapreduce.Mapper; 
  10. import org.apache.Hadoop.mapreduce.Reducer; 
  11. import org.apache.Hadoop.mapreduce.lib.input.FileInputFormat; 
  12. import org.apache.Hadoop.mapreduce.lib.input.TextInputFormat; 
  13. import org.apache.Hadoop.mapreduce.lib.output.FileOutputFormat; 
  14. import org.apache.Hadoop.mapreduce.lib.output.TextOutputFormat; 
  15. import org.apache.Hadoop.mapreduce.lib.partition.HashPartitioner; 
  16. /** 
  17.  * @author 吕桂强 
  18.  * @email larry.lv.word@gmail.com 
  19.  * @version 创建时间:2012-5-21 下午5:06:57 
  20.  */ 
  21. public class SecondarySort { 
  22.        // map阶段的最后会对整个map的List进行分区,每个分区映射到一个reducer 
  23.        public static class FirstPartitioner extends HashPartitioner<Text, IntWritable> { 
  24.              @Override 
  25.              public int getPartition(Text key, IntWritable value, int numPartitions) { 
  26.                    return (key.toString().split(":")[0].hashCode() & Integer.MAX_VALUE) % numPartitions; 
  27.              } 
  28.        } 
  29.       
  30.        // 每个分区内又调用job.setSortComparatorClass或者key的比较函数进行排序 
  31.        public static class SortComparator extends WritableComparator { 
  32.              protected SortComparator() { 
  33.                    super(Text.class, true); 
  34.              } 
  35.             
  36.              @SuppressWarnings("rawtypes") 
  37.              @Override 
  38.              public int compare(WritableComparable w1, WritableComparable w2) { 
  39.                    return -w1.toString().split(":")[0].compareTo(w2.toString().split(":")[0]); 
  40.              } 
  41.        } 
  42.       
  43.        // 只要这个比较器比较的两个key相同,他们就属于同一个组. 
  44.        // 它们的value放在一个value迭代器,而这个迭代器的key使用属于同一个组的所有key的第一个key 
  45.        public static class GroupingComparator extends WritableComparator { 
  46.              protected GroupingComparator() { 
  47.                    super(Text.class, true); 
  48.              } 
  49.              @SuppressWarnings("rawtypes") 
  50.              @Override 
  51.              public int compare(WritableComparable w1, WritableComparable w2) { 
  52.                    return w1.toString().split(":")[0].compareTo(w2.toString().split(":")[0]); 
  53.              } 
  54.        } 
  55.       
  56.        // 自定义map 
  57.        public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { 
  58.              private final IntWritable intvalue = new IntWritable(); 
  59.             
  60.              public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
  61.                    context.write(value, intvalue); 
  62.              } 
  63.        } 
  64.       
  65.        // 自定义reduce 
  66.        public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { 
  67.              public void setup(Context context) { 
  68.                    context.getConfiguration(); 
  69.                    System.out.println("reduce"); 
  70.              } 
  71.             
  72.              public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { 
  73.                    context.write(new Text("-------------------------"), new IntWritable(1)); 
  74.                    for (IntWritable val : values) { 
  75.                          // 虽然分在同一个组里,但是循环里每次输出的key都不相同(key看上去是个Text但实际也是一个list) 
  76.                          context.write(key, val); 
  77.                    } 
  78.              } 
  79.        } 
  80.       
  81.        public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { 
  82.              Configuration conf = new Configuration(); 
  83.              Job job = new Job(conf, "secondarysort"); 
  84.              job.setJarByClass(SecondarySort.class); 
  85.              job.setMapperClass(Map.class); 
  86.              // job.setCombinerClass(Reduce.class); 
  87.              job.setReducerClass(Reduce.class); 
  88.              // 分区函数 
  89.              job.setPartitionerClass(FirstPartitioner.class); 
  90.              job.setSortComparatorClass(SortComparator.class); 
  91.              // 分组函数 
  92.              job.setGroupingComparatorClass(GroupingComparator.class); 
  93.             
  94.              job.setMapOutputKeyClass(Text.class); 
  95.              job.setMapOutputValueClass(IntWritable.class); 
  96.              job.setOutputKeyClass(Text.class); 
  97.              job.setOutputValueClass(IntWritable.class); 
  98.             
  99.              job.setInputFormatClass(TextInputFormat.class); 
  100.              job.setOutputFormatClass(TextOutputFormat.class); 
  101.             
  102.              FileInputFormat.setInputPaths(job, new Path("/larry/wc/input")); 
  103.              FileOutputFormat.setOutputPath(job, new Path("/larry/wc/output")); 
  104.             
  105.              job.setNumReduceTasks(1); 
  106.              System.exit(job.waitForCompletion(true) ? 0 : 1); 
  107.        } 

输入:

1:3
1:2
1:1
2:1
2:2
2:3
3:1
3:2
3:3


输出:(Text类型的key每输出一次都会改变,所以其实也是个Iterable)

____________________ 1
3:1 0
3:2 0
3:3 0
____________________ 1
2:1 0
2:2 0
2:3 0
____________________ 1
1:3 0
1:2 0
1:1 0




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论