分享

hadoop面试题,测试一下你的实力

 
pig2 发表于 2013-11-10 23:18:07 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 62 137727
本帖最后由 pig2 于 2014-2-8 23:47 编辑

1 使用Hive或者自定义MR实现如下逻辑
product_no      lac_id  moment  start_time      user_id county_id       staytime        city_id
13429100031     22554   8       2013-03-11 08:55:19.151754088   571     571     282     571
13429100082     22540   8       2013-03-11 08:58:20.152622488   571     571     270     571
13429100082     22691   8       2013-03-11 08:56:37.149593624   571     571     103     571
13429100087     22705   8       2013-03-11 08:56:51.139539816   571     571     220     571
13429100087     22540   8       2013-03-11 08:55:45.150276800   571     571     66      571
13429100082     22540   8       2013-03-11 08:55:38.140225200   571     571     133     571
13429100140     26642   9       2013-03-11 09:02:19.151754088   571     571     18      571
13429100082     22691   8       2013-03-11 08:57:32.151754088   571     571     287     571
13429100189     22558   8       2013-03-11 08:56:24.139539816   571     571     48      571
13429100349     22503   8       2013-03-11 08:54:30.152622440   571     571     211     571
字段解释:
product_no:用户手机号;
lac_id:用户所在基站;
start_time:用户在此基站的开始时间;
staytime:用户在此基站的逗留时间。

需求描述:
根据lac_id和start_time知道用户当时的位置,根据staytime知道用户各个基站的逗留时长。根据轨迹合并连续基站的staytime。
最终得到每一个用户按时间排序在每一个基站驻留时长

期望输出举例:
13429100082     22540   8       2013-03-11 08:58:20.152622488   571     571     270     571
13429100082     22691   8       2013-03-11 08:56:37.149593624   571     571     390     571
13429100082     22540   8       2013-03-11 08:55:38.140225200   571     571     133     571
13429100087     22705   8       2013-03-11 08:56:51.139539816   571     571     220     571
13429100087     22540   8       2013-03-11 08:55:45.150276800   571     571     66      571

2 Linux脚本能力考察
2.1 请随意使用各种类型的脚本语言实现:批量将指定目录下的所有文件中的$HADOOP_HOME$替换成/home/ocetl/app/hadoop

2.2 假设有10台主机,H1到H10,在开启SSH互信的情况下,编写一个或多个脚本实现在所有的远程主机上执行脚本的功能
例如:runRemoteCmd.sh "ls -l"
期望结果:
H1:
XXXXXXXX
XXXXXXXX
XXXXXXXX
H2:
XXXXXXXX
XXXXXXXX
XXXXXXXX
H3:
...





3 Hadoop基础知识与问题分析的能力
3.1 描述一下hadoop中,有哪些地方使用了缓存机制,作用分别是什么

3.2 请描述https://issues.apache.org/jira/browse/HDFS-2379说的是什么问题,最终解决的思路是什么?


4 MapReduce开发能力
请参照wordcount实现一个自己的map reduce,需求为:
    a 输入文件格式:
       xxx,xxx,xxx,xxx,xxx,xxx,xxx
    b 输出文件格式:
       xxx,20
       xxx,30
       xxx.40
    c 功能:根据命令行参数统计输入文件中指定关键字出现的次数,并展示出来
       例如:hadoop jar xxxxx.jar keywordcount xxx,xxx,xxx,xxx(四个关键字)

5 MapReduce优化
请根据第五题中的程序, 提出如何优化MR程序运行速度的思路

6 Linux操作系统知识考察
请列举曾经修改过的/etc下的配置文件,并说明修改要解决的问题?


7 Java开发能力
7.1 写代码实现1G大小的文本文件,行分隔符为\x01\x02,统计一下该文件中的总行数,要求注意边界情况的处理

7.2 请描述一下在开发中如何对上面的程序进行性能分析,对性能进行优化的过程

回帖中答案可参考

可以参考另外一套面试题
另一套面试题答案
来自群组: Hadoop技术组

已有(62)人评论

跳转到指定楼层
SHARKS 发表于 2013-11-11 13:31:19
不错啊,就是不会
回复

使用道具 举报

