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 查找共同好友的更多相关文章

  1. 用Mapreduce求共同好友

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...

  2. 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 ...

  3. 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: ...

  4. MapReduce案例-好友推荐

    用过各种社交平台(如QQ.微博.朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图).它的算法主要是根据你们之间的共同好友 ...

  5. hadoop系列四:mapreduce的使用(二)

    转载请在页首明显处注明作者与出处 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6 ...

  6. (转)OpenFire源码学习之七:组(用户群)与花名册(用户好友)

    转:http://blog.csdn.net/huwenfeng_2011/article/details/43413651 Group 在openfire中的gorop——组,也可以理解为共享组.什 ...

  7. 易货beta版本测试报告

    测试 对于服务器端我们是进行了单元测试 对于客户端我们使用的是在线的云测工具对app进行了包括安装,启动,具体功能以及ui方面的测试. 另外,对于客户端,我们还进行了对细节功能的人工测试 功能需求编号 ...

  8. 使用Redis来实现LBS的应用

    原文地址 微信.陌陌 架构方案分析 近两年.手机应用,莫过于微信.陌陌之类最受欢迎:但实现原理,分享文章甚少. 故,提出两种方案,供分享:不对之处,敬请留言学习. 目标 查找附近的某某某,由近到远返回 ...

  9. 《易货》Alpha版本测试报告

    一.测试计划 功能需求编号 功能需求名称 功能需求描述 测试计划 1 用户注册 每一个想要发布商品或者需要购买商品的用户都需要注册一个账号 √ 2 用户登录 已经拥有账号的用户登录 √ 3 密码修改 ...

随机推荐

  1. cf 546C Soldier and Cards

    题目链接:C. Soldier and Cards Two bored soldiers are playing card war. Their card deck consists of exact ...

  2. Linux CURL的安装

    Linux CURL的安装  Linux CURL的安装   --获得安装包,从网上直接下载或者其他途径,这里直接wget# wget http://curl.haxx.se/download/cur ...

  3. 彻底理解Java中的hashcode方法(转)

    本文转自http://www.importnew.com/18851.html 哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有 ...

  4. Gradle Distributions

    Gradle Distributions services.gradle.org/ distributions/ gradle-3.4-rc-3-all.zip 13-Feb-2017 14:55 + ...

  5. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  6. SilverLight-3:SilverLight 备注

    ylbtech_silverlight 一.DebugSilverlight应用程序的方法: 第一种: 1.Silverlight引用命名空间:System.Diagnostics; 2.在程序必要的 ...

  7. [Functional Programming] Read and Transform Values from a State ADT’s State (get)

    Many times we need to access and transform state, either in part or in full, to be used when calcula ...

  8. [Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming

    Every dynamic programming algorithm starts with a grid. It entails solving subproblems and builds up ...

  9. 解决ListView在界面只显示一个item

    ListView只显示一条都是scrollview嵌套listView造成的,将listView的高度设置为固定高度之后,三个条目虽然都完全显示.但是这个地方是动态显示的,不能写死.故采用遍历各个子条 ...

  10. How to Handle Exception