- public class MyJob extends Configured implements Tool
- {
- public static class MapClass extends MapReduceBase implements Mapper<Text, Text, Text, Text>
- {//
- public void map(Text key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
- {
- output.collect(value, key);
- }
- }
- public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text>
- {
- @Override
- public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
- {
- String csv = "";
- while (values.hasNext())
- {
- csv += csv.length() > 0 ? "," : "";
- csv += values.next().toString();
- }
- output.collect(key, new Text(csv));
- }
- }
主要看run方法:
上面代码中的Jobconf无可厚非,只有在mapred包中有定义,这个没问题。
但是FileInputFormat和FileOutputFormat在mapred和mapreduce中都有定义,刚开始脑海里对这些都没有概念,就引用了mapreduce中的FileInputFormat和FIleOutputFormat。
这样操作就带来了后面的问题
FileInputFormat.setInputPaths(job, in);
FileOutputFormat.setOutputPath(job, out);
这两条语句不能通过编译,为什么呢,因为FileInputFormat.setInputPaths和FileOutputFormat.setOutputPath的第一个参数都是Job,而不是JobConf,
后来,无意中,看到mapred包中也有这两个类的定义,于是火箭速度修改为mapred下的包,OK,顺利通过编译!
下面还有 job.setOutputFormat(TextOutputFormat.class);语句编译不同通过,提示参数需要扩展。。。的参数;于是小菜也去mapred下面查找是否存在此类,正如期望,也存在此类,当即立段,修改为此包下的类,顺利编译通过,此时,颇有成就感!
可是现在小菜发现,mapred包下和mapreduce包下同时都存在又相应的类,不知道是为什么,那么下面就有目标的请教搜索引擎啦,呵呵,比刚才有很大进步。
结果令小菜很失望,就找到了一个符合理想的帖子。但是通过这个帖子,小菜知道了,mapred代表的是hadoop旧API,而mapreduce代表的是hadoop新的API。
OK,小菜在google输入框中输入“hadoop新旧API的区别”,结果很多。看了之后,又结合权威指南归结如下:
1. 首先第一条,也是小菜今天碰到这些问题的原因,新旧API不兼容。所以,以前用旧API写的hadoop程序,如果旧API不可用之后需要重写,也就是上面我的程序需要重写,如果旧API不能用的话,如果真不能用,这个有点儿小遗憾!
2. 新的API倾向于使用抽象类,而不是接口,使用抽象类更容易扩展。例如,我们可以向一个抽象类中添加一个方法(用默认的实现)而不用修改类之前的实现方法。因此,在新的API中,Mapper和Reducer是抽象类。
3. 新的API广泛使用context object(上下文对象),并允许用户代码与MapReduce系统进行通信。例如,在新的API中,MapContext基本上充当着JobConf的OutputCollector和Reporter的角色。
4. 新的API同时支持"推"和"拉"式的迭代。在这两个新老API中,键/值记录对被推mapper中,但除此之外,新的API允许把记录从map()方法中拉出,这也适用于reducer。分批处理记录是应用"拉"式的一个例子。
5. 新的API统一了配置。旧的API有一个特殊的JobConf对象用于作业配置,这是一个对于Hadoop通常的Configuration对象的扩展。在新的API中,这种区别没有了,所以作业配置通过Configuration来完成。作业控制的执行由Job类来负责,而不是JobClient,并且JobConf和JobClient在新的API中已经荡然无存。这就是上面提到的,为什么只有在mapred中才有Jobconf的原因。
6. 输出文件的命名也略有不同,map的输出命名为part-m-nnnnn,而reduce的输出命名为part-r-nnnnn,这里nnnnn指的是从0开始的部分编号。
这样了解了二者的区别就可以通过程序的引用包来判别新旧API编写的程序了。小菜建议最好用新的API编写hadoop程序,以防旧的API被抛弃!!!
小菜水平有限,如果哪位大牛看到文中的不足和错误,请指正,小菜会尽快更改文中错误,好让其他入门者不走我的弯路!
转自http://www.aboutyun.com/thread-8251-1-1.html
问题导读:
一直想写hadoop新旧api之间的关系,这对于爱好编程的程序猿来讲,是必备的。
1.hadoop中mapred与mapreduce包,那个是被弃用的?
2.hadoop旧api如何初始化job?
3.hadoop新api使用那个函数来初始化job对象?
程序说明:
下面的mapreduce程序的功能只是计算文件booklist.log的行数,最后输出结果。
分别调用旧包和新包的方法编写了两分带有main函数的java代码。
a,新建了mapreduce工程后,先把hadoop的配置目录下的xml都拷贝到src目录下。
b,在工程src同级目录旁建立conf目录,并放一个log4j.properties文件。
c, src目录下建立bookCount目录,然后再添加后面的子java文件。
d, 右击"run as application"或选择hadoop插件菜单"run on hadoop"来触发执行MapReduce程序即可运行。
生成要分析的输入文件
vi booklist.log
添加以下内容即可:
bookname
bookname
bookname
bookname
bookname
bookname
bookname
bookname
bookname
bookname
bookname
bookname
保存退出。
执行的前请通过hdfs的copyFromLocal命令拷贝到hdfs的/user/hduser用户目录下。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
老API使用mapred包的代码
文件BookCount.java:
- package bookCount;
- import java.io.IOException;
- import java.util.Iterator;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- 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.MapReduceBase;
- import org.apache.hadoop.mapred.Mapper;
- import org.apache.hadoop.mapred.OutputCollector;
- import org.apache.hadoop.mapred.Reducer;
- import org.apache.hadoop.mapred.Reporter;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- public class BookCount {
- public static Logger logger = Logger.getLogger(BookCount.class);
- public static void main(String[] args) throws IOException {
- PropertyConfigurator.configure("conf/log4j.properties");
- logger = Logger.getLogger(BookCount.class);
- logger.info("AnaSpeedMr starting");
- System.setProperty("HADOOP_USER_NAME", "hduser");
- JobConf conf = new JobConf(BookCount.class);
- conf.setJobName("bookCount_sample_job");
- FileInputFormat.setInputPaths(conf, new Path("booklist.log"));
- FileOutputFormat.setOutputPath(conf, new Path("booklistResultDir"));
- conf.setMapperClass(BookCountMapper.class);
- conf.setReducerClass(BookCountReducer.class);
- conf.setOutputKeyClass(Text.class);
- conf.setOutputValueClass(IntWritable.class);
- JobClient.runJob(conf);
- }
- static class BookCountMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
- @Override
- public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
- output.collect(new Text("booknum"), new IntWritable(1));
- logger.info("foxson_mapper_ok");
- System.out.println("foxsonMapper");
- }
- }
- static class BookCountReducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, LongWritable> {
- @Override
- public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, LongWritable> output, Reporter reporter) throws IOException {
- long sumBookNum = 0;
- while (values.hasNext()) {
- sumBookNum =sumBookNum+1;
- values.next();
- }
- logger.info("foxson_BookCountReducer_ok");
- output.collect(key, new LongWritable(sumBookNum));
- System.out.println("foxsonReduce");
- }
- }
- }
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
新API使用mapreduce包的例子
文件BookCountNew.java:
- package bookCount;
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- public class BookCountNew extends Configured implements Tool {
- public static final Logger logger = Logger.getLogger(BookCountNew.class);
- public static void main(String[] args) throws Exception {
- PropertyConfigurator.configure("conf/log4j.properties");
- logger.info("BookCountNew starting");
- System.setProperty("HADOOP_USER_NAME", "hduser");
- Configuration conf = new Configuration();
- int res = ToolRunner.run(conf, new BookCountNew(), args);
- logger.info("BookCountNew end");
- System.exit(res);
- }
- @Override
- public int run(String[] arg0) throws Exception {
- try {
- Configuration conf = getConf();
- Job job = Job.getInstance(conf, "bookCount_new_sample_job");
- job.setJarByClass(getClass());
- job.setMapperClass(BookCountMapper.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(IntWritable.class);
- job.setReducerClass(BookCountReducer.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- TextInputFormat.addInputPath(job, new Path("booklist.log"));
- TextOutputFormat.setOutputPath(job, new Path("booklistResultDir"));
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- } catch (Exception e) {
- logger.error(e.getMessage());
- e.printStackTrace();
- }
- return 0;
- }
- static class BookCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
- @Override
- public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
- context.write(new Text("booknum"), new IntWritable(1));
- logger.info("foxson_mapper_ok");
- System.out.println("foxsonMapper");
- }
- }
- static class BookCountReducer extends Reducer<Text, IntWritable, Text, LongWritable> {
- @Override
- public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
- long sumBookNum = 0;
- for (IntWritable value : values) {
- sumBookNum = sumBookNum + 1;
- }
- logger.info("foxson_BookCountReducer_ok");
- context.write(key, new LongWritable(sumBookNum));
- System.out.println("foxsonReduce");
- }
- }
- }
上面例子大家可以用来学习,这里在交给大家该如何学习查看api,
咱们还是以上面为例:
1.查看hadoop2.4在线api
首先打开下面链接
http://hadoop.apache.org/docs/r2.4.0/api/index.html
打开之后,我们说一下查看顺序:
如下图所示:
1-->2-->3的顺序
也就是说:如果想了解这个包都包含哪些类接口等需要查看2区域,想看类和接口的详细信息,比如包含哪些函数,函数有什么功能,查看3区域。
2.旧api的各个函数及实例
我们这里以jobconf为例:
从上图查看顺序,我们得到下面代码:
// Create a new JobConf
JobConf job = new JobConf(new Configuration(), MyJob.class);
// Specify various job-specific parameters
job.setJobName("myjob");
FileInputFormat.setInputPaths(job, new Path("in"));
FileOutputFormat.setOutputPath(job, new Path("out"));
job.setMapperClass(MyJob.MyMapper.class);
job.setCombinerClass(MyJob.MyReducer.class);
job.setReducerClass(MyJob.MyReducer.class);
job.setInputFormat(SequenceFileInputFormat.class);
job.setOutputFormat(SequenceFileOutputFormat.class);
3.新api的各个函数及实例
给了这么个例子:
- // Create a new Job
- Job job = new Job(new Configuration());
- job.setJarByClass(MyJob.class);
-
- // Specify various job-specific parameters
- job.setJobName("myjob");
-
- job.setInputPath(new Path("in"));
- job.setOutputPath(new Path("out"));
-
- job.setMapperClass(MyJob.MyMapper.class);
- job.setReducerClass(MyJob.MyReducer.class);
- // Submit the job, then poll for progress until the job is complete
- job.waitForCompletion(true);
带个横杠,含义就是被弃用了
下面我们继续寻找:
getInstance() 有很多重载函数,这里不需要解释什么是重载吧,面向对象估计大家学习过,重载就是函数名相同,参数个数和类型可能不同。
好吧,我们试一下这个,如上面新api就是采用这种实例化job的。同时这种实例化的方式采用的是工厂模式,工厂模式,大家也可以找找这方面的资料。
寻找api完毕,更多的函数大家可以在找找。