ToolRunner机制

本文深入探讨了Mahout框架中ToolRunner接口及其在Tool类中的实现方式,通过具体代码示例展示了如何利用ToolRunner统一入口调用,并详细解析了MinHashDriver的实现,包括参数覆盖、核心方法调用及MapReduce任务的启动流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义框架接口 
由具体实现类实现 
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. }  


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值