使用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 ...
随机推荐
- PHP7.3发布啦
作为PHP5的最后一个版本,也是目前使用最广泛的PHP版本,PHP 5.6始于公元2014年(不是1804年,嘿嘿),其第一个测试版PHP 5.6 alpha 1版于2014年1月发布.随机产生了第一 ...
- 走进 Akka.NET
官方文档:https://getakka.net/index.html 官网:https://petabridge.com/ 一.Akka.NET 是什么? Akka 是一个构建高并发.分布式和弹性消 ...
- 递归遍历JSON树
递归遍历JSON树 前几天有个人问我,json串的层级无限深,但在json串中的key是已知的,在json串中的value,有些事Object,有些是Array,如何把这些层级无限深的key所对应的v ...
- LoadRunner脚本回放日志中的Warning信息
关注LoadRunner脚本回放日志中的Warning信息 最近在与大家的讨论中发现了LoadRunner的很多问题,出于解决问题的出发点,我也就相关自己不理解的问题在Google中搜索了一番,并 ...
- sql 时间日期格式化
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- 安装 gcc 编译器
1.安装编译工具 gcc.gcc-c++.make 注意解决依赖关系,推荐使用 yum 安装,若不能联网可使用安装光 盘做为 yum 源 1)编辑 yum 配置文件: Mount /dev/cdrom ...
- python的Django使用mysql基本操作
环境:Centos6.6 ,python2.6 准备工作: mysql的安装,以及MySQL-python的安装 http://www.cnblogs.com/zychengzhiit1/p/4437 ...
- Oracle 使用序列、触发器实现自增
之前项目开发多用mysql,对于id自增长设置,只需要简单修改列属性便好.最近改用ORACLE,头大一圈.ORACLE的相关操作,多用脚本.想短平快,难.最终用sql developer通过UI进行修 ...
- 洛谷P2280 [HNOI2003] 激光炸弹 [前缀和]
题目传送门 题目描述 输入输出格式 输入格式: 输入文件名为input.txt 输入文件的第一行为正整数n和正整数R,接下来的n行每行有3个正整数,分别表示 xi,yi ,vi . 输出格式: 输出文 ...
- ExtJs之表单(form)
--Form和Form Basic Extjs Form和Form Basic是两个东西,Form提供界面的展示,而Form Basic则提供数据的处理.验证等功能.每一个Form Panel在创建的 ...