使用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 ...
随机推荐
- Java学习笔记(八)——java多线程
[前面的话] 实际项目在用spring框架结合dubbo框架做一个系统,虽然也负责了一块内容,但是自己的能力还是不足,所以还需要好好学习一下基础知识,然后做一些笔记.希望做完了这个项目可以写一些dub ...
- gulp在webstorm里运行,告别cmd控制台!
使用webstorm运行gulp程序,不再用一直开着cmd了!鬼知道自己会不会不小心关闭了cmd呢.看截图操作吧! 如果你的gulp设置了default任务,那么直接点击“运行‘default‘’就行 ...
- ubuntu下基于Anaconda使用Tensorflow
为了在ubuntu下利用Anaconda使用tensorflow,但在利用conda安装tensorflow,不能在终端,spyder和notebook中直接使用,需要我们进行一定的配置. 1.安装A ...
- hdu1006 Tick and Tick (数学题 借鉴了大神的博客)
先缩短一半的时间:早上的12个小时和下午的12小时对时钟是一样的,因为时钟12小时与0小时的三针位置相同.接着就是了解到每次所有的针从有重合到再次有重合至多有一段连续的段符合三针分离度大于n.所以只要 ...
- SQLSEVER 中的那些键和约束
SQL Server中有五种约束类型,分别是 PRIMARY KEY约束.FOREIGN KEY约束.UNIQUE约束.DEFAULT约束.和CHECK约束.查看或者创建约束都要使用到 Microso ...
- bzoj 2803 [Poi2012]Prefixuffix 兼字符串hash入门
打cf的时候遇到的问题,clairs告诉我这是POI2012 的原题..原谅我菜没写过..于是拐过来写这道题并且学了下string hash. 字符串hash基于Rabin-Karp算法,并且对于 ...
- 【启发式搜索】【A*算法】hdu6171 Admiral
一个舰队的目标状态如上图.红色是旗舰.然后给你初始局面,每一次决策可以把旗舰和其上一层或下一层的两个相邻的进行交换.如果能在20步内出解的话,输出最小步数:否则输出“too difficult”. 把 ...
- 【模拟】Gym - 101190A - Abbreviation
让你把所有的“连续的仅有首字母大写的”词组用缩写表示,并且在后面用括号注明原词组. #include<cstdio> #include<cstring> using names ...
- ACM-ICPC 2016亚洲区域赛(沈阳站)游记(滚粗记)
首发于QQ空间和知乎,我在这里也更一下. 前言 以前高中搞竞赛的时候,经常看到神犇出去比赛或者训练之后写游记什么的,感觉萌萌哒.但是由于太弱,就没什么心情好写.现在虽然还是很弱,但是抱着享受的心情 ...
- Problem C: 零起点学算法93——矩阵转置
#include<stdio.h> int main() { ][],b[][]; while(scanf("%d%d",&n,&m)!=EOF) { ...