MapReduce寻找共同好友
1.测试文件
A:B,C,D,F,E,O
B:A,C,E,K
C:F,A,D,I
D:A,E,F,L
E:B,C,D,M,L
F:A,B,C,D,E,O,M
G:A,C,D,E,F
H:A,C,D,E,O
I:A,O
J:B,O
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J
2.方法
2-1.方法一:
1.将域用户和好友分别作为值和键输出
{B,C,D,F,E,O}:A
{A,C,E,K}:B
2.可以看出:B,C,D,F,E,O都有共同好友A,
3.把A的好友两两组合作为键,A作为值,冒泡输出
4.经过shuffle处理后,会把BC作为键,共同好友作为值放入集合中
5.迭代集合中的好友,一次输出即可
2-2.方法二:
1.将用户和好友作为键和值输出
A:B,C,D,F,E,O --A:B,C,D,F,E,O
B:A,C,E,K --B:A,C,E,K
C:F,A,D,I --C:A,D,F,I
D:A,E,F,L --D:A,E,F,L
E:B,C,D,M,L --E:B,C,D,L,M
2.将所有键值对添加到map集合中
3.取map的键(所有用户)为数组
4.迭代数组,通过用户名"A"在map中取得他的好友
5.迭代除用户"A"以外的其他用户,获取这些用户的好友;
如果有用户同时存在于"A"和"B"的好友列表中
那么这些好友就是"AB"的共同好友
--A:{B,C,D,F,E,O}
--B:{A,C,E,K}
"A"中存在"C,E"用户,"B"中也存在"C,E"用户,那么"C,E"就是AB的共同好友
6.将"AB"作为键,共同好友作为值输出即可
3.代码
public class Friends {
// map
public static class MRMapper extends Mapper<LongWritable, Text, Text, Text> {
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String str = value.toString();
String friends = str.substring(2);
System.out.println(friends);
context.write(new Text(str.charAt(0) + ""), new Text(friends));
}
}
// reduce
public static class MRReducer extends Reducer<Text, Text, Text, Text> {
private static HashMap<String, String> map1 = new HashMap<String, String>();
public void run(Context context) throws IOException, InterruptedException {
try {
while (context.nextKeyValue()) {
reduce(context.getCurrentKey(), context.getValues(), context);
}
} finally {
cleanup(context);
}
}
public void reduce(Text key, Iterable<Text> iterable, Context context)
throws IOException, InterruptedException {
for (Text t : iterable) {
map1.put(key.toString(), t.toString());
}
}
public void cleanup(Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
List<String> list = new ArrayList<String>();
Collection<String> keys = map1.keySet();// 所有用户
String keys1 = keys.toString();
String keys2 = keys1.substring(1, keys1.length() - 1);
String[] split = keys2.split(",");
for (int i = 1; i < split.length; i++) {//迭代用户
String a = split[i].trim();
for (int j = (i+1); j < split.length; j++) {//迭代除外层循环以外的用户
String b = split[j].trim();
String a_and_b = "";
// a的好友
String af = map1.get(a);
String[] friends = af.split(",");
for (String s : friends) {//比较两个用户的好友列表,取共同好友
if (map1.get(b).contains(s)) {
a_and_b += "," + s;
}
}
System.out.println(a + "," + b + " 共同好友 " + a_and_b);
if (a_and_b.length() > 1) {
list.add(a + "," + b + " 共同好友 :" + a_and_b.substring(1));
}
}
}
for(String s:list){
context.write(new Text(""), new Text(s));
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(Friends.class);
job.setMapperClass(MRMapper.class);
job.setReducerClass(MRReducer.class);
job.setCombinerClass(MRReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.setInputPaths(job, new Path("hdfs://hadoop5:9000/input/friends.txt"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop5:9000/output/friends"));
System.out.println(job.waitForCompletion(true) ? 1 : 0);
}
}
如果有更简洁的方法,欢迎留言给博主。
MapReduce寻找共同好友的更多相关文章
- python版mapreduce题目实现寻找共同好友
看到一篇不知道是好好玩还是好玩玩童鞋的博客,发现一道好玩的mapreduce题目,地址http://www.cnblogs.com/songhaowan/p/7239578.html 如图 由于自己太 ...
- 用Mapreduce求共同好友
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...
- mapreduce 查找共同好友
A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E:B,C,D,M,L F:A,B,C,D,E,O,M G:A,C,D,E,F H:A,C,D,E,O I:A, ...
- mapreduce求共同好友
逻辑分析 以下是qq的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的) A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E: ...
- MapReduce案例-好友推荐
用过各种社交平台(如QQ.微博.朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图).它的算法主要是根据你们之间的共同好友 ...
- 大数据入门第九天——MapReduce详解(五)mapJoin、GroupingComparator与更多MR实例
一.数据倾斜分析——mapJoin 1.背景 接上一个day的Join算法,我们的解决join的方式是:在reduce端通过pid进行串接,这样的话: --order ,,P0001, ,,P0001 ...
- 中国移动飞信WAP登陆分析及脚本
中国移动飞信WAP网页版 http://f.10086.cn/im5/ 用WAP飞信登录并向好友发送信息,同时用wireshark抓包. 1.过滤POST表单提交数据包(wireshark规则: ht ...
- MapReduce实现二度好友关系
一.问题定义 我在网上找了些,关于二度人脉算法的实现,大部分无非是通过广度搜索算法来查找,犹豫深度已经明确了2以内:这个算法其实很简单,第一步找到你关注的人:第二步找到这些人关注的人,最后找出第二步结 ...
- 基于mapreduce的大规模连通图寻找算法
基于mapreduce的大规模连通图寻找算法 当我们想要知道哪些账号是一个人的时候往往可以通过业务得到两个账号之间有联系,但是这种联系如何传播呢? 问题 已知每个账号之间的联系 如: A B B C ...
随机推荐
- 总结Ajax验证注册功能的两种方式
方法一:使用jqueryForm插件提交表单注册 ①首先引入jquery和jqueryForm插件 <script type="text/javascript" src=&q ...
- CSS基础知识(颜色、伪类、盒子模型)
6.设置颜色单位 L 普通英文单词 {color : 属性值red;} 此方法简单,便捷.但设置的颜色在不同浏览器中,可能显示的颜色出现差异 * 三原色 - 红.绿.蓝 L 颜色的八进制方式 ...
- Mistakes in Hello World
今天在cmd中用Python写第一行代码"Hello World"出现错误. 写好的源码"HelloWorld.py"存放于" F:\learning ...
- d3 画地图终极自适应大小方案
d3,v4以前用d3画地图的时候,为了让地图差不多正好画在容器的上下左右正中间,不得不慢慢的这样调: const projection = d3.geoMercator() .center([108. ...
- flexbox 弹性盒子
flexbox 弹性盒子 1.基本知识 container(容器)属性 flex-direction: row | row-reverse | column | column-reverse 属性决定 ...
- Hadoop-MyEclipse安装配置
配置环境:Hadoop-1.2.1,MyEclipse,Centos6.5 网站上有很多关于Hadoop-eclipse的安装配置信息,但很少有讲到关于怎么在MyEclipse上配置Hadoop的相关 ...
- Java string和各种格式互转 string转int int转string
Java string和各种格式互转 string转int int转string 简单收集记录下 其他类型转String String s = String.valueOf( value); // 其 ...
- HDU 1014 Uniform Generator【GCD,水】
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 用线段树维护区间连通性,对于每一个区间记录6个域表示(左上,左下)(左上,右上)(右上, ...
- 跟我一起读postgresql源码(十二)——Executor(查询执行模块之——Materialization节点(下))
接前文,我们继续说剩下的4个Materialization节点. 7.SetOp节点 SetOp节点用于处理集合操作,对应于SQL语句中的EXCEPT.INTERSECT两种集合操作,至于另一种集合操 ...