pig2 发表于 2013-11-11 13:37:59
本帖最后由 pig2 于 2014-2-6 22:10 编辑
SHARKS 发表于 2013-11-11 13:31
不错啊,就是不会

这是about云(39327136)群友提供,有什么有问题的地方大家一起讨论一下。

1. 考虑后,决定使用 MR 来实现,于是使用Java,用一个MR Job完成这个事情:
  1. package org.aboutyun;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.LongWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  14. import java.io.IOException;
  15. import java.text.ParseException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.Comparator;
  20. public class TimeCount {
  21.     public static void main(String[] args) throws Exception {
  22.         Configuration conf = new Configuration();
  23.         Job job = new Job(conf, "time_count");
  24.         job.setOutputKeyClass(Text.class);
  25.         job.setOutputValueClass(Text.class);
  26.         job.setMapperClass(Map.class);
  27.         job.setReducerClass(Reduce.class);
  28.         job.setInputFormatClass(TextInputFormat.class);
  29.         job.setOutputFormatClass(TextOutputFormat.class);
  30.         FileInputFormat.addInputPath(job, new Path(args[0]));
  31.         FileOutputFormat.setOutputPath(job, new Path(args[1]));
  32.         job.waitForCompletion(true);
  33.     }
  34.     public static class Map extends Mapper<LongWritable, Text, Text, Text> {
  35.         private Text id = new Text();
  36.         private Text row = new Text();
  37.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  38.             String line = value.toString();
  39.             String[] items = line.split("\t");
  40.             if (items.length == 8) {
  41.                 if (StringUtils.isNumeric(items[6])) {
  42.                     id.set(items[0] + "\t" + items[1]);
  43.                     row.set(line);
  44.                     context.write(id, row);
  45.                 }
  46.             } else {
  47.                 System.out.println("Wrong length: " + items.length);
  48.             }
  49.         }
  50.     }
  51.     public static class Reduce extends Reducer<Text, Text, Text, Text> {
  52.         private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  53.         static {
  54.             format.setLenient(false);
  55.         }
  56.         private Text rest = new Text();
  57.         public void reduce(Text key, Iterable<Text> values, Context context)
  58.                 throws IOException, InterruptedException {
  59.             //  Parse row to Record
  60.             ArrayList<Record> list = new ArrayList<Record>();
  61.             for (Text row : values) {
  62.                 String[] items = row.toString().split("\t");
  63.                 try {
  64.                     Record record = new Record();
  65.                     record.items = items;
  66.                     record.start_time = format.parse(items[3]).getTime();
  67.                     record.stay_time = Long.parseLong(items[6]) * 1000;
  68.                     list.add(record);
  69.                 } catch (ParseException e) {
  70.                     e.printStackTrace();
  71.                 }
  72.             }
  73.             //  Sort
  74.             Collections.sort(list, new Comparator<Record>() {
  75.                 @Override
  76.                 public int compare(Record r1, Record r2) {
  77.                     return (int) (r1.start_time - r2.start_time);
  78.                 }
  79.             });
  80.             //  Find and merge slice
  81.             ArrayList<Record> result = new ArrayList<Record>();
  82.             for (Record r1 : list) {
  83.                 boolean found = false;
  84.                 long r1_stop_time = r1.start_time + r1.stay_time;
  85.                 for (Record r2 : result) {
  86.                     long r2_stop_time = r2.start_time + r2.stay_time;
  87.                     if (r1.start_time > r2.start_time && r1.start_time <= r2_stop_time && r1_stop_time > r2_stop_time) {
  88.                         //  merge the new slice
  89.                         r2.stay_time = r1_stop_time - r2.start_time;
  90.                         found = true;
  91.                     }
  92.                 }
  93.                 if (!found) {
  94.                     result.add(r1);
  95.                 }
  96.             }
  97.             //  Output
  98.             for (Record r : result) {
  99.                 key.set(r.items[0]);
  100.                 String value = r.items[1] + "\t"
  101.                         + r.items[2] + "\t"
  102.                         + r.items[3] + "\t"
  103.                         + r.items[4] + "\t"
  104.                         + r.items[5] + "\t"
  105.                         + (r.stay_time / 1000) + "\t"
  106.                         + r.items[6] + "\t";
  107.                 rest.set(value);
  108.                 context.write(key, rest);
  109.             }
  110.         }
  111.         static class Record {
  112.             String[] items;
  113.             long start_time;
  114.             long stay_time;
  115.         }
  116.     }
  117. }
复制代码
2.

2.1 使用 find + sed 来实现:
find /home/ocetl/app/hadoop -exec sed -i 's/\$HADOOP_HOME\$/\/home\/ocetl\/app\/hadoop/g' {} \;
2.2 直接使用ssh的参数
  1. #!/bin/bash
  2. if [ $# -ne 1 ]
  3. then
  4.         echo "Usage: `basename $0` {command}"
  5.         exit
  6. fi
  7. for i in H1 H2 H3 H4 H5 H6 H7 H8 H9 H10
  8. do
  9.         echo "$i:"
  10.         ssh $i "$1"
  11. done
复制代码
3.
3.1 不了解,HDFS用了缓存
3.2 问题是当硬盘空间很大,而内存页面缓存很少的时候,DN的Block report需要很长时间生成,而此时 FSVolumeSet 锁是锁住的,因此所有读写操作都无法执行,最终导致那些操作超时。此问题是建议提供一种方法使block report不需要持有FSVolumeSet锁,从而不会导致那些任务失败。

4. 只是替换分隔符从空格到逗号,以及增加搜索关键字列表:
  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.Path;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.LongWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Job;
  7. import org.apache.hadoop.mapreduce.Mapper;
  8. import org.apache.hadoop.mapreduce.Reducer;
  9. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  10. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. public class WordCount {
  16.     public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
  17.         private final static IntWritable one = new IntWritable(1);
  18.         private Text word = new Text();
  19.         private final static ArrayList<String> target_words = new ArrayList<String>();
  20.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  21.             String[] items = value.toString().toLowerCase().replaceAll("\\p{Punct}", "").split("\\s+");
  22.             for (String item : items) {
  23.                 if (target_words.contains(item)) {
  24.                     word.set(item);
  25.                     context.write(word, one);
  26.                 }
  27.             }
  28.         }
  29.         public static void clear() {
  30.             target_words.clear();
  31.         }
  32.         public static void add(String word) {
  33.             target_words.add(word);
  34.         }
  35.     }
  36.     public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
  37.         public void reduce(Text key, Iterable<IntWritable> values, Context context)
  38.                 throws IOException, InterruptedException {
  39.             int sum = 0;
  40.             for (IntWritable val : values) {
  41.                 sum += val.get();
  42.             }
  43.             context.write(key, new IntWritable(sum));
  44.         }
  45.     }
  46.     public static void main(String[] args) throws Exception {
  47.         Configuration conf = new Configuration();
  48.         if (args.length < 3) {
  49.             System.out.println("Usage: wordcount <input_path> <output_path> <keyword_list>");
  50.             return;
  51.         }
  52.         //  Add to target
  53.         String[] target_words = args[2].split(",");
  54.         for (String word : target_words) {
  55.             Map.add(word.toLowerCase());
  56.         }
  57.         Job job = new Job(conf, "wordcount");
  58.         job.setOutputKeyClass(Text.class);
  59.         job.setOutputValueClass(IntWritable.class);
  60.         job.setMapperClass(Map.class);
  61.         job.setReducerClass(Reduce.class);
  62.         job.setInputFormatClass(TextInputFormat.class);
  63.         job.setOutputFormatClass(TextOutputFormat.class);
  64.         FileInputFormat.addInputPath(job, new Path(args[0]));
  65.         FileOutputFormat.setOutputPath(job, new Path(args[1]));
  66.         job.waitForCompletion(true);
  67.     }
  68. }
复制代码
5. 第五题的程序是什么?

6.
        hosts:增加局域网主机名和ip对应关系,省得再记住ip;
        hostname:该主机名,克隆虚拟机的时候经常需要这么做;
        fstab:修改挂载点,加新硬盘的时候会需要;
        profile, bash.bashrc: 修改系统范围环境变量时经常用;
        network/interfaces:配置静态IP时需要。

