1. 程式人生 > >大資料工程師面試題—2

大資料工程師面試題—2

2.7.  用mapreduce來實現下面需求?
現在有10個資料夾,每個資料夾都有1000000個url.現在讓你找出top1000000url。
方法一:
運用2個job,第一個job直接用filesystem讀取10個資料夾作為map輸入,url做key,reduce計算url的sum,
下一個job map用url作key,運用sum作二次排序,reduce中取top10000000
1:首先進行wordcount計算
2:進行二次排序
如何啟動兩個job程式碼如下:
public class TopUrl {
     public static void main(String[] args) throws Exception {
     depending();
 }
 
 public static void depending() throws Exception{
      Configuration conf = new Configuration();
      //排序
      Job job2 = initJob2(conf, SecondSortMapper.class,SecondSortReduce.class);
      //讀取url
      Job job1 = initJob(conf, FirstMapper.class, FirstReduce.class);
      
      JobControl jobControl = new JobControl("groupName");
      List<ControlledJob>  dependingJobs = new ArrayList<ControlledJob>();
      //進行排序
      ControlledJob controlledJob1 = new ControlledJob(conf);
      controlledJob1.setJob(job1);
 //   dependingJobs.add(controlledJob1);
   
      //排序
      ControlledJob controlledJob2 = new ControlledJob(conf);
      controlledJob2.setJob(job2);
      controlledJob2.addDependingJob(controlledJob1);
      jobControl.addJob(controlledJob2);
      jobControl.addJob(controlledJob1);
  
   
    Thread jcThread = new Thread(jobControl);
    jcThread.start();
    while(true){
    if(jobControl.allFinished()){
         System.out.println(jobControl.getSuccessfulJobList());
         jobControl.stop();
         break;
    }
    if(jobControl.getFailedJobList().size() > 0){
         System.out.println(jobControl.getFailedJobList());
         jobControl.stop();
         break;
    }
   }
  
   FileSystem fs = FileSystem.get(conf);
   boolean ret = fs.deleteOnExit(new Path("hdfs://master:9000/user/hadoop/20130601/output"));
   System.out.println(ret);
 }
 
 
 public static Job  initJob2(Configuration conf,Class o1,Class o2) throws Exception{
      Job job = new Job(conf, "Join2");
      job.setJarByClass(TopUrl.class);
      job.setMapperClass(o1);
      job.setMapOutputKeyClass(Text.class);//map輸出key
      job.setOutputValueClass(IntWritable.class);
      job.setReducerClass(o2);
      job.setOutputKeyClass(Text.class); //reduce輸出key
      job.setOutputValueClass(IntWritable.class);
     
//      job.setPartitionerClass(cls);
      job.setSortComparatorClass(TextIntComparator.class);
//      job.setGroupingComparatorClass(cls);
     
      FileInputFormat.addInputPath(job, new Path
      ("hdfs://master:9000/user/hadoop/20130601/output"));
      FileOutputFormat.setOutputPath(job, new Path
      ("hdfs://master:9000/user/hadoop/20130601/output2"));
//      System.exit(job.waitForCompletion(true) ? 0 : 1);
      return job;
 }
 
 
 public static Job initJob(Configuration conf,Class o1,Class o2) throws Exception{
      Job job = new Job(conf, "Join1");
      job.setJarByClass(TopUrl.class);
      job.setMapperClass(o1);
      job.setMapOutputKeyClass(Text.class);//map輸出key
      job.setOutputValueClass(IntWritable.class);
      job.setReducerClass(o2);
      job.setOutputKeyClass(Text.class); //reduce輸出key
      job.setOutputValueClass(IntWritable.class);
      FileInputFormat.addInputPath(job, new Path
      ("hdfs://master:9000/user/hadoop/20130601/ippaixv.txt"));
      FileOutputFormat.setOutputPath(job, new Path
      ("hdfs://master:9000/user/hadoop/20130601/output"));
//    System.exit(job.waitForCompletion(true) ? 0 : 1);
      return job;
 }
}


方法二:
建hive表A,掛分割槽channel,每個資料夾是一個分割槽.
select x.url,x.c from(select url,count(1) as c from A where channel ='' group by url) x order by x.c desc limit 1000000;

還可以用treeMap, 到1000000了每來一個都加進去, 刪掉最小的

2.8.  hadoop中Combiner的作用?
combiner是reduce的實現,在map端執行計算任務,減少map端的輸出資料。
作用就是優化。
但是combiner的使用場景是mapreduce的map和reduce輸入輸出一樣。
 

2.9.  簡述hadoop安裝
1)建立hadoop使用者
2)改IP,修改HOSTS檔案域名
3)安裝SSH,配置無金鑰通訊
4)安裝JAVA,配置JAVA的環境變數
5)解壓hadoop
6)配置conf下的core-site.xml,hdfs-site.xml,mapred-site.xml,yarn-site.xml
7)配置hadoop的環境變數
8)hadoop namenode -format
9)start-all.sh

2.10. 請列出hadoop程序名
Namenode:管理叢集,並記錄datanode檔案資訊。
Secondarynamenode:可以做冷備,對一定範圍內資料做快照性備份。
Datanode:儲存資料。
Resourcemanager:管理任務,並將任務分配給MRAppMaster。
NodeManager:任務執行方。

2.11. 解決下面的錯誤
1、許可權問題,可能曾經用root啟動過叢集。(例如hadoop搭建的叢集,是tmp/hadoop-hadoop/.....)
2、可能是資料夾不存在
3、解決: 刪掉tmp下的那個檔案,或改成當前使用者

2.12. 寫出下面的命令
1)殺死一個job
hadoop job -list 拿到job-id,
hadoop job -kill job-id
2)刪除hdfs上的/tmp/aaa目錄
hadoop fs -rmr /tmp/aaa
3)加入一個新的儲存節點和刪除一個計算節點需要重新整理叢集狀態命令
加薪節點時:
hadoop-daemon.sh start datanode
yarn-daemon.sh start nodemanager
刪除時:
hadoop mradmin -refreshnodes
hadoop dfsadmin -refreshnodes