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 密码修改 ...
随机推荐
- 分层图【p4568】 [JLOI2011]飞行路线
Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为\(0\)到\(n−1\),一共有\(m\)种航线 ...
- codevs 2837 考前复习——01背包
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Aiden马上要考试了,可他还没怎么复习,于是他 ...
- outlook preview setup
ow To Show Subject Above/Below Sender In Mail List In Outlook? Normally in the compact view of a mai ...
- POJ 3713 Transferring Sylla (三连通图)
[题目链接] http://poj.org/problem?id=3713 [题目大意] 给出一个图判断是不是三连通图,三连通图的意思是对于图中任意两点, 至少有三条路是可以相互连通的. [题解] 我 ...
- (转)Unity3d通过Action注册事件,回调方法
http://www.cnblogs.com/jisi5789/archive/2013/04/22/3036589.html using UnityEngine; namespace Liulala ...
- Chrome的开发必备小技巧
谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...
- Android开发必须知道SERVICE的10件事
这些年我在和其他安卓攻城狮交流时经常谈到的一个话题就是Service组件被开发者错误地理解,不管是新手还是老司机.这篇文章就是交流的成果. 这篇文章不会讲解Service的使用方法,这个要去看官方的( ...
- ubuntu 下安装nodejs以及pm2
ubuntu 12.04服务器可以使用apt-get方式安装Node JS,但是,安装完后的版本为v0.6.12的版本,如果我们想要使用新一点的版本需要做如下配置: 1 2 3 4 apt-get i ...
- Hadoop之Mapreduce详解
1. 什么是Mapreduce Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架: Mapreduce核心功能是将用户编写的业务逻辑代码和自带 ...
- .net 真实代理和透明代理的交互
.本地代理调用 using System; using System.Runtime.Remoting ; using System.Runtime.Remoting.Services ; using ...