大数据面试-06-大数据工程师面试题

3.14
1、一个Hadoop环境,整合了HBase和Hive,是否有必要给HDFS和Hbase都分别配置压缩策略?请给出对压缩策略的建议。
hdfs在存储的时候不会将数据进行压缩,如果想进行压缩,我们可以在向hdfs上传数据的时候进行压缩。
1)、  采用压缩流
  1. //压缩文件  
  2. public static void compress(String codecClassName) throws Exception{  
  3.     Class<?> codecClass = Class.forName(codecClassName);  
  4.     Configuration conf = new Configuration();  
  5.     FileSystem fs = FileSystem.get(conf);  
  6.     CompressionCodec codec = (CompressionCodec)ReflectionUtils.newInstance(codecClass, conf);  
  7.     //指定压缩文件路径  
  8.     FSDataOutputStream outputStream = fs.create(new Path(“/user/hadoop/text.gz”));  
  9.     //指定要被压缩的文件路径  
  10.     FSDataInputStream in = fs.open(new Path(“/user/hadoop/aa.txt”));  
  11.     //创建压缩输出流  
  12.     CompressionOutputStream out = codec.createOutputStream(outputStream);   
  13.     IOUtils.copyBytes(in, out, conf);  
  14.     IOUtils.closeStream(in);  
  15.     IOUtils.closeStream(out);  
  16. }  
//压缩文件
public static void compress(String codecClassName) throws Exception{
    Class<?> codecClass = Class.forName(codecClassName);
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(conf);
    CompressionCodec codec = (CompressionCodec)ReflectionUtils.newInstance(codecClass, conf);
    //指定压缩文件路径
    FSDataOutputStream outputStream = fs.create(new Path("/user/hadoop/text.gz"));
    //指定要被压缩的文件路径
    FSDataInputStream in = fs.open(new Path("/user/hadoop/aa.txt"));
    //创建压缩输出流
    CompressionOutputStream out = codec.createOutputStream(outputStream); 
    IOUtils.copyBytes(in, out, conf);
    IOUtils.closeStream(in);
    IOUtils.closeStream(out);
}

2)、  采用序列化文件

  1. public void testSeqWrite() throws Exception {  
  2.     Configuration conf = new Configuration();// 创建配置信息  
  3.     conf.set(”fs.default.name”“hdfs://master:9000”);// hdfs默认路径  
  4.     conf.set(”hadoop.job.ugi”“hadoop,hadoop”);// 用户和组信息  
  5.     String uriin = ”hdfs://master:9000/ceshi2/”;// 文件路径  
  6.     FileSystem fs = FileSystem.get(URI.create(uriin), conf);// 创建filesystem  
  7.     Path path = new Path(“hdfs://master:9000/ceshi3/test.seq”);// 文件名  
  8.     IntWritable k = new IntWritable();// key,相当于int  
  9.     Text v = new Text();// value,相当于String  
  10.     SequenceFile.Writer w = SequenceFile.createWriter(fs, conf, path,k.getClass(), v.getClass());// 创建writer  
  11.     for (int i = 1; i < 100; i++) {// 循环添加  
  12.         k.set(i);  
  13.         v.set(”abcd”);  
  14.         w.append(k, v);  
  15.     }  
  16.     w.close();  
  17.     IOUtils.closeStream(w);// 关闭的时候flush  
  18.     fs.close();  
  19. }  
public void testSeqWrite() throws Exception {
    Configuration conf = new Configuration();// 创建配置信息
    conf.set("fs.default.name", "hdfs://master:9000");// hdfs默认路径
    conf.set("hadoop.job.ugi", "hadoop,hadoop");// 用户和组信息
    String uriin = "hdfs://master:9000/ceshi2/";// 文件路径
    FileSystem fs = FileSystem.get(URI.create(uriin), conf);// 创建filesystem
    Path path = new Path("hdfs://master:9000/ceshi3/test.seq");// 文件名
    IntWritable k = new IntWritable();// key,相当于int
    Text v = new Text();// value,相当于String
    SequenceFile.Writer w = SequenceFile.createWriter(fs, conf, path,k.getClass(), v.getClass());// 创建writer
    for (int i = 1; i < 100; i++) {// 循环添加
        k.set(i);
        v.set("abcd");
        w.append(k, v);
    }
    w.close();
    IOUtils.closeStream(w);// 关闭的时候flush
    fs.close();
}

hbase为列存数据库,本身存在压缩机制,所以无需设计。

3、简述Hbase性能优化的思路

1)、  在库表设计的时候,尽量考虑rowkey和columnfamily的特性
2)、  进行hbase集群的调优:见hbase调优

4、简述Hbase filter的实现原理是什么?结合实际项目经验,写出几个使用filter的场景。

hbase的filter是通过scan设置的,所以是基于scan的查询结果进行过滤。
1)、在进行订单开发的时候,我们使用rowkeyfilter过滤出某个用户的所有订单
2)、在进行云笔记开发时,我们使用rowkey过滤器进行redis数据的恢复。