7
7.1
  1. package org.aboutyun;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. public class LineCounter {
  7.     public static void main(String[] args) {
  8.         try {
  9.             BufferedReader reader = new BufferedReader(new FileReader(args[0]));
  10.             char[] buffer = new char[4096];
  11.             int count;
  12.             char last = 0;
  13.             long line_count = 0;
  14.             while((count = reader.read(buffer)) >= 0) {
  15.                 if (count > 0 && line_count == 0) {
  16.                     //  has something in file, so at least 1 line.
  17.                     line_count = 1;
  18.                 }
  19.                 for (int i = 0; i < count ; ++i) {
  20.                     char c = buffer[i];
  21.                     if (c == 0x02) {
  22.                         if (i == 0 && last == 0x01) {
  23.                             //  buffer split the 0x01,0x02
  24.                             ++line_count;
  25.                         } else if (buffer[i-1] == 0x01) {
  26.                             //  normal one
  27.                             ++line_count;
  28.                         }
  29.                     }
  30.                 }
  31.                 //  keep the last one
  32.                 last = buffer[count-1];
  33.             }
  34.             System.out.println(line_count);
  35.         } catch (FileNotFoundException e) {
  36.             e.printStackTrace();
  37.         } catch (IOException e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41. }
复制代码
7.2 可以使用Profiler来对性能进行评估分析,比如Eclipse的TPTP,或者JProfiler。可以观察不同函数调用次数和以及占用时间,从而减少调用次数,以及优化函数内部。




回复

使用道具 举报

半日闲 发表于 2013-11-11 13:52:33
嘿嘿,我没问题,求工作  :D
回复

使用道具 举报

gefieder 发表于 2013-11-11 13:57:30
给大家分享一下
1、hadoop运行的原理?
2、mapreduce的原理?
3、HDFS存储的机制?
4、举一个简单的例子说明mapreduce是怎么来运行的 ?
5、面试的人给你出一些问题,让你用mapreduce来实现?
      比如:现在有10个文件夹,每个文件夹都有1000000个url.现在让你找出top1000000url。
6、hadoop中Combiner的作用?

一、作用
1、combiner最基本是实现本地key的聚合,对map输出的key进行排序,value进行迭代。如下所示:
map: (K1, V1) → list(K2, V2)
combine: (K2, list(V2)) → list(K2, V2)
reduce: (K2, list(V2)) → list(K3, V3)

2、combiner还具有类似本地的reduce功能.
例如hadoop自带的wordcount的例子和找出value的最大值的程序,combiner和reduce完全一致。如下所示:
map: (K1, V1) → list(K2, V2)
combine: (K2, list(V2)) → list(K3, V3) ,减轻reduce的负担!reduce: (K3, list(V3)) → list(K4, V4)

3、如果不用combiner,那么,所有的结果都是reduce完成,效率会相对低下。使用combiner,先完成的map会在本地聚合,提升速度。

举一个hadoop自带的wordcount例子说明。
value就是一个叠加的数字,所以map一结束就可以进行reduce的value叠加,而不必要等到所有的map结束再去进行reduce的value叠加。

回复

使用道具 举报

pig2 发表于 2013-11-11 18:40:36

努力,你就是标准答案
回复

使用道具 举报

kevin 发表于 2013-11-12 21:01:04
真心不懂,但是真心想懂,慢慢学习希望会懂:lol
回复

使用道具 举报

pig2 发表于 2013-11-12 21:03:03
kevin 发表于 2013-11-12 21:01
真心不懂,但是真心想懂,慢慢学习希望会懂

真正懂的不多,如果你懂了,说明你是大牛了
回复

使用道具 举报

kevin 发表于 2013-11-12 21:11:12
pig2 发表于 2013-11-12 21:03
真正懂的不多,如果你懂了,说明你是大牛了

恩恩,昨天才买hadoop的书籍,接下来该好好研究研究了,楼主肯定大牛撒
回复

使用道具 举报

jacky 发表于 2013-11-13 19:02:49
hadoop知识太欠缺了,收藏慢慢消化撒
回复

使用道具 举报

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

本版积分规则

关闭

推荐上一条 /2 下一条