一、算法说明

好友关系如图:

1、直接相连的表示两个人是直接好友关系;

2、两个人有相同的好友表示两个人是间接好友(当然可能两个人同时也是直接好友,如图hello和hive)。

3、好友推荐列表就是按照两个用户的共同好友数量排名

二、MapReduce分析

1、分两步MapReduce计算完成;

2、第一步先得到用户的间接好友关系数目,注意有直接好友关系的用户需要过滤掉;

3、第二步根据间接好友关系数就可以得到用户推荐列表。

三、MapReduce实现

输入数据

tom hello hadoop cat
word hadoop hello hive
cat tom hive
mr hive hello
hive cat hadoop word hello mr
hadoop tom hive word
hello tom word hive mr

第一个是当前用户,后面的是其好友列表

第一步——计算好友间接关系数

一、mapper输出两种数据

1、对各用户的好友列表好友俩俩组合,输出间接好友关系;

2、当前用户和好友列表好友一一组合,输出直接好友关系。

注意!!!这里好友关系key需要顺序排序,避免重复记录,如hello:tom和tom:hello都表示同样两个人的关系。

public class FriendMapper extends Mapper<LongWritable, Text, Text, IntWritable> {private final Text text = new Text();private final IntWritable mval = new IntWritable();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {//样本数据:tom hello hadoop catString[] users = StringUtils.split(value.toString(), ' ');for (int i = 1; i < users.length; i++) {//和好友组成直接关系,组合好友关系按顺序排列,确保user1和user2不会因为顺序问题,而被认为是两对关系text.set(MrCommUtil.orderConcat(users[0], users[i]));mval.set(0);context.write(text, mval);for (int j = i + 1; j < users.length; j++) {//列表好友俩俩组合间接关系text.set(MrCommUtil.orderConcat(users[i], users[j]));mval.set(1);context.write(text, mval);}}}}

二、reducer统计同两个用户的间接关系数,并过滤已经是直接关系的一组用户。

public class FriendReducer extends Reducer<Text, IntWritable, Text, IntWritable> {private final IntWritable rval = new IntWritable();@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {//数据样本//hadoop word 0//hadoop word  1//hadoop word  0int num = 0;for (IntWritable value : values) {if (value.get() == 0) {return;}num += 1;}rval.set(num);context.write(key, rval);}
}

三、输出结果集如下

cat:hadoop   2
cat:hello   2
cat:mr  1
cat:word    1
hadoop:hello    3
hadoop:mr   1
hive:tom    3
mr:tom  1
mr:word 2
tom:word    2

第二步——统计最佳推荐好友

一、mapper输入数据集为第一步的结果集,map把记录映射成正反两组

public class Friend2Mapper extends Mapper<LongWritable, Text, FriendRelation, IntWritable> {private final FriendRelation mkey = new FriendRelation();private final IntWritable mval = new IntWritable();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {//数据样本:cat:hadoop   2String[] strs = StringUtils.split(value.toString(), '\t');String[] users = StringUtils.split(strs[0], ':');mkey.setFrom(users[0]);mkey.setTo(users[1]);int n = Integer.parseInt(strs[1]);mkey.setN(n);mval.set(n);context.write(mkey, mval);mkey.setFrom(users[1]);mkey.setTo(users[0]);context.write(mkey, mval);}
}

二、排序比价器根据第一个用户和关系数倒排序

public class Friend2SortComparator extends WritableComparator {public Friend2SortComparator() {super(FriendRelation.class, true);}@Overridepublic int compare(WritableComparable a, WritableComparable b) {//排序先根据from,同from再根据n排倒序FriendRelation f1 = (FriendRelation) a;FriendRelation f2 = (FriendRelation) b;int i = f1.getFrom().compareTo(f2.getFrom());if (i == 0) {return -Integer.compare(f1.getN(), f2.getN());}return i;}
}

三、分组比较器根据第一个用户分组

public class Friend2GroupComparator extends WritableComparator {public Friend2GroupComparator() {super(FriendRelation.class, true);}@Overridepublic int compare(WritableComparable a, WritableComparable b) {//分组只根据from分组FriendRelation f1 = (FriendRelation) a;FriendRelation f2 = (FriendRelation) b;int i = f1.getFrom().compareTo(f2.getFrom());return i;}}

四、reduce取出关系数最大的推荐关系

public class Friend2Reducer extends Reducer<FriendRelation, IntWritable, Text, IntWritable> {private final Text rkev= new Text();private final IntWritable rval = new IntWritable();@Overrideprotected void reduce(FriendRelation key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {//样本数据://cat:hadoop    2// cat:hello   2// cat:mr  1// cat:word    1int n = key.getN();//输出最大间接关系数的所有推荐for (IntWritable value : values) {if (n != value.get()) {break;}rkev.set(key.getFrom() + "->" + key.getTo());rval.set(key.getN());context.write(rkev, rval);}}
}

五、输出最终结果,用户推荐分最高的前两名

cat->hello    2
cat->hadoop  2
hadoop->hello    3
hello->hadoop    3
hive->tom    3
mr->word 2
tom->hive    3
word->mr 2
word->tom    2

六、完整代码及测试数据详见码云:hadoop-test传送门

大数据Hadoop学习之——好友推荐相关推荐

  1. 大数据Hadoop学习之——TF-IDF算法实现

    一.算法说明 1.词频TF:是指给定词语在给定文件中出现的次数,一般会做归一化,即除以文件的总词数(注意是分词数,不是字数). TF=词在文章出现次数 / 文章的总词数 2.逆向文件频率IDF:普遍重 ...

  2. 大数据入门学习必读好书推荐,请收藏!

    身处于一个大数据时代,大数据无疑是近期最时髦的词汇了. 不管是云计算.社交网络,还是物联网.移动互联网和智慧城市,都要与大数据搭上联系. 随着云计算.移动互联网和物联网等新一代信息技术的创新和应用普及 ...

  3. 大数据Hadoop学习系列之Hadoop、Spark学习路线

    1 Java基础: 视频方面:推荐毕老师<毕向东JAVA基础视频教程>. 学习hadoop不需要过度的深入,java学习到javase,在多线程和并行化多多理解实践即可. 书籍方面:推荐李 ...

  4. 大数据Hadoop学习笔记01

    1.Google在大数据方面三篇论文: GFS----HDFS Map-Reduce---MR BigTable---HBase 2.Hadoop优势: 高可靠性.高扩展性.高效性.高容错性 3.Ha ...

  5. 大数据Hadoop学习之————基于物品的协同过滤算法实现物品推荐

    一.基础概念 协同过滤算法一般分为两种实现: 基于用户的协同过滤算法(userCF):通过寻找相似兴趣的其他用户,为指定用户推荐物品.比如用户A喜欢商品A.B,用户B也喜欢商品A和B,则可以认为用户A ...

  6. 8年京东大数据架构师推荐的大数据开发学习路线

    一.我们先要了解大数据的工作方向 01.大数据工程师 02.数据分析师 03.大数据科学家 04.其他(数据挖掘本质算是机器学习,不过和数据相关,也可以理解为大数据的一个方向吧) 二.大数据工程师的技 ...

  7. 大数据Hadoop教程-学习笔记01【大数据导论与Linux基础】

    视频教程:哔哩哔哩网站:黑马大数据Hadoop入门视频教程,总时长:14:22:04 教程资源:https://pan.baidu.com/s/1WYgyI3KgbzKzFD639lA-_g,提取码: ...

  8. 大数据Hadoop教程-学习笔记02【Apache Hadoop、HDFS】

    视频教程:哔哩哔哩网站:黑马大数据Hadoop入门视频教程 教程资源:https://pan.baidu.com/s/1WYgyI3KgbzKzFD639lA-_g 提取码: 6666 [P001-P ...

  9. 2018大数据培训学习路线图(详细完整版)

    2018大数据培训学习路线全课程目录+学习线路详解(详细完整版) 第一阶段:大数据基础Java语言基础阶段 1.1:Java开发介绍 1.1.1 Java的发展历史 1.1.2 Java的应用领域 1 ...

最新文章

  1. PTA团体程序设计天梯赛-L2-023 图着色问题
  2. NoneBot2插件——今日人品
  3. vbs打开软件光标停在第一个输入框_三维设计软件,3DMAX最全快捷键大全,赶快收藏哦...
  4. MATLAB学习笔记(十二)
  5. 三次样条插值matlab,Matlab关于三次样条插值
  6. idea插件安装在哪个目录_从零开始编写自己需要的IntelliJ IDEA 插件
  7. css中的背景、边框、补丁相关属性
  8. 云原生:云计算时代命题之终极解决方案
  9. GeForce RTX 3090驱动下载
  10. 今晚7:30 | 推荐系统中的异构关系学习——香港大学计算机学院助理教授黄超
  11. pdffactory 打印字体_PdfFactory(虚拟打印机)
  12. Android图片拼接
  13. 软件工程-第三章 软件需求分析1
  14. ‘XXXX’ was compiled with optimization - stepping may behave oddly; variables may not be available
  15. 快速解决Kubernetes从k8s.gcr.io仓库拉取镜像失败问题
  16. 自由职业接单,大平台,有保障
  17. 2013全年3GPP RAN1会议关于D2D(Device-to-Device)技术的提案分析
  18. G120变频器准备就绪和运行指示信号解析
  19. Harris角点特征提取和角点特征匹配(2)
  20. 【空指针异常,也不全是。】

热门文章

  1. 【Linux】Ubuntu命令
  2. 使用JSON格式传递数据,获取JSON的值
  3. HTML5+CSS大作业——明星薛之谦(7页面))带轮播特效
  4. SonarQube配置前端工程代码检测
  5. webbench 下载_webbench 压力测试软件
  6. 4个国内免费DNS服务商
  7. 科大奥瑞物理实验——干涉法测微小量
  8. 高数——常数变易法的补充
  9. 旧电脑装什么系统最快_旧电脑的福音:Win10精简版,运行比Win7更快,安装包不到3GB...
  10. jenkins配置钉钉机器人