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

关联规则二项集Hadoop实现

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

步骤1的实现其实就是最简单的wordcount程序的实现,在步骤2中涉及到HDFS文件的读取以及写入。在生成frequence list file时排序时用到了PriorityQueue类,同时自定义了一个类用来定义排序规则;

第二步:步骤3,代码如下:

SortAndCut.java:

  1. package org.fansy.date1108.fpgrowth.twodimension;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.URI;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.LinkedHashSet;
  9. import java.util.Set;
  10. import java.util.regex.Pattern;
  11. import org.apache.Hadoop.conf.Configuration;
  12. import org.apache.hadoop.fs.FSDataInputStream;
  13. import org.apache.hadoop.fs.FileSystem;
  14. import org.apache.hadoop.fs.Path;
  15. import org.apache.hadoop.io.LongWritable;
  16. import org.apache.hadoop.io.NullWritable;
  17. import org.apache.hadoop.io.Text;
  18. import org.apache.hadoop.mapreduce.Job;
  19. import org.apache.hadoop.mapreduce.Mapper;
  20. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  21. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  22. publicclass SortAndCut {
  23. /**
  24. * sort and cut the items
  25. */
  26. publicstaticclass M extends Mapper<LongWritable,Text,NullWritable,Text>{
  27. private LinkedHashSet<String> list=new LinkedHashSet<String>();
  28. private Pattern splitter=Pattern.compile("[ ]*[ ,|\t]");
  29. publicvoid setup(Context context) throws IOException{
  30. String input=context.getConfiguration().get("FLIST");
  31. FileSystem fs=FileSystem.get(URI.create(input),context.getConfiguration());
  32. Path path=new Path(input);
  33. FSDataInputStream in1=fs.open(path);
  34. InputStreamReader isr1=new InputStreamReader(in1);
  35. BufferedReader br=new BufferedReader(isr1);
  36. String line;
  37. while((line=br.readLine())!=null){
  38. String[] str=line.split("\t");
  39. if(str.length>0){
  40. list.add(str[0]);
  41. }
  42. }
  43. }
  44. // map
  45. publicvoid map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{
  46. String [] items=splitter.split(value.toString());
  47. Set<String> set=new HashSet<String>();
  48. set.clear();
  49. for(String s:items){
  50. set.add(s);
  51. }
  52. Iterator<String> iter=list.iterator();
  53. StringBuffer sb=new StringBuffer();
  54. sb.setLength(0);
  55. int num=0;
  56. while(iter.hasNext()){
  57. String item=iter.next();
  58. if(set.contains(item)){
  59. sb.append(item+",");
  60. num++;
  61. }
  62. }
  63. if(num>0){
  64. context.write(NullWritable.get(), new Text(sb.toString()));
  65. }
  66. }
  67. }
  68. publicstaticvoid main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  69. // TODO Auto-generated method stub
  70. if(args.length!=3){
  71. System.out.println("Usage: <input><output><fListPath>");
  72. System.exit(1);
  73. }
  74. String input=args[0];
  75. String output=args[1];
  76. String fListPath=args[2];
  77. Configuration conf=new Configuration();
  78. conf.set("FLIST", fListPath);
  79. Job job=new Job(conf,"the sort and cut the items job");
  80. job.setJarByClass(SortAndCut.class);
  81. job.setMapperClass(M.class);
  82. job.setNumReduceTasks(0);
  83. job.setOutputKeyClass(NullWritable.class);
  84. job.setOutputValueClass(Text.class);
  85. FileInputFormat.setInputPaths(job, new Path(input));
  86. FileOutputFormat.setOutputPath(job, new Path(output));
  87. boolean succeed=job.waitForCompletion(true);
  88. if(succeed){
  89. System.out.println(job.getJobName()+" succeed ... ");
  90. }
  91. }
  92. }
linux
相关资讯       Hadoop  Mahout  关联规则  Hadoop实现 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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