MapReduce实现之 查找共同好友

转载地址:http://blog.youkuaiyun.com/xuedingkai/article/details/78997928


输入:邻接表

[plain]  view plain  copy
  1. 100, 200 300 400 500 600  
  2. 200, 100 300 400  
  3. 300, 100 200 400 500  
  4. 400, 100 200 300  
  5. 500, 100 300  
  6. 600, 100  
第一列表示用户,后面的表示用户的好友。

需求:查找两两用户的共同好友。

思路:1、key为两两用户,value为其中一个用户的所有好友

            2、求两个用户所有好友的交集

步骤:1、map:取每一行,组合user和其任一好友为key(key中的两个字段按字典序排列),user的所有好友为value

            2、reduce:求两个用户之间好友的交集

[java]  view plain  copy
  1. package dabook;  
  2.   
  3. import hadoop.FriendRecom;  
  4.   
  5. import java.io.IOException;  
  6. import java.util.ArrayList;  
  7. import java.util.Set;  
  8. import java.util.TreeSet;  
  9.   
  10. import org.apache.hadoop.conf.Configuration;  
  11. import org.apache.hadoop.fs.FileSystem;  
  12. import org.apache.hadoop.fs.Path;  
  13. import org.apache.hadoop.io.LongWritable;  
  14. import org.apache.hadoop.io.Text;  
  15. import org.apache.hadoop.mapreduce.Job;  
  16. import org.apache.hadoop.mapreduce.Mapper;  
  17. import org.apache.hadoop.mapreduce.Reducer;  
  18. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  19. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  20. import org.apache.hadoop.util.GenericOptionsParser;  
  21.   
  22. public class CommFriend {  
  23.       
  24.       
  25.     public static Set<String> intersect(Set<String> set1, Set<String> set2){  
  26.         if(set1==null || set2 == null){  
  27.             return null;  
  28.         }  
  29.           
  30.         Set<String> result = new TreeSet<String>();  
  31.           
  32.         Set<String> small = null;  
  33.         Set<String> big = null;  
  34.         if(set1.size() < set2.size()){  
  35.             small = set1;  
  36.             big = set2;  
  37.         }  
  38.         else {  
  39.             small = set2;  
  40.             big = set1;  
  41.         }  
  42.           
  43.         for (String String : small) {  
  44.             if(big.contains(String)){  
  45.                 result.add(String);  
  46.             }  
  47.         }  
  48.         return result;  
  49.     }  
  50.       
  51.       
  52.       
  53.     static class MyMapper extends Mapper<LongWritable, Text, Text, Text>{  
  54.           
  55.         private static Text outKey = new Text();  
  56.         private static Text outValue = new Text();  
  57.           
  58.         @Override  
  59.         protected void map(LongWritable key, Text value, Context context)  
  60.                 throws IOException, InterruptedException {  
  61.             String [] input = value.toString().split(",");  
  62.             if(input.length != 2){  
  63.                 return;  
  64.             }  
  65.               
  66.             outValue.set(input[1]);  
  67.             String [] sz = input[1].split(" ");  
  68.             for (String string : sz) {  
  69.                 if(input[0].compareTo(string) < 0){  
  70.                     outKey.set("[" + input[0] + ", " + string + "]");  
  71.                 }  
  72.                 else {  
  73.                     outKey.set("[" + string + ", " + input[0] + "]");  
  74.                 }  
  75.                 context.write(outKey, outValue);  
  76.             }  
  77.         }  
  78.           
  79.     }  
  80.       
  81.     static class MyReducer extends Reducer<Text, Text, Text, Text>{  
  82.           
  83.         private Text outKey = new Text();  
  84.         private Text outValue = new Text();  
  85.           
  86.         @Override  
  87.         protected void reduce(Text key, Iterable<Text> value, Context context)  
  88.                 throws IOException, InterruptedException {  
  89.               
  90.             int len = 0;  
  91.             Set<String> set1 = new TreeSet<String>();  
  92.             Set<String> set2 = new TreeSet<String>();  
  93.             ArrayList<String> arrayList = new ArrayList<String>();  
  94.             for (Text text : value) {  
  95.                 arrayList.add(text.toString());  
  96.                 len++;  
  97.             }  
  98.               
  99.             if(len != 2){  
  100.                 return;  
  101.             }  
  102.               
  103.             String [] sz = arrayList.get(0).split(" ");  
  104.             for (String s : sz) {  
  105.                 set1.add(s);  
  106.             }  
  107.               
  108.             sz = arrayList.get(1).trim().split(" ");  
  109.             for (String s : sz) {  
  110.                 set2.add(s);  
  111.             }  
  112.               
  113.             Set<String> res = intersect(set1, set2);  
  114.             if(res == null){  
  115.                 return;  
  116.             }  
  117.             StringBuilder sb = new StringBuilder();  
  118.             for (String s : res) {  
  119.                 sb.append(s + ", ");  
  120.             }  
  121.               
  122.             String substring = null;  
  123.             if(sb.length() > 1){  
  124.                 substring = sb.substring(0, sb.length()-2);  
  125.             }  
  126.               
  127.             if(substring != null){  
  128.                 this.outValue.set(substring);  
  129.                 context.write(key, outValue);  
  130.             }  
  131.         }  
  132.     }  
  133.       
  134.     private static String inputPath = "dabook/commfriend";  
  135.     private static String outputPath = "dabook/commfriend-out";  
  136.   
  137.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
  138.         Configuration conf = new Configuration();  
  139.         String[] otherArgs = new GenericOptionsParser(conf, args)  
  140.                 .getRemainingArgs();  
  141.         Job job = new Job(conf, "common friend");  
  142.         job.setJarByClass(CommFriend.class);  
  143.         job.setMapperClass(MyMapper.class);  
  144.         job.setReducerClass(MyReducer.class);  
  145.   
  146.         job.setMapOutputKeyClass(Text.class);  
  147.         job.setMapOutputValueClass(Text.class);  
  148.         job.setOutputKeyClass(Text.class);  
  149.         job.setOutputValueClass(Text.class);  
  150.         FileSystem fs = FileSystem.get(conf);  
  151.         Path inPath = new Path(inputPath);  
  152.         if (fs.exists(inPath)) {  
  153.             FileInputFormat.addInputPath(job, inPath);  
  154.         }  
  155.   
  156.         Path outPath = new Path(outputPath);  
  157.         fs.delete(outPath, true);  
  158.         FileOutputFormat.setOutputPath(job, outPath);  
  159.   
  160.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  161.   
  162.           
  163.           
  164.           
  165.     }  
  166.       
  167.     private void test(){  
  168.         Set<String> set1 = new TreeSet<String>();  
  169.         Set<String> set2 = new TreeSet<String>();  
  170.           
  171.         set2.add("2");  
  172.         set2.add("3");  
  173.         set2.add("4");  
  174.         set2.add("5");  
  175.           
  176.         set1.add("3");  
  177.         set1.add("4");  
  178.         set1.add("6");  
  179.           
  180.         Set<String> res = intersect(set1, set2);  
  181.           
  182.         for (String string : res) {  
  183.             System.out.println(string);  
  184.         }  
  185.           
  186.     }  
  187.   
  188. }  
输出:

[plain]  view plain  copy
  1. [100, 200]  300, 400  
  2. [100, 300]  200, 400, 500  
  3. [100, 400]  200, 300  
  4. [100, 500]  300  
  5. [200, 300]  100, 400  
  6. [200, 400]  100, 300  
  7. [300, 400]  100, 200  
  8. [300, 500]  100  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值