用过各种社交平台(如QQ、微博、朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图)。它的算法主要是根据你们之间的共同好友数进行推荐,当然也有其他如爱好、特长等等。共同好友的数量越多,表明你们可能认识,系统便会自动推荐。今天我将向大家介绍如何使用MapReduce计算共同好友

算法

  1. 假设有以下好友列表,A的好友有B,C,D,F,E,O; B的好友有A,C,E,K 以此类推
  2. 那我们要如何算出A-O用户每个用户之间的共同好友呢?
  1. A:B,C,D,F,E,O
  2. B:A,C,E,K
  3. C:F,A,D,I
  4. D:A,E,F,L
  5. E:B,C,D,M,L
  6. F:A,B,C,D,E,O,M
  7. G:A,C,D,E,F
  8. H:A,C,D,E,O
  9. I:A,O
  10. J:B,O
  11. K:A,C,D
  12. L:D,E,F
  13. M:E,F,G
  14. O:A,H,I,J
  1. 下面我们将演示分步计算,思路主要如下:先算出某个用户是哪些用户的共同好友,
  2. AB,C,D,E等的共同好友。遍历B,C,D,E两两配对如(B-C共同好友A,注意防止重复B-C,C-B)作为key放松给reduce端,
  3. 这样reduce就会收到所有B-C的共同好友的集合。可能到这里你还不太清楚怎么回事,下面我给大家画一个图。

代码演示

由上可知,此次计算由两步组成,因此需要两个MapReduce程序先后执行

  1. 第一步:通过mapreduce得到 某个用户是哪些用户的共同好友。
  1. public class FriendsDemoStepOneDriver {
  2. static class FriendsDemoStepOneMapper extends Mapper<LongWritable, Text, Text, Text> {
  3. @Override
  4. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  5. String line = value.toString();
  6. String[] split = line.split(":");
  7. String user = split[0];
  8. String[] friends = split[1].split(",");
  9. for (String friend : friends) {
  10. // 输出友人,人。 这样的就可以得到哪个人是哪些人的共同朋友
  11. context.write(new Text(friend),new Text(user));
  12. }
  13. }
  14. }
  15. static class FriendsDemoStepOneReducer extends Reducer<Text,Text,Text,Text>{
  16. @Override
  17. protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
  18. StringBuilder sb = new StringBuilder();
  19. for (Text person : values) {
  20. sb.append(person+",");
  21. }
  22. context.write(key,new Text(sb.toString()));
  23. }
  24. }
  25. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  26. Configuration conf = new Configuration();
  27. conf.set("mapreduce.framework.name","local");
  28. conf.set("fs.defaultFS","file:///");
  29. Job job = Job.getInstance(conf);
  30. job.setJarByClass(FriendsDemoStepOneDriver.class);
  31. job.setMapperClass(FriendsDemoStepOneMapper.class);
  32. job.setReducerClass(FriendsDemoStepOneReducer.class);
  33. job.setOutputKeyClass(Text.class);
  34. job.setOutputValueClass(Text.class);
  35. FileInputFormat.setInputPaths(job,new Path("/Users/kris/Downloads/mapreduce/friends/friends.txt"));
  36. FileOutputFormat.setOutputPath(job,new Path("/Users/kris/Downloads/mapreduce/friends/output"));
  37. boolean completion = job.waitForCompletion(true);
  38. System.out.println(completion);
  39. }
  40. }
  1. 运行的到的结果如下:

由图可见:I,K,C,B,G,F,H,O,D都有好友A;A,F,J,E都有好友B。接下来我们只需组合这些拥有相同的好友的用户,

作为key发送给reduce,由reduce端聚合d得到所有共同的好友

  1. /**
  2. * 遍历有同个好友的用户的列表进行组合,得到两人的共同好友
  3. */
  4. public class FriendsDemoStepTwoDriver {
  5. static class FriendDemoStepTwoMapper extends Mapper<LongWritable, Text, Text, Text> {
  6. @Override
  7. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  8. String line = value.toString();
  9. String[] split = line.split("\t");
  10. String friend = split[0];
  11. String[] persons = split[1].split(",");
  12. Arrays.sort(persons);
  13. for (int i = 0; i < persons.length-1; i++) {
  14. for (int i1 = i+1; i1 < persons.length; i1++) {
  15. context.write(new Text(persons[i]+"--"+persons[i1]),new Text(friend));
  16. }
  17. }
  18. }
  19. }
  20. static class FriendDemoStepTwoReducer extends Reducer<Text, Text, Text, Text> {
  21. @Override
  22. protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
  23. StringBuilder sb = new StringBuilder();
  24. for (Text friend : values) {
  25. sb.append(friend + ",");
  26. }
  27. context.write(key,new Text(sb.toString()));
  28. }
  29. }
  30. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  31. Configuration conf = new Configuration();
  32. conf.set("mapreduce.framework.name","local");
  33. conf.set("fs.defaultFS","file:///");
  34. Job job = Job.getInstance(conf);
  35. job.setJarByClass(FriendsDemoStepOneDriver.class);
  36. job.setMapperClass(FriendDemoStepTwoMapper.class);
  37. job.setReducerClass(FriendDemoStepTwoReducer.class);
  38. job.setOutputKeyClass(Text.class);
  39. job.setOutputValueClass(Text.class);
  40. FileInputFormat.setInputPaths(job,new Path("/Users/kris/Downloads/mapreduce/friends/output"));
  41. FileOutputFormat.setOutputPath(job,new Path("/Users/kris/Downloads/mapreduce/friends/output2"));
  42. boolean completion = job.waitForCompletion(true);
  43. System.out.println(completion);
  44. }
  45. }
  1. 得到的结果如下:

