逻辑分析

以下是qq的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的)

  1. A:B,C,D,F,E,O
  2. B:A,C,E,K
  3. C:F,A,D,I
  4. D:A,E,F,L
  5. E:B,C,D,M,L
  6. F:A,B,C,D,E,O,M
  7. G:A,C,D,E,F
  8. H:A,C,D,E,O
  9. I:A,O
  10. J:B,O
  11. K:A,C,D
  12. L:D,E,F
  13. M:E,F,G
  14. O:A,H,I,J

求出哪些人两两之间有共同好友,及他俩的共同好友都有谁?

解题思路:

  1. 第一步
  2. map
  3. 读一行 A:B,C,D,F,E,O
  4. 输出 <B,A><C,A><D,A><F,A><E,A><O,A>
  5. 在读一行 B:A,C,E,K
  6. 输出 <A,B><C,B><E,B><K,B>
  7.  
  8. REDUCE
  9. 拿到的数据比如<C,A><C,B><C,E><C,F><C,G>......
  10.  
  11. 输出:
  12. <A-B,C>
  13. <A-E,C>
  14. <A-F,C>
  15. <A-G,C>
  16. <B-E,C>
  17. <B-F,C>.....
  18.  
  19. 第二步
  20. map
  21. 读入一行<A-B,C>
  22. 直接输出<A-B,C>
  23. reduce
  24. 读入数据 <A-B,C><A-B,F><A-B,G>.......
  25. 输出: A-B C,F,G,.....

第一步:代码实现

  1. public class ComonsFriendsStepOne extends Configured implements Tool {
  2. @Override
  3. public int run(String[] args) throws Exception {
  4. Configuration conf = super.getConf();
  5. Job job = Job.getInstance(conf, ComonsFriendsStepOne.class.getSimpleName());
  6. job.setInputFormatClass(TextInputFormat.class);
  7. TextInputFormat.addInputPath(job,new Path("file:///F:\\\input"));
  8. job.setMapperClass(ComonsFriendsStepOneMapper.class);
  9. job.setMapOutputKeyClass(Text.class);
  10. job.setMapOutputValueClass(Text.class);
  11. job.setReducerClass(ComonsFriendsStepOneReducer.class);
  12. job.setOutputKeyClass(Text.class);
  13. job.setOutputValueClass(Text.class);
  14. job.setOutputFormatClass(TextOutputFormat.class);
  15. TextOutputFormat.setOutputPath(job,new Path("file:///F:\\output"));
  16. boolean b = job.waitForCompletion(true);
  17. return b?0:1;
  18. }
  19. public static class ComonsFriendsStepOneMapper extends Mapper<LongWritable,Text,Text,Text>{
  20. @Override
  21. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  22. String[] split = value.toString().split(":");
  23. String person = split[0];
  24. String[] friends = split[1].split(",");
  25. for (String friend : friends) {
  26. context.write(new Text(friend),new Text(person));
  27. }
  28. }
  29. }
  30. public static class ComonsFriendsStepOneReducer extends Reducer<Text,Text,Text,Text>{
  31. @Override
  32. protected void reduce(Text friend, Iterable<Text> persons, Context context) throws IOException, InterruptedException {
  33. StringBuffer buffer = new StringBuffer();
  34. for (Text person : persons) {
  35. buffer.append(person).append("-");
  36. }
  37. context.write(friend,new Text(buffer.toString()));
  38. }
  39. }
  40. public static void main(String[] args) throws Exception {
  41. Configuration configuration = new Configuration();
  42. ToolRunner.run(configuration,new ComonsFriendsStepOne(),args);
  43. }
  44. }

