使用MapReduce实现二度人脉搜索算法
一,背景介绍
在新浪微博、人人网等社交网站上,为了使用户在网络上认识更多的朋友,社交网站往往提供类似“你可能感兴趣的人”、“间接关注推荐”等好友推荐的功能,其中就包含了二度人脉算法。
二,算法实现
原始数据集测试:
a b
b c
a c
b d
c e
e c
e f
数据集说明:为关注关系,即a关注b,b关注c和d,所以a的二度人脉应该是d和c,而c已经被a关注,所以应该舍去,自己不能二度人脉是自己,如c关注e,而e又关注c
代码实现,代码用了两个Job实现的
难点:两个job如何先后执行
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set; public class De2Friends {
public static class De2Mapper1 extends Mapper<Object,Text,Text,Text>{
@Override
protected void map(Object key, Text value, Context context) throws
IOException, InterruptedException {
String line =value.toString();
String[] strArr = line.split("\t");
if(strArr.length==2) {
//关注的人
context.write(new Text(strArr[0]), new Text("1" + strArr[1]));
//被关注的人
context.write(new Text(strArr[1]), new Text("0" + strArr[0]));
}
}
} public static class De2Reducer1 extends Reducer<Text,Text,Text,Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Set<String> follows= new HashSet<String>();
Set<String> fans=new HashSet<String>();
for(Text val :values ){
String friend =val.toString();
if(friend.startsWith("1")){
context.write(key,new Text(friend));//输出用户已经关注的人,一度人脉
follows.add(friend.substring(1));
}
if(friend.startsWith("0")){
fans.add(friend.substring(1));
}
}
for(String fan : fans)
for(String follow:follows) {
if (!fan.equals(follow)) {
context.write(new Text(fan),new Text("2"+follow));
}
} }
} public static class De2Mapper2 extends Mapper<Object,Text,Text,Text>{
@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String line =value.toString();
String[] strArr=line.split("\t");
if(strArr.length==2) {
context.write(new Text(strArr[0]), new Text(strArr[1]));//输出用户的一度好友和二度好友
}
}
} public static class De2Reducer2 extends Reducer<Text,Text,Text,Text>{
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Set<String> firstFriend = new HashSet<String>();
Set<String> secondFriend =new HashSet<String>();
for(Text val:values){
String friend =val.toString();
if(friend.contains("1")){
firstFriend.add(friend.substring(1));
}
if(friend.contains("2")){
secondFriend.add(friend.substring(1));
}
}
for(String second:secondFriend) {
if(!(firstFriend.contains(second)))
context.write(key,new Text(second)); //输出好友的二度人脉
}
}
} public static void main(String[] args) throws Exception{
System.setProperty("hadoop.home.dir","E:\\softs\\majorSoft\\hadoop-2.7.5");
Configuration conf =new Configuration();
conf.set("mapreduce.app-submission.cross-platform", "true");
Path fileInput = new Path("hdfs://mycluster/testFile/qq.txt");
Path tempDir = new Path("hdfs://mycluster/output/deg2friend-temp-" + Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));
Path fileOutput = new Path("hdfs://mycluster/output/qq");
Job job = Job.getInstance(conf,"de2Firend");
job.setJar("E:\\bigData\\hadoopDemo\\out\\artifacts\\wordCount_jar\\hadoopDemo.jar");
job.setJarByClass(De2Friends.class);
job.setMapperClass(De2Mapper1.class);
job.setReducerClass(De2Reducer1.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setNumReduceTasks(1);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job,fileInput);
FileOutputFormat.setOutputPath(job,tempDir);
job.waitForCompletion(true);//必须有,感觉是等job执行完才让job2执行的效果,即阻塞吧 Job job2 = Job.getInstance(conf,"de2Firend");
job2.setJar("E:\\bigData\\hadoopDemo\\out\\artifacts\\wordCount_jar\\hadoopDemo.jar");
job2.setJarByClass(De2Friends.class);
job2.setMapperClass(De2Mapper2.class);
job2.setReducerClass(De2Reducer2.class);
job2.setMapOutputKeyClass(Text.class);
job2.setMapOutputValueClass(Text.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job2,tempDir);
FileOutputFormat.setOutputPath(job2,fileOutput); System.exit(job2.waitForCompletion(true)?0:1);
}
}
结果如下:
a d
b e
b f
c f
使用MapReduce实现二度人脉搜索算法的更多相关文章
- 海量数据的二度人脉挖掘算法(Hadoop 实现)
最近做了一个项目,要求找出二度人脉的一些关系,就好似新浪微博的“你可能感兴趣的人” 中,间接关注推荐:简单描述:即你关注的人中有N个人同时都关注了 XXX . 在程序的实现上,其实我们要找的是:若 U ...
- 依据二度人脉推荐好友sql
friend表结构 DROP TABLE IF EXISTS FRIEND; create table friend( uid bigint not null comment ' ...
- hadoop计算二度人脉关系推荐好友
https://www.jianshu.com/p/8707cd015ba1 问题描述: 以下是qq好友关系,进行好友推荐,比如:老王和二狗是好友 , 二狗和春子以及花朵是好友,那么老王和花朵 或者老 ...
- MapReduce实现二度好友关系
一.问题定义 我在网上找了些,关于二度人脉算法的实现,大部分无非是通过广度搜索算法来查找,犹豫深度已经明确了2以内:这个算法其实很简单,第一步找到你关注的人:第二步找到这些人关注的人,最后找出第二步结 ...
- Hadoop MapReduce实现人员二度关系运算
1.一度人脉:双方直接是好友 2.二度人脉:双方有一个以上共同的好友,这时朋友网可以计算出你们有几个共同的好友并且呈现数字给你.你们的关系是: 你->朋友->陌生人 3.三度人脉:即你朋友 ...
- Spark 计算人员二度关系
1.一度人脉:双方直接是好友 2.二度人脉:双方有一个以上共同的好友,这时朋友网可以计算出你们有几个共同的好友并且呈现数字给你.你们的关系是: 你->朋友->陌生人 3.三度人脉:即你朋友 ...
- 基于Spark GraphX计算二度关系
关系计算问题描述 二度关系是指用户与用户通过关注者为桥梁发现到的关注者之间的关系.目前微博通过二度关系实现了潜在用户的推荐.用户的一度关系包含了关注.好友两种类型,二度关系则得到关注的关注.关注的好友 ...
- <转>“人脉投资”的10条建议
谁都知道人脉很重要,所以有些人非常勤奋的“做人脉”,他们往往会这样做—— 积极的参与各类线下活动,逢人就换名片.加微信. 见到名人或者重要人物必合影,而且他们还会掏出手机来给你看. 逢年过节,给所有他 ...
- 【网易官方】极客战记(codecombat)攻略-地牢-Kithmaze 二度历险
关卡连接: https://codecombat.163.com/play/level/the-second-kithmaze 很多人试过,但只有少数人能穿过此迷宫. 简介: 记住,你只需 一个 wh ...
随机推荐
- Python3判断自身脚本是不是在运行
采用Socket方式,启动时占用一个端口,转化问题为判断端口是不是存在? import sys import locale import http.server import socketserver ...
- 使用php后台给自己做一个页面路由,配合ajax实现局部刷新。
今天就要放假了,把近来囤积的小玩意儿总结整理一下. 在请求一个html页面来嵌入到当前页会有一个问题,就是跟随请求过来的html他的样式表和脚本会失效.是因为文档加载的先后顺序等问题造成的.因此,加载 ...
- localStorage和sessionStorage的总结
localStorage:没有时间限制的数据存储 API: 1.localStorage.setItem('name','wangwei')/localStorage.name='wangwei'存储 ...
- 转:python cgi编程
转:http://www.runoob.com/Python/python-cgi.html 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway ...
- go chapter 5 - 异常处理 error、panic、recover
https://blog.csdn.net/tennysonsky/article/details/78946265 error(不中断).panic(中断).recover(拦截中断 类似于 ca ...
- Memory Allocation with COBOL
Generally, the use of a table/array (Static Memory) is most common in COBOL modules in an applicatio ...
- WHERE 子句中的标量子查询
标量子查询不仅可以用在SELECT 语句的列表中,它还可以用在WHERE 子句中,而且实际应用中子查询很多的时候都是用在WHERE子句中的. 先来看一个简单的例子,我们要检索喜欢“Story”的读者主 ...
- 关于使用jqmobi前端框架在phonegap平台上开发时的日期时间选择控件
jqmobi(appframework)作为Intel的一款html5移动前端框架,以其自身轻量级和容易上手获得了很多移动HTML5开发者的喜爱,相对于jquerymobile,它可以说将jQuery ...
- 插头dp练习
最近学了插头dp,准备陆续更新插头dp类练习. 学习论文还是cdq那篇<基于连通性状态压缩的动态规划问题>. 基本的想法都讲得很通透了,接下来就靠自己yy了. 还有感谢kuangbin大大 ...
- HDU 6060 RXD and dividing(LCA)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点1, ...