Hadoop中的輔助類ToolRunner和Configured的用法詳解


在開始學習hadoop時,最痛苦的一件事就是難以理解所寫程序的執行過程,讓我們先來看這個實例,這個測試類ToolRunnerTest繼承Configured的基礎上實現了Tool接口,下面對其用到的基類源碼進行分析,就可以理解其執行過程是如此簡單。。。。。。

 1 package xml;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.conf.Configured;
 5 import org.apache.hadoop.util.Tool;
 6 import org.apache.hadoop.util.ToolRunner;
 7 
 8 public class ToolRunnerTest extends Configured implements Tool {
 9 
10     @Override
11     public int run(String[] arg0) throws Exception {
12         //調用基類Configured的getConf獲取環境變量實例
13         Configuration conf=getConf();
14         //獲取屬性值
15         System.out.println("flower is " + conf.get("flower"));
16         System.out.println("color id "+ conf.get("color"));
17         System.out.println("blossom ? "+conf.get("blossom"));
18         System.out.println("this is the host default name ="+conf.get("fs.default.name"));    
19         return 0;
20     }
21 
22     /**
23      * @param args
24      * @throws Exception 
25      */
26     public static void main(String[] args) throws Exception {
27         // TODO Auto-generated method stub
28         //獲取當前環境變量
29         Configuration conf=new Configuration();
30         //使用ToolRunner的run方法對自定義的類型進行處理
31         ToolRunner.run(conf, new ToolRunnerTest(), args);
32         
33     }
34 
35 }

基類Configured實現了Configurable接口,而Configurable接口源碼如下

1 Public interface Configurable{
2     Void setConf(Configuration conf);
3     Configuration getConf();
4 }

Configured則必須實現Configurable類的兩個方法,源碼如下

 1 Public class Configured implements  Configurable{
 2 Private Configuration conf;
 3 Public Configured(Configuration conf){setConf(conf);}//構造方法
 4 Public void setConf(Configuration conf)
 5 {
 6 This.conf=conf;
 7 }
 8 Public getConf()
 9 {
10 Return conf;
11 }
12 }

Tool的源碼如下所示:

public interface Tool extends Configurable {

int run(String [] args) throws Exception;

}

就這么一點點

ToolRunner類的源碼如下

 1 public class ToolRunner {
 2 public static int run(Configuration conf, Tool tool, String[] args) 
 3     throws Exception{
 4     if(conf == null) {
 5       conf = new Configuration();
 6     }
 7     GenericOptionsParser parser = new GenericOptionsParser(conf, args);
 8     //set the configuration back, so that Tool can configure itself
 9     tool.setConf(conf)
10     String[] toolArgs = parser.getRemainingArgs();
11     return tool.run(toolArgs);
12   }
13    public static int run(Tool tool, String[] args) 
14     throws Exception{
15     return run(tool.getConf(), tool, args);
16   }
17   
18   public static void printGenericCommandUsage(PrintStream out) {
19     GenericOptionsParser.printGenericCommandUsage(out);
20   }
21   
22 }

解析:當程序執行ToolRunner.run(conf, new ToolRunnerTest(), args);時,會轉到ToolRunner類的run方法部分,因為Configuration已經實例,所以直至執行到tool.run(toolArgs);又因為Tool是一個只含有一個run方法框架的接口,所以將執行實現這個接口的類ToolRunnerTestrun方法。完成其輸出。其實在看完這幾個類的源碼后,其執行過程是很簡單的

該實例的運行結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM