手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop指南:分析一个MapReduce实例

[日期:2011-12-07] 来源:Linux社区  作者:erkiuyao [字体:大 中 小]
学了半个月Hadoop了,都还没见过一行关于Hadoop的代码,这是不行的。我们不仅要学习理论/方法,还得学习怎么用,我们今天揪个程序出来,瞧瞧一个Hadoop程序到底是有哪些部分组成的。


先来看一下这个程序:


package  com.apress.hadoopbook.examples.ch2;


import java.io.IOException;


import  org.apache.hadoop.io.Text;

import  org.apache.hadoop.mapred.FileInputFormat;

import  org.apache.hadoop.mapred.FileOutputFormat;

import  org.apache.hadoop.mapred.JobClient;

import  org.apache.hadoop.mapred.JobConf;

import  org.apache.hadoop.mapred.KeyValueTextInputFormat;

import  org.apache.hadoop.mapred.RunningJob;

import  org.apache.hadoop.mapred.lib.IdentityMapper;

import  org.apache.hadoop.mapred.lib.IdentityReducer;

import  org.apache.log4j.Logger;


 

public  class  MapReduceIntro {

protected  static Loggerlogger= Logger.getLogger(MapReduceIntro.class);


 

public static  voidmain(final  String[] args) {

try  {


 

final  JobConfconf = new  JobConf(MapReduceIntro.class);


//定义Hadoop运行作业时的临时文件目录

conf.set("hadoop.tmp.dir","/tmp");


 

MapReduceIntroConfig.exampleHouseKeeping(conf, MapReduceIntroConfig.getInputDirectory(), MapReduceIntroConfig.getOutputDirectory());


 

conf.setInputFormat(KeyValueTextInputFormat.class);

FileInputFormat.setInputPaths(conf, MapReduceIntroConfig.getInputDirectory());


 

conf.setMapperClass(IdentityMapper.class);


 

FileOutputFormat.setOutputPath(conf, MapReduceIntroConfig.getOutputDirectory());

conf.setOutputKeyClass(Text.class);

conf.setOutputValueClass(Text.class);

conf.setNumReduceTasks(1);

 

 

conf.setReducerClass(IdentityReducer.class);


logger.info("Launching the job.");

 

final  RunningJob job = JobClient.runJob(conf);

logger.info("The job has completed.");


if  (!job.isSuccessful()) {

logger.error("The job failed.");

System.exit(1);

}

logger.info("The job completed successfully.");

System.exit(0);

} catch  (final  IOException e) {

logger.error("The job has failed due to an IO Exception", e);

e.printStackTrace();

}


}

}



程序很短,而且注释很详细,不过我们还是来看看它有哪些部分组成。


1.日志跟踪。通过日志我们才能跟踪 MapReduceIntro 类,将作业运行期间所发生的动作记录下来,便于后面的分析:


protected static Loggerlogger= Logger.getLogger(MapReduceIntro.class);

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款