第二步:代码实现

  1. public class ComonsFriendsStepTwo extends Configured implements Tool {
  2. @Override
  3. public int run(String[] args) throws Exception {
  4.  
  5. Job job = Job.getInstance(super.getConf(), ComonsFriendsStepTwo.class.getSimpleName());
  6. job.setInputFormatClass(TextInputFormat.class);
  7. TextInputFormat.addInputPath(job,new Path("file:///F:\\output"));
  8. job.setMapOutputKeyClass(Text.class);
  9. job.setMapOutputValueClass(Text.class);
  10. job.setMapperClass(ComonsFriendStepTwoMapper.class);
  11. job.setReducerClass(ComonsFriendStepTwoReducer.class);
  12. job.setOutputKeyClass(Text.class);
  13. job.setOutputValueClass(Text.class);
  14.  
  15. job.setOutputFormatClass(TextOutputFormat.class);
  16. TextOutputFormat.setOutputPath(job,new Path("file:///F:\\outstep2"));
  17. boolean b = job.waitForCompletion(true);
  18. return b?0:1;
  19. }
  20. public static class ComonsFriendStepTwoMapper extends Mapper<LongWritable,Text,Text,Text>{
  21.  
  22. /**
  23. * A F-D-O-I-H-B-K-G-C-
  24. * B E-A-J-F-
  25. * C K-A-B-E-F-G-H-
  26. */
  27. @Override
  28. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  29. String[] split = value.toString().split("\t");
  30. String friends = split[0];
  31. String[] persons = split[1].split("-");
  32. //排序,避免c-b 与b-c 这样的情况出现
  33. Arrays.sort(persons);
  34. for(int i =0;i< persons.length -1 ;i++){
  35. for(int j = i+1;j<persons.length;j++){
  36. context.write(new Text(persons[i]+"-"+persons[j]),new Text(friends));
  37. }
  38.  
  39. }
  40. }
  41. }
  42. public static class ComonsFriendStepTwoReducer extends Reducer<Text,Text,Text,Text>{
  43. @Override
  44. protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
  45. StringBuffer buffer = new StringBuffer();
  46. for (Text value : values) {
  47. buffer.append(value.toString()+"\t");
  48. }
  49. context.write(key,new Text(buffer.toString()));
  50. }
  51. }
  52. public static void main(String[] args) throws Exception {
  53. ToolRunner.run(new Configuration(),new ComonsFriendsStepTwo(),args);
  54. }
  55. } 

扩展:求互粉的人。

mapreduce求共同好友的更多相关文章

  1. 用Mapreduce求共同好友

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

  2. Hadoop阅读笔记(二)——利用MapReduce求平均数和去重

    前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...

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

  4. Mapreduce求气温值项目

    Mapreduce前提工作 简单的来说map是大数据,reduce是计算<运行时如果数据量不大,但是却要分工做这就比较花时间了> 首先想要使用mapreduce,需要在linux中进行一些 ...

  5. Hadoop学习之路(二十)MapReduce求TopN

    前言 在Hadoop中,排序是MapReduce的灵魂,MapTask和ReduceTask均会对数据按Key排序,这个操作是MR框架的默认行为,不管你的业务逻辑上是否需要这一操作. 技术点 MapR ...

  6. Hadoop 学习笔记 (十一) MapReduce 求平均成绩

    china:张三 78李四 89王五 96赵六 67english张三 80李四 82王五    84赵六 86math张三 88李四 99王五 66赵六 77 import java.io.IOEx ...

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

  8. MapReduce求最大值最小值问题

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

  9. mapreduce求平均数

    1. 现有某电商关于商品点击情况的数据文件,表名为goods_click,包含两个字段(商品分类,商品点击次数),分隔符“     ”,由于数据很大,所以为了方便统计我们只截取它的一部分数据,内容如下 ...

随机推荐

  1. JS谷歌浏览器断点调试

    1.找到对应的文件 按F12打开网页调试工具,默认打开的是Elements,显示的是网页标签元素.选择Source,在左侧找到对应的js代码文件(这里是在page标签上找到的) 1.1.如何找到web ...

  2. 深入理解js——非构造函数的继承

    原文来自阮一峰日志(http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.ht ...

  3. python中chr()函数和ord()函数的用法

    一,chr()函数 格式:Chr(<数值表达式>)   说明:函数返回值类型为String,其数值表达式值取值范围为0~255.   例如:Print Chr(78),结果显示:N.   ...

  4. 小程序 css 文字溢出,长度过长用 。。。

    word-break: break-all;/*允许在单词内换行*/ text-align: left; /* line-height: 45rpx; */ text-overflow: -o-ell ...

  5. selenium2-java 浏览器cookie的获取

    //成功登陆后增加如下代码        File cookieFile = new File("C:\\tmp\\tangdai.cookie.txt");           ...

  6. jenkins-参数化构建插件:Choice Parameter

    参考: 谢谢大佬的总结: https://www.cnblogs.com/zhaojingyu/p/9862371.html 使用方式 step1: 添加参数,选择Choice Parameter,并 ...

  7. PAT_A1073#Scientific Notation

    Source: PAT A1073 Scientific Notation (20 分) Description: Scientific notation is the way that scient ...

  8. JQuery获取与设置select

    获取select : 1.获取select 选中的 text :    $("#ddlregtype").find("option:selected").tex ...

  9. leetcode.分治.241为运算表达式设计优先级-Java

    1. 具体题目 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: & ...

  10. 44-python基础-python3-字符串-常用字符串方法(二)-isalpha()-isalnum()-isdigit()-isspace()-istitle()

    3-isX 字符串方法   序号 方法 条件 返回结果1 返回结果2 1 isalpha() 如果字符串只包含字母,并且非空; True False 2 isalnum() 如果字符串只包含字母和数字 ...