分享

优酷hadoop,mapred面试题及答案

gefieder 发表于 2015-1-8 13:01:17 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 27 66660





mapred找共同朋友,数据格式如下
  1. A B C D E F
  2. B A C D E
  3. C A B E
  4. D A B E
  5. E A B C D
  6. F A
复制代码



第一字母表示本人,其他是他的朋友,找出有共同朋友的人,和共同朋友是谁

答案如下
  1. import java.io.IOException;
  2. import java.util.Set;
  3. import java.util.StringTokenizer;
  4. import java.util.TreeSet;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.Mapper.Context;
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.util.GenericOptionsParser;
  15. public class FindFriend {
  16.        
  17.           public static class ChangeMapper extends Mapper<Object, Text, Text, Text>{                      
  18.                    @Override
  19.                    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  20.                              StringTokenizer itr = new StringTokenizer(value.toString());
  21.                                  Text owner = new Text();
  22.                                  Set<String> set = new TreeSet<String>();
  23.                              owner.set(itr.nextToken());
  24.                              while (itr.hasMoreTokens()) {
  25.                                      set.add(itr.nextToken());
  26.                              }             
  27.                              String[] friends = new String[set.size()];
  28.                              friends = set.toArray(friends);
  29.                              
  30.                              for(int i=0;i<friends.length;i++){
  31.                                      for(int j=i+1;j<friends.length;j++){
  32.                                              String outputkey = friends[i]+friends[j];
  33.                                              context.write(new Text(outputkey),owner);
  34.                                      }                                     
  35.                              }
  36.                    }
  37.           }
  38.           
  39.           public static class FindReducer extends Reducer<Text,Text,Text,Text> {                         
  40.                         public void reduce(Text key, Iterable<Text> values,
  41.                                         Context context) throws IOException, InterruptedException {
  42.                                   String  commonfriends ="";
  43.                               for (Text val : values) {
  44.                                   if(commonfriends == ""){
  45.                                           commonfriends = val.toString();
  46.                                   }else{
  47.                                           commonfriends = commonfriends+":"+val.toString();
  48.                                   }
  49.                                }
  50.                               context.write(key, new Text(commonfriends));                               
  51.                         }                         
  52.           }
  53.           
  54.         public static void main(String[] args) throws IOException,
  55.         InterruptedException, ClassNotFoundException {
  56.                
  57.             Configuration conf = new Configuration();
  58.             String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  59.             if (otherArgs.length < 2) {
  60.               System.err.println("args error");
  61.               System.exit(2);
  62.             }
  63.             Job job = new Job(conf, "word count");
  64.             job.setJarByClass(FindFriend.class);
  65.             job.setMapperClass(ChangeMapper.class);
  66.             job.setCombinerClass(FindReducer.class);
  67.             job.setReducerClass(FindReducer.class);
  68.             job.setOutputKeyClass(Text.class);
  69.             job.setOutputValueClass(Text.class);
  70.             for (int i = 0; i < otherArgs.length - 1; ++i) {
  71.               FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
  72.             }
  73.             FileOutputFormat.setOutputPath(job,
  74.               new Path(otherArgs[otherArgs.length - 1]));
  75.             System.exit(job.waitForCompletion(true) ? 0 : 1);
  76.                
  77.         }
  78. }
复制代码




运行结果
  1. AB      E:C:D
  2. AC      E:B
  3. AD      B:E
  4. AE      C:B:D
  5. BC      A:E
  6. BD      A:E
  7. BE      C:D:A
  8. BF      A
  9. CD      E:A:B
  10. CE      A:B
  11. CF      A
  12. DE      B:A
  13. DF      A
  14. EF      A
复制代码




欢迎加入about云群371358502、39327136,云计算爱好者群,亦可关注about云腾讯认证空间||关注本站微信

已有(27)人评论

跳转到指定楼层
民谊玉超 发表于 2016-9-22 16:59:55
进击的巨人 发表于 2016-7-26 21:38
reduce的部分  怎样实现寻找重叠的部分(即共同的好友),  在reduce部分commomdfriend是怎样找到value中重 ...

个人觉得:此算法,就是先从每行的朋友中两两组合,找到他们的共同朋友,即第一行的bc、bd、be....的共同好友就是a
输入:A B C D E F
          B A C D E
          C A B E
          D A B E
          E A B C D
          F
A的map  输出:
          key:BC,value:A
          Key:BD,value:A
          key:BE,value:A
          ...........
Reduce 输入:
          key:BC,value[{A}]
          key:BD,value[{A,E}]
Reduce 输出:
          就是讲各个value进行累加



补充内容 (2016-12-23 10:23):
如果好友关系是单向的,这个方法不成立
回复

使用道具 举报

蒲公英_gJM1Z 发表于 2015-4-3 21:49:13
上面的问题有没有考虑每一个好友只有一个朋友
回复

使用道具 举报

msylctt 发表于 2015-6-27 14:30:56
输入:A B C D E F
          B A C D E
          C A B E
          D A B E
          E A B C D
          F Amap  输出:
          key:AB,value:C.D.E.F
          Key:AC,value:B,D,E,F
          key:AD,value:B,C,E,F
          ...........
          KEY:AF,value null
Reduce step:
          key:AB,value[{C,D,E,F},{C,D,E}]=>AB overlap 2 set =>{C,D,E}

回复

使用道具 举报

arBen 发表于 2015-1-9 08:57:39
顶楼主,顶上去让更多人看到
回复

使用道具 举报

懒惰的穷人 发表于 2015-1-29 21:54:12
顶,楼主好人啊
回复

使用道具 举报

凌飞羽 发表于 2015-3-29 22:26:54
回复

使用道具 举报

尘世随缘 发表于 2015-4-1 16:09:09
思路很好,学习了!
回复

使用道具 举报

ainubis 发表于 2015-4-5 12:41:47
回复

使用道具 举报

123下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条