你好,游客 登录 注册 搜索
背景:
阅读新闻

利用MapReduce解决在海量数据中求Top K个数

[日期:2012-05-13] 来源:Linux社区  作者:jtlyuan [字体:大 中 小]
利用MapReduce求海量数据中最大的K个数
  1. package jtlyuan.csdn;  
  2. import java.io.IOException;  
  3. import org.apache.Hadoop.conf.Configuration;  
  4. import org.apache.hadoop.conf.Configured;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.io.LongWritable;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.Mapper;  
  11. import org.apache.hadoop.mapreduce.Reducer;  
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  13. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  16. import org.apache.hadoop.util.Tool;  
  17. import org.apache.hadoop.util.ToolRunner;  
  18. //利用MapReduce求最大值海量数据中的K个数   
  19. public class TopKNum extends Configured implements Tool {  
  20. public static class MapClass extends Mapper<LongWritable, Text, IntWritable, IntWritable> {  
  21. public static final int K = 100;  
  22. private int[] top = new int[K];  
  23. public void map(LongWritable key, Text value, Context context)  
  24. throws IOException, InterruptedException {  
  25. String[] str = value.toString().split(",", -2);  
  26. try {// 对于非数字字符我们忽略掉   
  27. int temp = Integer.parseInt(str[8]);  
  28. add(temp);  
  29. } catch (NumberFormatException e) {  
  30. }  
  31. }  
  32. private void add(int temp) {//实现插入   
  33. if(temp>top[0]){  
  34. top[0]=temp;  
  35. int i=0;  
  36. for(;i<99&&temp>top[i+1];i++){  
  37. top[i]=top[i+1];  
  38. }  
  39. top[i]=temp;  
  40. }  
  41. }  
  42. @Override  
  43. protected void cleanup(Context context) throws IOException,  
  44. InterruptedException {  
  45. for(int i=0;i<100;i++){  
  46. context.write(new IntWritable(top[i]), new IntWritable(top[i]));  
  47. }  
  48. }  
  49. }  
  50. public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {  
  51. public static final int K = 100;  
  52. private int[] top = new int[K];  
  53. public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)  
  54. throws IOException, InterruptedException {  
  55. for (IntWritable val : values) {  
  56. add(val.get());  
  57. }  
  58. }  
  59. private void add(int temp) {//实现插入if(temp>top[0]){   
  60. top[0]=temp;  
  61. int i=0;  
  62. for(;i<99&&temp>top[i+1];i++){  
  63. top[i]=top[i+1];  
  64. }  
  65. top[i]=temp;  
  66. }  
  67. }  
  68. @Override  
  69. protected void cleanup(Context context) throws IOException,  
  70. InterruptedException {  
  71. for(int i=0;i<100;i++){  
  72. context.write(new IntWritable(top[i]), new IntWritable(top[i]));  
  73. }  
  74. }  
  75. }  
  76. public int run(String[] args) throws Exception {  
  77. Configuration conf = getConf();  
  78. Job job = new Job(conf, "TopKNum");  
  79. job.setJarByClass(TopKNum.class);  
  80. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  81. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  82. job.setMapperClass(MapClass.class);  
  83. job.setCombinerClass(Reduce.class);  
  84. job.setReducerClass(Reduce.class);  
  85. job.setInputFormatClass(TextInputFormat.class);  
  86. job.setOutputFormatClass(TextOutputFormat.class);  
  87. job.setOutputKeyClass(IntWritable.class);  
  88. job.setOutputValueClass(IntWritable.class);  
  89. System.exit(job.waitForCompletion(true) ? 0 : 1);  
  90. return 0;  
  91. }  
  92. public static void main(String[] args) throws Exception {  
  93. int res = ToolRunner.run(new Configuration(), new TopKNum(), args);  
  94. System.exit(res);  
  95. }  
  96. }  
  97. /* 
  98. * 列举一部分出来: 
  99. * 306 306 
  100. 307 307 
  101. 309 309 
  102. 313 313 
  103. 320 320 
  104. 346 346 
  105. 348 348 
  106. 393 393 
  107. 394 394 
  108. 472 472 
  109. 642 642 
  110. 706 706 
  111. 868 868 
  112. */  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款