MapReduce数据处理两表join连接

来源:互联网 发布:三轴加速度传感器算法 编辑:程序博客网 时间:2024/04/29 21:26

现在这里有两个text文档,需要把它合并成一个文档,并且里面的数据不能有冗余..

[plain] view plaincopy
  1. <strong><span style="color:#FF0000;">info.txt文件:</span></strong>  
  2.   
  3. 1003 kaka  
  4. 1004 da  
  5. 1005 jue  
  6. 1006 zhao  

[plain] view plaincopy
  1. <span style="color:#FF0000;"><strong>cpdata.txt文件:</strong></span>  
  2.   
  3. 201001 1003 abc  
  4. 201002 1005 def  
  5. 201003 1006 ghi  
  6. 201004 1003 jkl  
  7. 201005 1004 mno  
  8. 201006 1005 pqr  
  9. 201001 1003 abc  
  10. 201004 1003 jkl  
  11. 201006 1005 mno  
  12. 200113 1007 zkl  

生成文件:

[plain] view plaincopy
  1. 1003    201001 abc kaka  
  2. 1003    201004 jkl kaka  
  3. 1004    201005 mno da  
  4. 1005    201002 def jue  
  5. 1005    201006 pqr jue  
  6. 1005    201006 mno jue  
  7. 1006    201003 ghi zhao  

这里先申明下,这个纯属个人想法,如果有跟好的方法可以告诉我

   因为info.txt文档的第一个字段与cpdata.txt的第二个字段是相同的,所以我把他们做为key值,这样通过Map他们就会组合了.去冗余,

              主要是用了个List记录已经读取过的变量,如果有一样的就不读取了.

代码如下:

[java] view plaincopy
  1. public class Advanced extends Configured implements Tool {  
  2.   
  3.     public static class AdMap extends Mapper<LongWritable, Text, Text, TextPair>{  
  4.   
  5.         @Override  
  6.         protected void map(LongWritable key, Text value, Context context)  
  7.                 throws IOException, InterruptedException {  
  8.             // TODO Auto-generated method stub  
  9. //          String filePath = ((FileSplit)context.getInputSplit()).getPath().toString();  
  10.             Text word = new Text();  
  11.               
  12.             String line = value.toString();  
  13.             String[] childline = line.split(" ");    //以空格截取  
  14.                    //判断是哪一张表,其实个人觉得这样判断还不合理,可以使用上面注视掉的获取路径值来判断  
  15.                                if(childline.length == 3){  
  16.                      TextPair pair = new TextPair();  
  17.                      pair.setFlag("0");         //这是个标识   0.表示 cpdata.txt     1表示info.txt  
  18.                      pair.setKey(childline[1]);  
  19.                      pair.setValue(childline[0]+" "+childline[2]);  
  20.                      pair.setContent(pair.toString());  
  21.                      word.clear();  
  22.                      word.set(pair.getKey());  
  23.                                          context.write(word, pair);        //传递一个对象要实现WritableComparable接口  
  24.                 }else{  
  25.                     TextPair pair = new  TextPair();  
  26.                     pair.setFlag("1");  
  27.                     pair.setKey(childline[0]);  
  28.                     pair.setValue(childline[1]);  
  29.                     pair.setContent(pair.toString());  
  30.                     word.clear();  
  31.                     word.set(pair.getKey());  
  32.                     context.write(word, pair);  
  33.                 }  
  34.               
  35.               
  36.         }  
  37.           
  38.     }  
  39.       
  40.     public static class AdReduce extends Reducer<Text, TextPair, Text, Text>{  
  41.   
  42.         @Override  
  43.         public void reduce(Text key, Iterable<TextPair> values,  
  44.                 Context context)  
  45.                 throws IOException, InterruptedException {  
  46.             // TODO Auto-generated method stub  
  47.                         //list0装载的都是cpdata的数据,list1装载的是info的数据  
  48.                         List<Text> list0 = new ArrayList<Text>();       
  49.             List<Text> list1 = new ArrayList<Text>();  
  50.             Iterator<TextPair> it = values.iterator();  
  51.             TextPair pair = new TextPair();  
  52.             while(it.hasNext()){  
  53.                     pair = it.next();  
  54.                 if("1".equals(pair.getFlag()))  
  55.                     list1.add(new Text(pair.getValue()));  
  56.                 else   
  57.                     list0.add(new Text(pair.getValue()));  
  58.             }  
  59.               
  60.             List<Text> sublist = new ArrayList<Text>();       //sublist用来添加已经写过的数据,然后再判断,如果存在就不用操作  
  61.             for(int i = 0 ; i<list1.size(); i++){  
  62.                 for(int j = 0 ;j<list0.size();j++){  
  63.                     if(!sublist.contains(list0.get(j))){  
  64.                         sublist.add(list0.get(j));  
  65.                         context.write(key, new Text(list0.get(j)+" " +list1.get(i)));  
  66.                     }  
  67.                 }  
  68.             }  
  69.               
  70.               
  71.         }  
  72.           
  73.     }  
  74.       
  75.       
  76.       
  77.       
  78.       
  79.       
  80.     /** 
  81.      * @param args 
  82.      */  
  83.     public static void main(String[] args) {  
  84.         try {  
  85.                        int res = ToolRunner.run(new Configuration(), new Advanced(), args);  
  86.             System.exit(res);  
  87.         } catch (Exception e) {  
  88.             // TODO Auto-generated catch block  
  89.             e.printStackTrace();  
  90.         }  
  91.     }  
  92.   
  93.     @Override  
  94.     public int run(String[] args) throws Exception {  
  95.         Configuration conf = new Configuration();  
  96.           
  97.         FileSystem fs = FileSystem.get(conf);  
  98.         if(fs.exists(new Path(args[2]))){  
  99.                         //如果文件已近存在就删除文件  
  100. //          System.out.println("error : file is exists");  
  101. //          System.exit(-1);  
  102.             fs.delete(new Path(args[2]), true);  
  103.         }  
  104.           
  105.         Job job = new Job(conf , "Advanced");  
  106.         job.setJarByClass(Advanced.class);  
  107.         job.setMapperClass(AdMap.class);  
  108.         job.setReducerClass(AdReduce.class);  
  109.           
  110.         job.setMapOutputKeyClass(Text.class);  
  111.         job.setMapOutputValueClass(TextPair.class);  
  112.           
  113.         job.setOutputKeyClass(Text.class);  
  114.         job.setOutputValueClass(Text.class);  
  115.           
  116.         FileInputFormat.setInputPaths(job, new Path(args[0]),new Path(args[1]));  
  117.         FileOutputFormat.setOutputPath(job, new Path(args[2]));  
  118.         return job.waitForCompletion(true) ? 0 : 1;  
  119.     }  
  120.       
  121.       
  122.       
  123.     public static class TextPair implements WritableComparable<TextPair>{  
  124.   
  125.         public String getValue() {  
  126.             return value;  
  127.         }  
  128.   
  129.         public void setValue(String value) {  
  130.             this.value = value;  
  131.         }  
  132.   
  133.         @Override  
  134.         public String toString() {  
  135.             return " " + key +" "+ value;   
  136.         }  
  137.   
  138.         public String getFlag() {  
  139.             return flag;  
  140.         }  
  141.   
  142.         public void setFlag(String flag) {  
  143.             this.flag = flag;  
  144.         }  
  145.   
  146.         public String getKey() {  
  147.             return key;  
  148.         }  
  149.   
  150.         public void setKey(String key) {  
  151.             this.key = key;  
  152.         }  
  153.   
  154.         public String getContent() {  
  155.             return content;  
  156.         }  
  157.   
  158.         public void setContent(String content) {  
  159.             this.content = content;  
  160.         }  
  161.   
  162.         private String flag = "";  
  163.         private String key ="";  
  164.         private String value ="";  
  165.         private String content = "";  
  166.           
  167.           
  168.   
  169.         public TextPair(String flag, String key, String value, String content) {  
  170.             this.flag = flag;  
  171.             this.key = key;  
  172.             this.value = value;  
  173.             this.content = content;  
  174.         }  
  175.   
  176.         public TextPair() {  
  177.         }  
  178.   
  179.         @Override  
  180.         public void write(DataOutput out) throws IOException {  
  181.             // TODO Auto-generated method stub  
  182.             out.writeUTF(this.flag);  
  183.             out.writeUTF(this.key);  
  184.             out.writeUTF(this.value);  
  185.             out.writeUTF(this.content);  
  186.         }  
  187.   
  188.         @Override  
  189.         public void readFields(DataInput in) throws IOException {  
  190.             // TODO Auto-generated method stub  
  191.             this.flag = in.readUTF();  
  192.             this.key = in.readUTF();  
  193.             this.value = in.readUTF();  
  194.             this.content = in.readUTF();  
  195.         }  
  196.   
  197.         @Override  
  198.         public int compareTo(TextPair o) {  
  199.             // TODO Auto-generated method stub  
  200.             return 0;  
  201.         }  
  202.           
  203.           
  204.     }  
  205.       
  206.       
  207.       
  208.   
  209. }  
0 0
原创粉丝点击