如图,我们就得到了拥有共同好友的用户列表及其对应关系,在实际场景中再根据用户关系(如是否已经是好友)进行过滤,在前端展示,就形成了我们所看到"可能认识"或者"好友推荐"啦~

  1. 今天给大家分享的好友推荐算法就是这些,今天的只是一个小小的案例,现实场景中的运算肯定要比这个复杂的多,
  2. 但是思路和方向基本一致,如果有更好的建议或算法,欢迎与小吴一起讨论喔~
  3. 如果您喜欢这篇文章的话记得like,share,comment喔(^^)

MapReduce案例-好友推荐的更多相关文章

  1. 【大数据系列】MapReduce示例好友推荐

    package org.slp; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import ...

  2. MapReduce -- 好友推荐

    MapReduce实现好友推荐: 张三的好友有王五.小红.赵六; 同样王五.小红.赵六的共同好友是张三; 在王五和小红不认识的前提下,可以通过张三互相认识,给王五推荐的好友为小红, 给小红推荐的好友是 ...

  3. 19-hadoop-fof好友推荐

    好友推荐的案例, 需要两个job, 第一个进行好友关系度计算, 第二个job将计算的关系进行推荐 1, fof关系类 package com.wenbronk.friend; import org.a ...

  4. 吴裕雄--天生自然HADOOP操作实验学习笔记:qq好友推荐算法

    实验目的 初步认识图计算的知识点 复习mapreduce的知识点,复习自定义排序分组的方法 学会设计mapreduce程序解决实际问题 实验原理 QQ好友推荐算法是所有推荐算法中思路最简单的,我们利用 ...

  5. mapreduce案例:获取PI的值

    mapreduce案例:获取PI的值 * content:核心思想是向以(0,0),(0,1),(1,0),(1,1)为顶点的正方形中投掷随机点. * 统计(0.5,0.5)为圆心的单位圆中落点占总落 ...

  6. 【Hadoop离线基础总结】MapReduce案例之自定义groupingComparator

    MapReduce案例之自定义groupingComparator 求取Top 1的数据 需求 求出每一个订单中成交金额最大的一笔交易 订单id 商品id 成交金额 Order_0000005 Pdt ...

  7. 【Hadoop学习之十】MapReduce案例分析二-好友推荐

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 最应该推荐的好友TopN,如何排名 ...

  8. MapReduce案例二:好友推荐

    1.需求 推荐好友的好友 图1: 2.解决思路 3.代码 3.1MyFoF类代码 说明: 该类定义了所加载的配置,以及执行的map,reduce程序所需要加载运行的类 package com.hado ...

  9. 【尚学堂·Hadoop学习】MapReduce案例2--好友推荐

    案例描述 根据好友列表,推荐好友的好友 数据集 tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive ...

随机推荐

  1. 2019NC#8

    题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A All-one Matrices 点击查看 单调栈+前缀和 326/2017  通过 B Beauty Values 点击查看 进入讨论 8 ...

  2. POJ-2153Colored Sticks解题报告+欧拉回路,字典树,并查集;

    传送门:http://poj.org/problem?id=2513 题意:给你许多木棍,木棍两端都有颜色,问能不能首尾相接,要求颜色相同. 参考:https://www.cnblogs.com/ku ...

  3. “玲珑杯”ACM比赛 Round #11 B -- 萌萌哒的第二题

    DESCRIPTION 一条东西走向的河两边有都排着工厂,北边有n间工厂A提供原材料,南边有n间工厂B进行生产.现在需要在工厂A和工厂B之间建运输桥以减少运输成本.可是每个工厂B只能接受最多6个工厂A ...

  4. Atcoder C - Closed Rooms(思维+bfs)

    题目链接:http://agc014.contest.atcoder.jp/tasks/agc014_c 题意:略. 题解:第一遍bfs找到所有可以走的点并标记步数,看一下最少几步到达所有没锁的点,然 ...

  5. pt工具校验主从数据一致性之dsns方式

    mysql主从数据一致性校验,常用的方法是Percona-Toolkit的组件pt-table-checksum,这东西怎么用网上一大堆,就不啰嗦了.主要说一下通过dsns方式发现从库的一种方式. p ...

  6. Java虚拟机原理和调优

    https://blog.csdn.net/sun1021873926/article/details/78002118 115道Java经典面试题(面中率最高.最全) 史上最全 40 道 Dubbo ...

  7. math库的使用

    math库简介 math库是Python提供的内置数学内函数库,因为复数类型常用于科学计算,一般计算并不常用,因此math库不支持复数类型,仅支持整数和浮点数运算,math库一共提供4个数学常数和44 ...

  8. idea报错 Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource

    核对一下控制器是不是写了相同的路径...org.springframework.beans.factory.BeanCreationException: Error creating bean wit ...

  9. C++ 基础中的基础 ---- 引用

    C++ 基础中的基础 ---- 引用 引用的概念:引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字.一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量.比如: int n ...

  10. Javaweb设置session过期时间

    在Java Web开发中,Session为我们提供了很多方便,Session是由浏览器和服务器之间维护的.Session超时理解为:浏览器和服务器之间创建了一个Session,由于客户端长时间(休眠时 ...