海量数据的二度人脉挖掘算法(Hadoop 实现)

大数据 算法 Hadoop
最近做了一个项目,要求找出二度人脉的一些关系,就好似新浪微博的“你可能感兴趣的人” 中,间接关注推荐;简单描述:即你关注的人中有N个人同时都关注了 XXX 。

最近做了一个项目,要求找出二度人脉的一些关系,就好似新浪微博的“你可能感兴趣的人” 中,间接关注推荐;简单描述:即你关注的人中有N个人同时都关注了 XXX 。

 

在程序的实现上,其实我们要找的是:若 User1 follow了10个人 {User3,User4,User5,... ,User12}记为集合UF1,那么 UF1中的这些人,他们也有follow的集合,分别是记为: UF3(User3 follow的人),UF4,UF5,...,UF12;而在这些集合肯定会有交集,而由最多集合求交产生的交集,就是我们要找的:感兴趣的人。

 

我在网上找了些,关于二度人脉算法的实现,大部分无非是通过广度搜索算法来查找,犹豫深度已经明确了2以内;这个算法其实很简单,***步找到你关注的人;第二步找到这些人关注的人,***找出第二步结果中出现频率***的一个或多个人,即完成。

 

但如果有***别的用户,那在运算时,就肯定会把这些用户的follow 关系放到内存中,计算的时候依次查找;先说明下我没有明确的诊断对比,这样做的效果一定没 基于hadoop实现的好;只是自己,想用hadoop实现下,最近也在学;若有不足的地方还请指点。

首先,我的初始数据是文件,每一行为一个follow 关系 ida+‘\t’+idb;表示 ida follow idb。其次,用了2个Map/Reduce任务。

Map/Reduce 1:找出 任意一个用户 的 follow 集合与 被 follow 的集合。如图所示:

代码如下:

Map任务: 输出时 key :间接者 A 的ID ,value:follow 的人的ID 或 被follow的人的ID

  1. public void map(Text key, IntWritable values, Context context) throws IOException,InterruptedException{  
  2.         int value = values.get();  
  3.         //切分出两个用户id  
  4.         String[] _key = Separator.CONNECTORS_Pattern.split(key.toString());  
  5.         if(_key.length ==2){  
  6.             //"f"前缀表示 follow;"b" 前缀表示 被follow  
  7.             context.write(new Text(_key[0]), new Text("f"+_key[1]));  
  8.             context.write(new Text(_key[1]), new Text("b"+_key[0]));  
  9.               
  10.               
  11.         }  
  12.     } 

Reduce任务: 输出时 key :间接者 A 的ID , value为 两个String,***个而follow的所有人(用分割符分割),第二个为 被follow的人(同样分割)

  1. &nbsp;&nbsp;&nbsp;&nbsp;protected void reduce(Text key, Iterable<TextPair> pairs, Context context)  
  2.      throws IOException,InterruptedException{  
  3.         StringBuilder first_follow = new StringBuilder();  
  4.         StringBuilder second_befollow = new StringBuilder();  
  5.           
  6.         for(TextPair pair: pairs){  
  7.             String id = pair.getFirst().toString();  
  8.             String value = pair.getSecond().toString();  
  9.             if(id.startsWith("f")){  
  10.                 first_follow.append(id.substring(1)).append(Separator.TABLE_String);  
  11.             } else if(id.startsWith("b")){  
  12.                 second_befollow.append(id.substring(1)).append(Separator.TABLE_String);  
  13.             }  
  14.         }  
  15.           
  16.         context.write(key, new TextPair(first_follow.toString(),second_befollow.toString()));  
  17. &nbsp;&nbsp;&nbsp;&nbsp;} 

其中Separator.TABLE_String为自定义的分隔符;TextPair为自定义的 Writable 类,让一个key可以对应两个value,且这两个value可区分。

 

 

 

Map/Reduce 2:在上一步关系中,若B follow A,而 A follow T ,则可以得出 T 为 B 的二度人脉,且 间接者为A ,于是找出 相同二度人脉的不同间接人。如图所示:

代码如下:

Map 任务:输出时 key 为 由两个String 记录的ID表示的 二度人脉关系,value 为 这个二度关系产生的间接人的ID

  1. public void map(Text key, TextPair values, Context context) throws IOException,InterruptedException{  
  2.         Map<String, String> first_follow = new HashMap<String, String>();  
  3.         Map<String, String> second_befollow = new HashMap<String, String>();  
  4.         String _key = key.toString();  
  5.         String[] follow = values.getFirst().toString().split(Separator.TABLE_String);  
  6.           
  7.         String[] second = values.getSecond().toString().split(Separator.TABLE_String);  
  8.           
  9.         for(String sf : follow){  
  10.               
  11.             first_follow.put(sf , _key );  
  12.               
  13.         }  
  14.           
  15.         for(String ss : second){  
  16.               
  17.             second_befollow.put(ss , _key );  
  18.               
  19.         }  
  20.           
  21.         for(Entry<String, String> f : first_follow.entrySet()){  
  22.             for(Entry<String, String> b : second_befollow.entrySet()){  
  23.                 context.write(new TextPair(f.getKey() ,b.getKey()), new Text(key));  
  24.             }  
  25.         }  
  26. &nbsp;&nbsp;&nbsp;&nbsp;} 

Reduce任务:输出时 key 仍然为二度人脉关系, value 为所有间接人 的ID以逗号分割。

  1. protected void reduce(TextPair key, Iterable<Text> values, Context context)  
  2.     throws IOException, InterruptedException {  
  3.       
  4.     StringBuilder resutl = new StringBuilder();  
  5.     for (Text text : values){  
  6.         resutl.append(text.toString()).append(",");  
  7.     }  
  8.       
  9.     context.write(key, new Text(resutl.toString()));  

到这步,二度人脉关系基本已经挖掘出来,后续的处理就很简单了,当然也基于二度人脉挖掘三度,四度:)

原文链接:http://my.oschina.net/BreathL/blog/75112

责任编辑:林师授 来源: OSCHINA
相关推荐

2013-01-05 02:37:30

HadoopHadoop实例

2010-03-02 09:24:52

2012-07-17 10:05:23

亚马逊数据中心

2012-08-16 11:30:24

2021-04-15 19:48:38

0day漏洞Chrome

2023-08-25 13:32:05

COBOLJavaAI

2014-11-04 09:18:33

安全策略安全管理威胁情报

2016-04-11 14:35:59

机器学习数据挖掘数据模型

2022-01-25 14:21:05

亿咖通智能座舱吉利

2021-12-02 06:19:23

推特CEOCTO

2014-03-10 18:16:48

曙光大数据应用

2012-08-06 11:30:01

CreCloud云网管美信科技

2017-11-16 19:26:34

海量数据算法计算机

2012-03-13 15:16:09

2014-03-18 10:16:58

SVM

2012-07-06 16:19:23

华为服务器

2010-07-12 17:25:32

SQL Server海

2015-03-11 17:53:36

2009-07-03 19:58:51

SQL Server2
点赞
收藏

51CTO技术栈公众号