MapReduce实现QQ好友推荐

大家都知道qq用户量上亿,每个用户又有很多的好友,因此,数据量十分的庞大,如何才能实现QQ的好友推荐呢? 
下面举一个例子: 
A有QQ好友B 
B有QQ好友C 
则A,C有可能是好友。 

当A登录的时候,则会向A推荐C,当C登录的时候,则会向C推荐A。

[java]  view plain  copy
  1. package com.FriendsRecommended.findFrends;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.URI;  
  5. import java.net.URISyntaxException;  
  6. import java.util.HashSet;  
  7. import java.util.Iterator;  
  8. import java.util.Set;  
  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.   
  21. /** 
  22.  * @author Robin 
  23.  * qq好友推荐 
  24.  * Hadoop好友hello,hello好友是world...依次类推。 
  25.  * 那么hadoop和world有共同的好友hello,所以hadoop和world可能具有好友关系, 
  26.  * world就是hadoop的推荐好友,hadoop也是world的好友推荐。 
  27.  * 计算出qq文件内符合上述条件的推荐好友 
  28.  *  
  29.  * 好友文件 :friends.txt 
  30.  * hadoop hello   
  31.  * hdfs world   
  32.  * tom cat   
  33.  * cat dog   
  34.  * hello world   
  35.  * hello hdfs   
  36.  */  
  37. public class FindFriends {  
  38.     /*  
  39.      *  Mapping结果:  
  40.      *  hadoop hello  
  41.      *  hello hadoop  
  42.         hdfs world  
  43.         world hdfs  
  44.         tom cat  
  45.         cat tom  
  46.         cat dog  
  47.         dog cat  
  48.         hello world  
  49.         world hello  
  50.         hello hdfs  
  51.         hdfs hello  
  52.      */    
  53.     public static class FindFriendsMapper extends Mapper<LongWritable, Text, Text, Text> {  
  54.         @Override  
  55.         protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)  
  56.                 throws IOException, InterruptedException {  
  57.             String line = value.toString();  
  58.             String array[] = line.split("\\s+");  
  59.             context.write(new Text(array[0]), new Text(array[1]));  
  60.             context.write(new Text(array[1]), new Text(array[0]));  
  61.         }  
  62.     }  
  63.       
  64.     /* 
  65.      *maping之后reduce之前,Shuffling进行洗牌,把相同key的整理在一起(默认的shuffling),结果如下:  
  66.      *  
  67.      * 洗牌结果(输入到reduce):   
  68.      *hadoop hello   
  69.      * 
  70.      *hello hadoop   
  71.      *hello world   
  72.      *hello hdfs   
  73.      * 
  74.      *hdfs world   
  75.      *hdfs hello   
  76.      * 
  77.      *tom cat   
  78.      * 
  79.      *cat tom   
  80.      *cat dog   
  81.      * 
  82.      *dog cat   
  83.      */  
  84.       
  85.     // Reducing进行笛卡尔乘积计算  
  86.     public static class FindFriendsReduce extends Reducer<Text, Text, Text, Text> {  
  87.         @Override  
  88.         protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)  
  89.                 throws IOException, InterruptedException {  
  90.             // 去重  
  91.             Set<String> set = new HashSet<String>();  
  92.             for (Text v : values) {  
  93.                 set.add(v.toString());  
  94.             }  
  95.               
  96.             if (set.size() > 1) {  
  97.                 for (Iterator<String> i = set.iterator(); i.hasNext();) {  
  98.                     String qqName = i.next();  
  99.                     for (Iterator<String> j = set.iterator(); j.hasNext();) {  
  100.                         String otherQqName = j.next();  
  101.                         if (!qqName.equals(otherQqName)) {  
  102.                             context.write(new Text(qqName), new Text(otherQqName));  
  103.                         }  
  104.                     }  
  105.                 }  
  106.             }  
  107.         }  
  108.     }  
  109.       
  110.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {  
  111.         final String INPUT_PATH = "hdfs://192.168.24.203:9000/user/FriendsRecommended/FriendsRecommended.txt";  
  112.         final String OUTPUT_PATH = "hdfs://192.168.24.203:9000/user/FriendsRecommended/out";  
  113.          /** 
  114.          * Configuration:map/reduce的j配置类,向hadoop框架描述map-reduce执行的工作 
  115.          */  
  116.         Configuration conf = new Configuration();  
  117.           
  118.         final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);  
  119.         if(fileSystem.exists(new Path(OUTPUT_PATH))) {  
  120.             fileSystem.delete(new Path(OUTPUT_PATH), true);  
  121.         }  
  122.           
  123.         Job job = Job.getInstance(conf, "FindFriends"); //设置一个用户定义的job名称  
  124.         job.setJarByClass(FindFriends.class);  
  125.         job.setMapperClass(FindFriendsMapper.class); //为job设置Mapper类  
  126. //      job.setCombinerClass(IntSumReducer.class);    //为job设置Combiner类  
  127.         job.setReducerClass(FindFriendsReduce.class); //为job设置Reducer类  
  128.         job.setOutputKeyClass(Text.class);        //为job的输出数据设置Key类  
  129.         job.setOutputValueClass(Text.class);    //为job输出设置value类  
  130.           
  131.         FileInputFormat.addInputPath(job, new Path(INPUT_PATH));  
  132.         FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));  
  133.   
  134.         System.exit(job.waitForCompletion(true) ?0 : 1);        //运行job  
  135.     }  
  136.   
  137. }  

最后计算结果:

[java]  view plain  copy
  1. tom dog  
  2. dog tom  
  3. hello   world  
  4. world   hello  
  5. hdfs    world  
  6. hdfs    hadoop  
  7. world   hdfs  
  8. world   hadoop  
  9. hadoop hdfs  
  10. hadoop world  
  11. hello   hdfs  
  12. hdfs    hello  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值