1. 需求:给出A-O个人中每个人的好友列表,求出哪些人两两之间有共同好友,以及他们的共同好友都有谁。
       注意:这些人好友都是单向的,可能A是B的好友,但是B不一定是A的好友,这种类似的微博的关注,
                  A关注B,但是B不一定关注了A。
  2. 原始文件如下:
      
  3. 要求输出的格式如下:
     
  4. 思路分析:
       ⑴我们从上面可以现在我们知道A-O每个人拥有哪些好友,但是我们现在是要找出两两之间的人有哪些共同好友。那么
           我们可以逆向思维,第一步找出哪些好友拥有A,哪些好友拥有B.....依次找出,结果如下:
           
       ⑵通过得出上面的数据后,我们可以对后面的好友进行排序,避免重复,将 “拥有这名朋友的所有人”进行两两配对,并将配对后的         字符串当做键,“朋友”当做值输出,即输出<人-人,共同朋友>
           
  5. 代码实现,通过两次job运算
     a:FriendMapper01

    package com.kgf.mapreduce.friend;import java.io.IOException;import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;public class FriendMapper01 extends Mapper<LongWritable, Text, Text, Text>{Text k  =new Text();Text v  =new Text();@Overrideprotected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {//1:获取一行数据String line = value.toString();//2:对一行数据进行切割String[] fields = line.split(":");String person = fields[0];String[] friends = fields[1].split(",");for (String friend : friends) {k.set(friend);v.set(person);context.write(k, v);}}
    }
    

    b:FriendReducer

    package com.kgf.mapreduce.friend;import java.io.IOException;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;public class FriendReducer extends Reducer<Text, Text, Text, Text> {@Overrideprotected void reduce(Text key, Iterable<Text> values,Context context)throws IOException, InterruptedException {StringBuffer sb = new StringBuffer();//1:获取哪些好友都有对应的人for (Text text : values) {sb.append(text.toString()+",");}sb.deleteCharAt(sb.length()-1);context.write(key, new Text(sb.toString()));}
    }
    

    c:FriendDriver01

    package com.kgf.mapreduce.friend;import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class FriendDriver01 {public static void main(String[] args) throws Exception {//1:获取Job对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//2:设置jarjob.setJarByClass(FriendDriver01.class);//3:关联Mapper和reducerjob.setMapperClass(FriendMapper01.class);job.setReducerClass(FriendReducer.class);//4:设置mapper输出参数job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);//5:设置最终输出job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//6:设置文件输入输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));//7:提交boolean result = job.waitForCompletion(true);System.exit(result?0:1);}}
    

    d:FriengMapper02

    package com.kgf.mapreduce.friend;import java.io.IOException;
    import java.util.Arrays;import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;public class FriengMapper02 extends Mapper<LongWritable, Text, Text, Text>{@Overrideprotected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {//1:获取一行String line = value.toString();//2:切割数据String[] fileds = line.split("\t");String friend = fileds[0];String[] persons = fileds[1].split(",");Arrays.sort(persons);//排序for (int i = 0; i < persons.length; i++) {for (int j = i+1; j < persons.length; j++) {context.write(new Text(persons[i]+"-"+persons[j]),new Text(friend));}}}}
    

    e:FriendReducer2

    package com.kgf.mapreduce.friend;import java.io.IOException;
    import java.util.HashSet;import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;public class FriendReducer2 extends Reducer<Text, Text, Text, Text> {@Overrideprotected void reduce(Text key, Iterable<Text> values,Context context)throws IOException, InterruptedException {StringBuffer sb = new StringBuffer();HashSet<String> set = new HashSet<String>();for (Text value : values) {String v = value.toString();if(!set.contains(v)) {set.add(v);sb.append(v).append(",");}}sb.deleteCharAt(sb.length()-1);context.write(key, new Text(sb.toString()));}
    }
    

    f:FriendDriver2

    package com.kgf.mapreduce.friend;import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class FriendDriver2 {public static void main(String[] args) throws Exception {//1:获取Job对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//2:设置jarjob.setJarByClass(FriendDriver2.class);//3:关联Mapper和reducerjob.setMapperClass(FriengMapper02.class);job.setReducerClass(FriendReducer2.class);//4:设置mapper输出参数job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);//5:设置最终输出job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//6:设置文件输入输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));//7:提交boolean result = job.waitForCompletion(true);System.exit(result?0:1);}}
    

MapReduce实现寻找共同好友相关推荐

  1. 用MapReduce实现寻找共同好友(Python)

    1.需求: 给出A-O个人中每个人的好友列表(好友单向),求出哪些人两两之间有共同好友,以及他们的共同好友都有谁. 好友列表数据: A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I ...

  2. 使用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:B,C ...

  3. python版mapreduce题目实现寻找共同好友

    看到一篇不知道是好好玩还是好玩玩童鞋的博客,发现一道好玩的mapreduce题目,地址http://www.cnblogs.com/songhaowan/p/7239578.html 如图 由于自己太 ...

  4. HadoopMapReduce寻找共同好友

    假设有所有用户的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的),如果两个用户之间存在共同好友,需要找出他们之间的共同好友. 样例文本如下: A:B,C,D,F,E ...

  5. MapReduce学习笔记(7)—— 寻找共同好友

    1 数据 冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的).求出哪些人两两之间有共同好友,及他俩的共同好友都有谁? A:B,C,D,F,E,O B:A,C,E,K C:F,A,D ...

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

  7. 黑猴子的家:MapReduce 找微信共同好友分析

    1.数据 https://www.jianshu.com/p/1613f171f466 2.需求 以上是微信的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的) 求出 ...

  8. 算法系列讲解之:社交网络之共同好友模型讲解

    问题导读 1.寻找共同好友,该如何转换为程序逻辑? 2.寻找共同好友的思路是什么? 3.如何通过MapReduce实现寻找共同好友? 我们知道社交网络经常会看到共同好友,共同好友目前资料也非常的多,也 ...

  9. MapReduce实现好友单向推荐

    需求: 现有一份QQ好友数据,其中数据第一列代表用户QQ昵称,第二列代表好友QQ昵称.要求编写MapReduce程序实现QQ好友推荐,例如A的好友是B,B的好友是C,即A与C有共同的好友B,则可以向A ...

最新文章

  1. go和python计算字节数组sha1
  2. GDCM:gdcm::PNMCodec的测试程序
  3. Python中类方法、类实例方法、静态方法,私有属性和私有方法有何区别?
  4. RedHat下JDK1.6安装-利用alternative实现多版本并存(Ubuntu同理)
  5. linux sed删除指定行_shell三剑客之sed!
  6. android ListView控件滑动时出现黑色背景问题解法方案
  7. L2-001. 紧急救援-PAT团体程序设计天梯赛GPLT(Dijkstra算法)
  8. duilib中各控件响应的消息类型
  9. 北斗导航 | 北斗高精度定位在智能驾驶汽车领域的应用
  10. 编写程序:5类员工有对应封装类,创建Employee数组,若干不同的Employee对象,并实现增删改查功能(《黑马程序员》P144编程题加强版)
  11. 面试官:生产环境中 CPU 利用率飙高怎么办?
  12. 台式计算机如何连接手机热点上网,台式机如何使用手机热点上网
  13. Raft一致性算法论文
  14. 飞思卡尔 I.MX6Q-高分辨率(1080P)视频采集编码
  15. 磨金石教育设计干货分享|20个海报设计小技巧,果断打包带走
  16. CNC+CRC/SoftPLC/OpenCASCADE/CAD/CAM开源项目收藏
  17. 东莞市中考计算机考试试题,东莞市数学中考真题(共7份word版,含答案).doc
  18. 分享148个ASP源码,总有一款适合您
  19. 史上最强算法论战:请不要嘻哈,这是哈希
  20. dom4j 解析xml 获取节点值和节点属性

热门文章

  1. 四元数---基本概念(转载)
  2. 江苏大学885程序设计
  3. 《人类简史:从动物到上帝》《中西…
  4. 专访中银金科:数字驱动成为新的增长引擎,未来业务转化是关键
  5. 陈天奇任CTO,TVM团队成立OctoML:让任何硬件都能部署机器学习模型
  6. 自然常数e与重要极限
  7. Ubuntu18.04 命令行打开计算器
  8. 以计算机写一篇作文500字,有关电脑作文500字3篇
  9. 一款BLE-MIDI的蓝牙MIDI模块
  10. C\C++真桌面贪吃蛇,桌面操控,有音乐音效“详解”