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,O
J:B,O
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J 求出哪些人两两之间有共同好友,及他俩的共同好友都是谁
例如A-B:C,E
A-E:B,C,D
一种错误的理解就是E在A的还有列表中,那么A也在E的列表,且A和E同时有的好友才是共同好友
,如果按照这个观点思考下去就简单了,但是这个是错的,因为A-E:B,C,D 这种不不满足
正确的理解是求人与人之间的共同好友,人与人之间是否是同一个好友,是否在彼此的好友列表无关。
如果这个程序不用mapreduce做那么应该是先把人全部切分出来,然后循环进行人与人的组合,组合之后将他们好友列表组合,将那些出现两次的还有找到,这些就是人与人之间的共同还有,也是人工去找共同好友的方法,
但是放在mapreuce。,,每次只能读取一行数据不能都到他行的,如果要读到其他行的就要找到一个key然后还要将其他行的数据类聚一起,这样才能读到其他行。
如果知道答案的话,这样想的话就可以避免混淆了
tom: apple,pear,banana,waterball
jerry:apple,pear
jack:banana,apple
哪些人两两之间有共同的水果,列举出两人所有的共同水果。这样大家都不会混淆了。但是工作中遇到的就是人和好友的问题,大胆的抽象成人和水果也是工作中要做的
下面链接是答案
package my.hadoop.hdfs.findFriend; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class FindCommonFriendOne { public static class FindFriendMapper extends
Mapper<LongWritable, Text, Text, Text> {
// 泛型,定义输入输出的类型
/**
* 友 人
*/
Text text = new Text(); @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// 将mptask传给我们的文本内容转换成String
String line = value.toString();
IntWritable ONE = new IntWritable();
// 根据空格切分
String[] qqAndFriend = line.split(":");//分割出QQ号
String qq = qqAndFriend[];
String otherFriend = "";
StringBuffer friendbuf = new StringBuffer(qqAndFriend[]+","); String[] friends = qqAndFriend[].split(",");
for (String friend : friends) {
//查找其他朋友
//otherFriend = friendbuf.delete(friendbuf.indexOf(friend),friendbuf.indexOf(friend)+1).toString();
context.write(new Text(friend), new Text(qq));
}
}
} public static class FindFriendReducer extends
Reducer<Text, Text, Text, Text> { @Override
protected void reduce(Text Keyin, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
String qqs = "";
for (Text val : values) {
qqs +=val.toString() + ",";
}
context.write(Keyin, new Text(qqs));
}
} public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException { Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJarByClass(FindCommonFriendOne.class); job.setMapperClass(FindFriendMapper.class);
job.setReducerClass(FindFriendReducer.class);
//指定最终输出的数据kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path(args[])); FileOutputFormat.setOutputPath(job, new Path(args[]));
boolean res = job.waitForCompletion(true);
System.exit(res ? :);
} }
package my.hadoop.hdfs.findFriend; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class FindCommonFriendTwo { public static class FindFriendMapper extends
Mapper<LongWritable, Text, Text, Text> {
// 泛型,定义输入输出的类型
/**
* 友 人
*/
Text text = new Text(); @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// 将mptask传给我们的文本内容转换成String
String line = value.toString();
IntWritable ONE = new IntWritable();
// 根据空格切分
String[] friendAndQQ = line.split("\t");//分割出QQ号
String friend = friendAndQQ[];
String otherFriend = "";
StringBuffer friendbuf = new StringBuffer(friendAndQQ[] ); String[] qqs = friendAndQQ[].split(",");
for (int i=;i < qqs.length;i++) {
//查找其他朋友
for(int j = i+;j<qqs.length;j++)
{
//避免出现A-D 与D-A的情况
if(qqs[i].compareTo(qqs[j])>)
{
context.write(new Text(qqs[i]+"-"+qqs[j]), new Text(friend));
}
else{
context.write(new Text(qqs[j]+"-"+qqs[i]), new Text(friend));
} } }
}
} public static class FindFriendReducer extends
Reducer<Text, Text, Text, Text> { @Override
protected void reduce(Text Keyin, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
StringBuffer friends = new StringBuffer();
for (Text val : values) {
if(friends.indexOf(val.toString())<)
{
friends.append(val).append(",");
}
}
context.write(Keyin, new Text(friends.toString()));
}
} public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException { Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJarByClass(FindCommonFriendTwo.class); job.setMapperClass(FindFriendMapper.class);
job.setReducerClass(FindFriendReducer.class);
//指定最终输出的数据kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path(args[])); FileOutputFormat.setOutputPath(job, new Path(args[]));
boolean res = job.waitForCompletion(true);
System.exit(res ? :);
} }
[hadoop@mini2 study]$ hadoop fs -cat /findfriend/output/tes0/*
A I,K,C,B,G,F,H,O,D,
B A,F,J,E,
C A,E,B,H,F,G,K,
D G,C,K,A,L,F,E,H,
E G,M,L,H,A,F,B,D,
F L,M,D,C,G,A,
G M,
H O,
I O,C,
J O,
K B,
L D,E,
M E,F,
O A,H,I,J,F,
[hadoop@mini2 study]$ hadoop fs -cat /findfriend/output/tes2/*
B-A E,C,
C-A F,D,
C-B A,
D-A E,F,
D-B A,E,
D-C F,A,
E-A D,C,B,
E-B C,
E-C D,
E-D L,
F-A C,O,D,E,B,
F-B C,A,E,
F-C A,D,
F-D E,A,
F-E C,B,M,D,
G-A E,D,C,F,
G-B E,A,C,
G-C D,F,A,
G-D A,E,F,
G-E D,C,
G-F C,A,E,D,
H-A O,E,C,D,
H-B E,C,A,
H-C D,A,
H-D E,A,
H-E C,D,
H-F C,D,A,E,O,
H-G C,A,E,D,
I-A O,
I-B A,
I-C A,
I-D A,
I-F A,O,
I-G A,
I-H A,O,
J-A B,O,
J-E B,
J-F O,B,
J-H O,
J-I O,
K-A D,C,
K-B A,C,
K-C D,A,
K-D A,
K-E C,D,
K-F D,C,A,
K-G D,C,A,
K-H C,D,A,
K-I A,
L-A E,D,F,
L-B E,
L-C D,F,
L-D F,E,
L-E D,
L-F D,E,
L-G E,F,D,
L-H E,D,
L-K D,
M-A F,E,
M-B E,
M-C F,
M-D F,E,
M-F E,
M-G E,F,
M-H E,
M-L E,F,
O-B A,
O-C I,A,
O-D A,
O-F A,
O-G A,
O-H A,
O-I A,
O-K A,
mapreduce系列(7)--查找共同好友
mapreduce 查找共同好友的更多相关文章
- 用Mapreduce求共同好友
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...
- 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 ...
- 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.微博.朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图).它的算法主要是根据你们之间的共同好友 ...
- hadoop系列四:mapreduce的使用(二)
转载请在页首明显处注明作者与出处 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6 ...
- (转)OpenFire源码学习之七:组(用户群)与花名册(用户好友)
转:http://blog.csdn.net/huwenfeng_2011/article/details/43413651 Group 在openfire中的gorop——组,也可以理解为共享组.什 ...
- 易货beta版本测试报告
测试 对于服务器端我们是进行了单元测试 对于客户端我们使用的是在线的云测工具对app进行了包括安装,启动,具体功能以及ui方面的测试. 另外,对于客户端,我们还进行了对细节功能的人工测试 功能需求编号 ...
- 使用Redis来实现LBS的应用
原文地址 微信.陌陌 架构方案分析 近两年.手机应用,莫过于微信.陌陌之类最受欢迎:但实现原理,分享文章甚少. 故,提出两种方案,供分享:不对之处,敬请留言学习. 目标 查找附近的某某某,由近到远返回 ...
- 《易货》Alpha版本测试报告
一.测试计划 功能需求编号 功能需求名称 功能需求描述 测试计划 1 用户注册 每一个想要发布商品或者需要购买商品的用户都需要注册一个账号 √ 2 用户登录 已经拥有账号的用户登录 √ 3 密码修改 ...
随机推荐
- luogu P3834 【模板】可持久化线段树 1(主席树)
题解真的是越写越懒 // luogu-judger-enable-o2 #include<cstdio> #include<algorithm> using std::sort ...
- BZOJ 3864 Hero Meets Devil
题目大意 给定一个由AGCT组成的串\(t\), 求对于所有的\(L \in [1, |t|]\), 有多少个由AGCT组成的串\(s\)满足\(LCS(s, t) = L\). Solution 传 ...
- tiny4412 串口驱动分析四 --- 修改默认的串口输出
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- 【Linux】CentOS7上rpm命令批量卸载删除模糊rpm包名
例如,我要删除如下文件名匹配上wine的所有文件
- ubuntu16 安装cron 以及使用
https://www.cnblogs.com/intval/p/5763929.html sudo apt-get install cron 启动cron sudo service cron sta ...
- NFS 服务配置篇
安装.配置NFS服务 1.NFS简介 NFS(network file system) NFS是一个主机A通过网络,允许其他主机B可以来共享主机A的一个目录文件的一个文件系统 2.需要安装两个包nfs ...
- api.js
ylbtech-JavaScript-util: api.js API 代理接口 1.A,JS-效果图返回顶部 1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.yinta ...
- Swagger2 (3) 集成easymock 生成mock 测试数据
转载:http://blog.csdn.net/sai739295732/article/details/73957138 2.可以集成swagger 3.我们来玩一下 首先你需要一个swagger ...
- java学习笔记——大数据操作类
java.math包中提供了两个大数字操作类:BigInteger(大整数操作类) BigDecimal(大小数操作类). 大整数操作类:BigInteger BigInteger类构造方法:publ ...
- Win8.1应用开发之Bing Maps
这里介绍怎样进行Bing Maps的开发.首先我们须要在我们的程序中引入Bing Map的SDK.详细方法,这里推荐一个链接<win8>使用Bing地图.这样一个hello world便出 ...