手机版
你好,游客 登录 注册
背景:
阅读新闻

关联规则二项集Hadoop实现

[日期:2012-11-08] 来源:Linux社区  作者:fansy1990 [字体:大 中 小]

在本阶段的Mapper的setup中读取frequence file到一个LinkedHashSet(可以保持原始的插入顺序)中,然后在map中针对一个事务输出这个LinkedHashSet,不过限制输出是在这个事务中出现的项目而已。

第三步:步骤4和步骤5,代码如下:

OutRules.java

  1. package org.fansy.date1108.fpgrowth.twodimension;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map.Entry;
  6. import java.util.Stack;
  7. import java.util.TreeSet;
  8. import org.apache.Hadoop.conf.Configuration;
  9. import org.apache.hadoop.fs.Path;
  10. import org.apache.hadoop.io.LongWritable;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.Reducer;
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  17. publicclass OutRules {
  18. publicstaticclass M extends Mapper<LongWritable,Text,Text,Text>{
  19. publicvoid map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{
  20. String str=value.toString();
  21. String[] s=str.split(",");
  22. if(s.length<=1){
  23. return;
  24. }
  25. Stack<String> stack=new Stack<String>();
  26. for(int i=0;i<s.length;i++){
  27. stack.push(s[i]);
  28. }
  29. int num=str.length();
  30. while(stack.size()>1){
  31. num=num-2;
  32. context.write(new Text(stack.pop()),new Text(str.substring(0,num)));
  33. }
  34. }
  35. }
  36. // Reducer
  37. publicstaticclass R extends Reducer<Text ,Text,Text,Text>{
  38. privateint minConfidence=0;
  39. publicvoid setup(Context context){
  40. String str=context.getConfiguration().get("MIN");
  41. try {
  42. minConfidence=Integer.parseInt(str);
  43. } catch (NumberFormatException e) {
  44. // TODO Auto-generated catch block
  45. minConfidence=3;
  46. }
  47. }
  48. publicvoid reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
  49. HashMap<String,Integer> hm=new HashMap<String ,Integer>();
  50. for(Text v:values){
  51. String[] str=v.toString().split(",");
  52. for(int i=0;i<str.length;i++){
  53. if(hm.containsKey(str[i])){
  54. int temp=hm.get(str[i]);
  55. hm.put(str[i], temp+1);
  56. }else{
  57. hm.put(str[i], 1);
  58. }
  59. }
  60. }
  61. // end of for
  62. TreeSet<String> sss=new TreeSet<String>(new MyComparator(" "));
  63. Iterator<Entry<String,Integer>> iter=hm.entrySet().iterator();
  64. while(iter.hasNext()){
  65. Entry<String,Integer> k=iter.next();
  66. if(k.getValue()>minConfidence&&!key.toString().equals(k.getKey())){
  67. sss.add(k.getKey()+" "+k.getValue());
  68. }
  69. }
  70. Iterator<String> iters=sss.iterator();
  71. StringBuffer sb=new StringBuffer();
  72. while(iters.hasNext()){
  73. sb.append(iters.next()+"|");
  74. }
  75. context.write(key, new Text(":\t"+sb.toString()));
  76. }
  77. }
  78. publicstaticvoid main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  79. // TODO Auto-generated method stub
  80. if(args.length!=3){
  81. System.out.println("Usage: <input><output><min_confidence>");
  82. System.exit(1);
  83. }
  84. String input=args[0];
  85. String output=args[1];
  86. String minConfidence=args[2];
  87. Configuration conf=new Configuration();
  88. conf.set("MIN", minConfidence);
  89. Job job=new Job(conf,"the out rules job");
  90. job.setJarByClass(OutRules.class);
  91. job.setMapperClass(M.class);
  92. job.setNumReduceTasks(1);
  93. job.setReducerClass(R.class);
  94. job.setOutputKeyClass(Text.class);
  95. job.setOutputValueClass(Text.class);
  96. FileInputFormat.setInputPaths(job, new Path(input));
  97. FileOutputFormat.setOutputPath(job, new Path(output));
  98. boolean succeed=job.waitForCompletion(true);
  99. if(succeed){
  100. System.out.println(job.getJobName()+" succeed ... ");
  101. }
  102. }
  103. }
linux
相关资讯       Hadoop  Mahout  关联规则  Hadoop实现 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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