使用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 ...
随机推荐
- 【转载】Web开发技术发展历史-版本1
原文在这里. Web开发技术发展历史 Web的诞生 提到Web,不得不提一个词就是“互联网”.Web是World Wide Web的简称,中文译为万维网.“万维网”和我们经常说的“互联网”是两个联系极 ...
- “无法在web服务器上启动调试,不是Debugger User组成员..."
在使用VS.net2003开发asp.net项目时,有时候在你调试项目时,会提示”无法在web服务器上启动调试,不是Debugger User组成员..."这样一个错误信息.很是让人头疼,一 ...
- 【JBPM4】任务form表单
主要是在任务节点处保存一个链接地址(表单),方便在执行到此时取值,访问. 好处是XML可随时修改,部署. JPDL <?xml version="1.0" encoding= ...
- JavaScript备忘录-闭包(2)
闭包的定义 闭包是指函数有自由独立的变量.换句话说,定义在闭包中的函数可以“记忆”它创建时候的环境. 闭包的浅显理解 function makeFunc() { var name = "Mo ...
- vue不用webpake等环境及脚手架也可以玩
初学时,搭环境,es6也来了,vuecil脚手架也弄了,调错,照着教程一遍一遍kei着... 然尔,实际开发中,所写的东西最后是要打包封装成软件的,为了方便其他人修改查看,不能打包成js文件... 难 ...
- Django学习过程中的排错总结
报错一:RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPE ...
- react native 手势响应
参考地址:https://www.jianshu.com/p/935e5c6a5064 官方文档地址:https://facebook.github.io/react-native/docs/panr ...
- 创建一个支持ES6的Nodejs项目
文章来自于:https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o 第一步:创建项目文件夹并初始化 ...
- Mysql Sql Explain
1.使用mysql explain的原因 在我们php程序员的日常写代码中,有时候会发现我们写的sql语句运行的特别慢,导致响应时间特别长,这种情况在高并发的情况下,我们的网站会直接崩溃,为什么双十一 ...
- Unity游戏开发之C#快速入门
C#是微软团队在开发.NET框架时开发的,它的构想接近于C.C++,也和JAVA十分相似,有许多强大的编程功能. 个人感受是C#吸收了众多编程语言的优点,从中可以看到C.C++.Java.Javasc ...