大數據面試題—6

3.14
1、一個Hadoop環境,整合了HBase和Hive,是否有必要給HDFS和Hbase都分別配置壓縮策略?請給出對壓縮策略的建議。
hdfs在存儲的時候不會將數據進行壓縮,如果想進行壓縮,我們可以在向hdfs上傳數據的時候進行壓縮。
1)、  採用壓縮流

2)、  採用序列化文件

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