5、ROWKEY的后缀匹配怎么实现?例如ROWKEY是yyyyMMDD-UserID形式,如果要以UserID为条件查询数据,怎样实现。
使用rowkey过滤器实现
6、简述Hive中的虚拟列作用是什么,使用它的注意事项。
Hive提供了三个虚拟列:
INPUT__FILE__NAME
BLOCK__OFFSET__INSIDE__FILE
ROW__OFFSET__INSIDE__BLOCK
但ROW__OFFSET__INSIDE__BLOCK默认是不可用的,需要设置hive.exec.rowoffset为true才可以。可以用来排查有问题的输入数据。
INPUT__FILE__NAME, mapper任务的输出文件名。
BLOCK__OFFSET__INSIDE__FILE, 当前全局文件的偏移量。对于块压缩文件,就是当前块的文件偏移量,即当前块的第一个字节在文件中的偏移量。
hive> SELECT INPUT__FILE__NAME, BLOCK__OFFSET__INSIDE__FILE, line
> FROM hive_text WHERE line LIKE ‘%hive%’ LIMIT 2;
har://file/user/hive/warehouse/hive_text/folder=docs/
data.har/user/hive/warehouse/hive_text/folder=docs/README.txt  2243
har://file/user/hive/warehouse/hive_text/folder=docs/
data.har/user/hive/warehouse/hive_text/folder=docs/README.txt  3646
7、如果要存储海量的小文件(大小都是几百K~几M),请简述自己的设计方案。
1)、将小文件打成har文件存储
2)、将小文件序列化到hdfs中
8、有两个文本文件,文件中的数据按行存放,请编写MapReduce程序,找到两个文件中彼此不相同的行。
写个mapreduce链  用依赖关系,一共三个mapreduce,第一个处理第一个文件,第二个处理第二个文件,第三个处理前两个的输出结果,
第一个mapreduce将文件去重,第二个mapreduce也将文件去重,第三个做wordcount,wordcount为1的结果就是不同的。

4.   共同朋友

mapred找共同朋友,数据格式如下
usr:friend,friend,friend…
—————
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
第一个字母表示本人,其他是他的朋友,找出共同朋友的人,和共同朋友是谁。
思路:例如A,他的朋友是B\C\D\E\F\,那么BC的共同朋友就是A。所以将BC作为key,将A作为value,在map端输出即可!其他的朋友循环处理。
  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.    
  16. public class FindFriend {     
  17.    
  18.     public static class ChangeMapper extends Mapper<Object, Text, Text,Text>{                        
  19.         @Override  
  20.         public void map(Object key, Text value, Context context) throws IOException, InterruptedException {  
  21.             StringTokenizer itr = new StringTokenizer(value.toString());  
  22.             Text owner = new Text();  
  23.             Set<String> set = new TreeSet<String>();  
  24.             owner.set(itr.nextToken());  
  25.             while (itr.hasMoreTokens()) {  
  26.                 set.add(itr.nextToken());  
  27.             }               
  28.             String[] friends = new String[set.size()];  
  29.             friends = set.toArray(friends);   
  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, Context context) throws IOException,InterruptedException {  
  41.             String  commonfriends =”“;   
  42.             for (Text val : values) {  
  43.                 if(commonfriends == “”){  
  44.                     commonfriends = val.toString();  
  45.                 }else{  
  46.                     commonfriends = commonfriends+”:”+val.toString();  
  47.                 }  
  48.             }  
  49.             context.write(key, new Text(commonfriends));   
  50.         }                             
  51.     }  
  52.    
  53.     public static void main(String[] args) throws IOException,InterruptedException, ClassNotFoundException{               
  54.         Configuration conf = new Configuration();  
  55.         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  56.         if (otherArgs.length < 2) {  
  57.             System.err.println(”args error”);  
  58.             System.exit(2);  
  59.         }  
  60.         Job job = new Job(conf, “word count”);  
  61.         job.setJarByClass(FindFriend.class);  
  62.         job.setMapperClass(ChangeMapper.class);  
  63.         job.setCombinerClass(FindReducer.class);  
  64.         job.setReducerClass(FindReducer.class);  
  65.         job.setOutputKeyClass(Text.class);  
  66.         job.setOutputValueClass(Text.class);  
  67.         for (int i = 0; i < otherArgs.length - 1; ++i) {  
  68.             FileInputFormat.addInputPath(job, new Path(otherArgs[i]));  
  69.         }  
  70.         FileOutputFormat.setOutputPath(job,new Path(otherArgs[otherArgs.length - 1]));  
  71.         System.exit(job.waitForCompletion(true) ? 0 : 1);                  
  72.     }  
  73. }  
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);                
    }
}

结果:

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

5.   基站逗留时间

需求:

期望:

思路:

将数据导入hive表中,查询时,用电话号码和时间排序即可!

6.   脚本替换


脚本:随意命名为 aaa.sh

#!/bin/bash

ls $1 | while read line

do

sed -i ‘s,$HADOOP_HOME$,\/home\/aa,g’ 1 line

echo 1line

done

脚本执行命令:替换/home/hadoop/test/下的所有文件
./aaa.sh /home/hadoop/test/

7.   一键执行




脚本:

vi runRemoteCmd.sh

#!/bin/bash

$1

ssh -q hadoop@slave1 “$1”

ssh -q hadoop@slave2 “$1”

执行命令




./runRemoteCmd.sh “ls -l”



8.   大数据面试汇总

1.讲解一下MapReduce 的一些基本流程
任务提交流程,任务运行流程
2.你们数据库怎么导入hive 的,有没有出现问题
使用sqoop导入,我们公司的数据库中设计了text字段,导致导入的时候出现了缓存不够的情况(见云笔记),开始解决起来感觉很棘手,后来查看了sqoop的文档,加上了limit属性,解决了。
3.公司技术选型可能利用storm 进行实时计算,讲解一下storm
从storm的应用,代码书写,运行机制讲
4.问你java 集合类的数据结构,比如hashmap
看java面试宝典
5.问你知不知道concurrent 包下的东西,例如concurrenthashmap
看java面试宝典
6.公司最近主要在自然语言学习去开发,有没有接触过
没有用过






                </div>
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值