`
乡里伢崽
  • 浏览: 108899 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

mapreduce '找共同朋友',面试题

 
阅读更多
mapred找共同朋友,数据格式如下:

引用

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


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



答案如下:


import java.io.IOException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class FindFriend {
        
          public static class ChangeMapper extends Mapper<Object, Text, Text, Text>{                      
                   @Override
                   public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
                             StringTokenizer itr = new StringTokenizer(value.toString());
                                 Text owner = new Text();
                                 Set<String> set = new TreeSet<String>();
                             owner.set(itr.nextToken());
                             while (itr.hasMoreTokens()) {
                                     set.add(itr.nextToken());
                             }             
                             String[] friends = new String[set.size()];
                             friends = set.toArray(friends);
                             
                             for(int i=0;i<friends.length;i++){
                                     for(int j=i+1;j<friends.length;j++){
                                             String outputkey = friends[i]+friends[j];
                                             context.write(new Text(outputkey),owner);
                                     }                                     
                             }
                   }
          }
          
          public static class FindReducer extends Reducer<Text,Text,Text,Text> {                          
                        public void reduce(Text key, Iterable<Text> values, 
                                        Context context) throws IOException, InterruptedException {
                                  String  commonfriends =""; 
                              for (Text val : values) {
                                  if(commonfriends == ""){
                                          commonfriends = val.toString();
                                  }else{
                                          commonfriends = commonfriends+":"+val.toString();
                                  }
                               }
                              context.write(key, new Text(commonfriends));                                
                        }                          
          }
          

        public static void main(String[] args) throws IOException,
        InterruptedException, ClassNotFoundException {
                
            Configuration conf = new Configuration();
            String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
            if (otherArgs.length < 2) {
              System.err.println("args error");
              System.exit(2);
            }
            Job job = new Job(conf, "word count");
            job.setJarByClass(FindFriend.class);
            job.setMapperClass(ChangeMapper.class);
            job.setCombinerClass(FindReducer.class);
            job.setReducerClass(FindReducer.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            for (int i = 0; i < otherArgs.length - 1; ++i) {
              FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
            }
            FileOutputFormat.setOutputPath(job,
              new Path(otherArgs[otherArgs.length - 1]));
            System.exit(job.waitForCompletion(true) ? 0 : 1);
                
        }

}





运行结果:

AB      E:C:D
AC      E:B
AD      B:E
AE      C:B:D
BC      A:E
BD      A:E
BE      C:D:A
BF      A
CD      E:A:B
CE      A:B
CF      A
DE      B:A
DF      A
EF      A

分享到:
评论

相关推荐

    MapReduce经典常见面试实操题

    MapReduce经典常见面试实操题,面试题,实操题,练习题,练手题

    mapreduce练习资源

    该资源中中包含MapReduce的练习题,可以是你同过这些练习题更好的掌握MapReduce

    mapreduce mapreduce mapreduce

    mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce ...

    大数据工作面试练习题 BAT大数据面试题 Hadoop、kafka、HDFS、Spark、MapReduce 共19页.pdf

    大数据工作面试练习题 2018最新BAT大数据面试题 Hadoop、kafka、HDFS、Spark、MapReduce 【内容大纲-共25道题目】 1、kafka的message包括哪些信息? 3 2、怎么查看kafka的offset? 3 3、hadoop的shuffle过程是? 3 ...

    Hadoop中的HDFS和Mapreduce

    Hadoop中的HDFS和Mapreduce详细的讲解,原理以及样例

    Hadoop 100道基础面试题

    关于hadoop的一些基础面试题,包括架构、源码、mapreduce、hdfs的基础面试题。提供一些基础的面试知识kaodian

    大数据组件 Spark 面试题 + Spark 高频面试题

    里面有 Spark 的部署方式,spark core、spark sql、spark streaming 等组件的各种面试题,包括但不限于宽窄依赖、缓存、数据广播、shuffle以及数据倾斜的查看和解决方式等等 对于大数据(离线、在线)面试的同学还是...

    python hadoop mapreduce 相似用户|mapreduce.rar

    使用python写微博关注者之间,相似度最高的十个用户的mapreduce,代码有点粗糙,主要是给大家提供思路的,不同的问题是需要修改的。

    MapReduce基础

    MapReduce基础

    实验项目 MapReduce 编程

    4 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后查看 MapReduce Web 界面。 5. 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后练习 MapReduce Shell 常用命令。 。。

    大数据知识、面试题 HDFS、Mapreduce、hive

    kafka 数据丢失问题,及如何保证1. 数据丢失: a.acks=1 的时候(只保证写入 leader 成功),如果刚好 leader 挂了.数据会丢失. b. Acks=0 的时候, 使用异步模式的时候,该模式下 kafka 无法保证消息,有可能会丢。...

    MapReduce发明人关于MapReduce的介绍

    MapReduce发明人关于MapReduce的介绍

    学生mapreduce成绩分析

    学生mapreduce成绩分析

    大数据面试题.docx

    a)它是 NameNode 的热备 b)它对内存没有要求 c)它的目的是帮助 NameNode 合并编辑日志,减少 NameNode 启动时间 d)SecondaryNameNode 应与 NameNode 部署到一个节点 大数据面试题全文共16页,当前为第2页。...

    hadoop面试题

    面试hadoop可能被问到的问题你能回答出几个 ?...5、面试的人给你出一些问题,让你用mapreduce来实现? 比如:现在有10个文件夹,每个文件夹都有1000000个url.现在让你找出top1000000url。 6、hadoop中Combiner的作用?

    图解MapReduce.doc

    图解MapReduce,系统介绍Hadoop MapReduce工作过程原理

    大数据面试题-.docx

     a)Martin Fowler 大数据面试题-全文共15页,当前为第1页。 b)Kent Beck c)Doug cutting 5. HDFS 默认 Block Size a)32MB b)64MB c)128MB 6. 下列哪项通常是集群的最主要瓶颈 a)CPU b)网络 c)磁盘 d)内存 7. 关于 ...

    【MapReduce篇07】MapReduce之数据清洗ETL1

    【MapReduce篇07】MapReduce之数据清洗ETL1

    大数据Spark面试题汇总

    大数据Spark面试题汇总,共有79道面试题以及题目的解答 部分题目如下: 1. spark 的有几种部署模式,每种模式特点? 2. Spark 为什么比 mapreduce 快? 3. 简单说一下 hadoop 和 spark 的 shuffle 相同和差异? 5. ...

    Hadoop mapreduce实现wordcount

    Hadoop 用mapreduce实现Wordcount实例,绝对能用

Global site tag (gtag.js) - Google Analytics