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

算法

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

代码演示

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

第一步:通过mapreduce得到 某个用户是哪些用户的共同好友。
public class FriendsDemoStepOneDriver {

    static class FriendsDemoStepOneMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString(); String[] split = line.split(":");
String user = split[0];
String[] friends = split[1].split(","); for (String friend : friends) {
// 输出友人,人。 这样的就可以得到哪个人是哪些人的共同朋友
context.write(new Text(friend),new Text(user));
}
}
} static class FriendsDemoStepOneReducer extends Reducer<Text,Text,Text,Text>{ @Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
for (Text person : values) {
sb.append(person+",");
} context.write(key,new Text(sb.toString()));
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
conf.set("mapreduce.framework.name","local");
conf.set("fs.defaultFS","file:///"); Job job = Job.getInstance(conf); job.setJarByClass(FriendsDemoStepOneDriver.class); job.setMapperClass(FriendsDemoStepOneMapper.class);
job.setReducerClass(FriendsDemoStepOneReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job,new Path("/Users/kris/Downloads/mapreduce/friends/friends.txt"));
FileOutputFormat.setOutputPath(job,new Path("/Users/kris/Downloads/mapreduce/friends/output")); boolean completion = job.waitForCompletion(true);
System.out.println(completion);
} }
运行的到的结果如下:

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

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

/**
* 遍历有同个好友的用户的列表进行组合,得到两人的共同好友
*/
public class FriendsDemoStepTwoDriver { static class FriendDemoStepTwoMapper extends Mapper<LongWritable, Text, Text, Text> { @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] split = line.split("\t");
String friend = split[0];
String[] persons = split[1].split(","); Arrays.sort(persons); for (int i = 0; i < persons.length-1; i++) {
for (int i1 = i+1; i1 < persons.length; i1++) {
context.write(new Text(persons[i]+"--"+persons[i1]),new Text(friend));
}
} }
} static class FriendDemoStepTwoReducer extends Reducer<Text, Text, Text, Text> { @Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder(); for (Text friend : values) {
sb.append(friend + ",");
} context.write(key,new Text(sb.toString()));
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
conf.set("mapreduce.framework.name","local");
conf.set("fs.defaultFS","file:///"); Job job = Job.getInstance(conf); job.setJarByClass(FriendsDemoStepOneDriver.class); job.setMapperClass(FriendDemoStepTwoMapper.class);
job.setReducerClass(FriendDemoStepTwoReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job,new Path("/Users/kris/Downloads/mapreduce/friends/output"));
FileOutputFormat.setOutputPath(job,new Path("/Users/kris/Downloads/mapreduce/friends/output2")); boolean completion = job.waitForCompletion(true);
System.out.println(completion);
}
}
得到的结果如下:

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

今天给大家分享的好友推荐算法就是这些,今天的只是一个小小的案例,现实场景中的运算肯定要比这个复杂的多,
但是思路和方向基本一致,如果有更好的建议或算法,欢迎与小吴一起讨论喔~ 如果您喜欢这篇文章的话记得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. Scrum的三个仪式:Sprint规划会,Scrum每日站会,Sprint评审会

    转自:http://blog.sina.com.cn/s/blog_6997f01501010m21.html Sprint Planning Meeting(Sprint规划会) 根据Product ...

  2. codeforces 822 D. My pretty girl Noora(dp+素数筛)

    题目链接:http://codeforces.com/contest/822/problem/D 题解:做这题首先要推倒一下f(x)假设第各个阶段分成d1,d2,d3...di组取任意一组来说,如果第 ...

  3. HDU dp递推 母牛的故事 *

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. windows10环境下安装深度学习环境anaconda+pytorch+CUDA+cuDDN

    步骤零:安装anaconda.opencv.pytorch(这些不详细说明).复制运行代码,如果没有报错,说明已经可以了.不过大概率不行,我的会报错提示AssertionError: Torch no ...

  5. JS千分位格式化方法,以及多种方法性能比较

    方法一字符串版 function toThousands(num) { var result = '', counter = 0; num = (num || 0).toString(); for ( ...

  6. dropwizard-core模块和应用启动分析

    简介 Dropwizard是一款开发运维友好.高效.RESTful web服务的框架.Dropwizard将稳定.成熟的java生态系统中的库整合为一个简单的.轻量级的包,即跨越了库和框架之间的界限, ...

  7. Spring错误

    今天在学习spring的aop操作时碰到了一个问题: Caused by: org.springframework.aop.framework.AopConfigException: Cannot p ...

  8. sql 增删改列名

    添加列:alter table table_name add new_column data_type [interality_codition] ALTER TABLE dbo.tb newColu ...

  9. 聊聊 Python 的单元测试框架(二):nose 和它的继任者 nose2

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  10. python自增自减?赋值语句返回值?逗号表达式?

    咳咳,直接进入正题吧. 自增自减(++/--),以及赋值语句,还有逗号表达式都是在C/C++中常见的运算符或表达式. 熟悉C/C++的小伙伴们都知道,在C/C++中: 自增自减(前缀/后缀)运算符将实 ...