hadoop 入门 启动任务

来源:互联网 发布:子域名挖掘机 编辑:程序博客网 时间:2024/05/17 06:09
定义框架接口 
由具体实现类实现 
Java代码  收藏代码
  1. public interface Tool extends Configurable {  
  2.   int run(String [] args) throws Exception;  
  3. }  



ToolRunner 
同一的入口调用 
按配置解析参数,调用接口方法 
Java代码  收藏代码
  1. public static int run(Configuration conf, Tool tool, String[] args)   
  2.   throws Exception{  
  3.   if(conf == null) {  
  4.     conf = new Configuration();  
  5.   }  
  6.   GenericOptionsParser parser = new GenericOptionsParser(conf, args);  
  7.   //set the configuration back, so that Tool can configure itself  
  8.   tool.setConf(conf);  
  9.     
  10.   //get the args w/o generic hadoop args  
  11.   String[] toolArgs = parser.getRemainingArgs();  
  12.   return tool.run(toolArgs);  
  13. }  


Mahout 中具体调用示例 
Java代码  收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.   ToolRunner.run(new Configuration(), new MinHashDriver(), args);  
  3. }  


覆盖方法,提取参数,调用核心方法 
Java代码  收藏代码
  1. @Override  
  2. public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
  3.   addInputOption();  
  4.   addOutputOption();  
  5.   //...........  
  6.   runJob(input,  
  7.          output,  
  8.          minClusterSize,  
  9.          minVectorSize,  
  10.          hashType,  
  11.          numHashFunctions,  
  12.          keyGroups,  
  13.          numReduceTasks,  
  14.          debugOutput);  
  15.   return 0;  
  16. }  


核心方法,配置job,开始map reduce任务 
Java代码  收藏代码
  1. private void runJob(Path input,   
  2.                     Path output,  
  3.                     int minClusterSize,  
  4.                     int minVectorSize,   
  5.                     String hashType,   
  6.                     int numHashFunctions,   
  7.                     int keyGroups,  
  8.                     int numReduceTasks,   
  9.                     boolean debugOutput) throws IOException, ClassNotFoundException, InterruptedException {  
  10.   Configuration conf = getConf();  
  11.   
  12.   //配置参数设置........................      
  13.   Job job = new Job(conf, "MinHash Clustering");  
  14.   job.setJarByClass(MinHashDriver.class);  
  15.   
  16.   //Job参数设置.........................  
  17.   job.waitForCompletion(true);  
  18. }  


 



0 0
原创粉丝点击