Hadoop中mapred包和mapreduce包的区别与联系

[java]  view plain  copy
  1. public  class MyJob extends Configured implements Tool  
  2. {  
  3.       
  4.     public static class MapClass extends MapReduceBase implements Mapper<Text, Text, Text, Text>  
  5.     {//  
  6.         public void map(Text key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException  
  7.         {  
  8.             output.collect(value, key);  
  9.         }  
  10.           
  11.     }  
  12.   
  13.     public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text>  
  14.     {  
  15.   
  16.         @Override  
  17.         public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter)     throws IOException  
  18.         {  
  19.             String csv = "";  
  20.             while (values.hasNext())  
  21.             {  
  22.                 csv += csv.length() > 0 ? "," : "";  
  23.                 csv += values.next().toString();                  
  24.             }  
  25.             output.collect(key, new Text(csv));  
  26.         }  
  27.           
  28.     }  





主要看run方法:
上面代码中的Jobconf无可厚非,只有在mapred包中有定义,这个没问题。

但是FileInputFormat和FileOutputFormat在mapred和mapreduce中都有定义,刚开始脑海里对这些都没有概念,就引用了mapreduce中的FileInputFormat和FIleOutputFormat。

这样操作就带来了后面的问题

FileInputFormat.setInputPaths(job, in);
FileOutputFormat.setOutputPath(job, out);
这两条语句不能通过编译,为什么呢,因为FileInputFormat.setInputPathsFileOutputFormat.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:

[java]  view plain  copy
  1. package bookCount;  
  2.   
  3.   
  4.   
  5. import java.io.IOException;  
  6.   
  7. import java.util.Iterator;  
  8.   
  9.   
  10.   
  11. import org.apache.hadoop.fs.Path;  
  12.   
  13. import org.apache.hadoop.io.IntWritable;  
  14.   
  15. import org.apache.hadoop.io.LongWritable;  
  16.   
  17. import org.apache.hadoop.io.Text;  
  18.   
  19. import org.apache.hadoop.mapred.FileInputFormat;  
  20.   
  21. import org.apache.hadoop.mapred.FileOutputFormat;  
  22.   
  23. import org.apache.hadoop.mapred.JobClient;  
  24.   
  25. import org.apache.hadoop.mapred.JobConf;  
  26.   
  27. import org.apache.hadoop.mapred.MapReduceBase;  
  28.   
  29. import org.apache.hadoop.mapred.Mapper;  
  30.   
  31. import org.apache.hadoop.mapred.OutputCollector;  
  32.   
  33. import org.apache.hadoop.mapred.Reducer;  
  34.   
  35. import org.apache.hadoop.mapred.Reporter;  
  36.   
  37. import org.apache.log4j.Logger;  
  38.   
  39. import org.apache.log4j.PropertyConfigurator;  
  40.   
  41.   
  42.   
  43.   
  44.   
  45. public class BookCount {  
  46.   
  47.        public static Logger logger = Logger.getLogger(BookCount.class);  
  48.   
  49.         
  50.   
  51.        public static void main(String[] args) throws IOException {  
  52.   
  53.               PropertyConfigurator.configure("conf/log4j.properties");  
  54.   
  55.               logger = Logger.getLogger(BookCount.class);  
  56.   
  57.               logger.info("AnaSpeedMr starting");  
  58.   
  59.               System.setProperty("HADOOP_USER_NAME""hduser");  
  60.   
  61.               JobConf conf = new JobConf(BookCount.class);  
  62.   
  63.               conf.setJobName("bookCount_sample_job");  
  64.   
  65.               FileInputFormat.setInputPaths(conf, new Path("booklist.log"));  
  66.   
  67.               FileOutputFormat.setOutputPath(conf, new Path("booklistResultDir"));  
  68.   
  69.               conf.setMapperClass(BookCountMapper.class);  
  70.   
  71.               conf.setReducerClass(BookCountReducer.class);  
  72.   
  73.               conf.setOutputKeyClass(Text.class);  
  74.   
  75.               conf.setOutputValueClass(IntWritable.class);  
  76.   
  77.               JobClient.runJob(conf);  
  78.   
  79.        }  
  80.   
  81.         
  82.   
  83.         
  84.   
  85.        static class BookCountMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {  
  86.   
  87.                 
  88.   
  89.               @Override  
  90.   
  91.               public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {  
  92.   
  93.                      output.collect(new Text("booknum"), new IntWritable(1));  
  94.   
  95.                      logger.info("foxson_mapper_ok");  
  96.   
  97.                      System.out.println("foxsonMapper");  
  98.   
  99.               }  
  100.   
  101.        }  
  102.   
  103.   
  104.   
  105.        static class BookCountReducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, LongWritable> {  
  106.   
  107.               @Override  
  108.   
  109.               public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, LongWritable> output, Reporter reporter) throws IOException {  
  110.   
  111.                      long sumBookNum  = 0;  
  112.   
  113.                      while (values.hasNext()) {  
  114.   
  115.                             sumBookNum =sumBookNum+1;  
  116.   
  117.                             values.next();  
  118.   
  119.                      }  
  120.   
  121.                      logger.info("foxson_BookCountReducer_ok");  
  122.   
  123.                      output.collect(key, new LongWritable(sumBookNum));  
  124.   
  125.                      System.out.println("foxsonReduce");  
  126.   
  127.               }  
  128.   
  129.        }  
  130.   
  131.   
  132.   
  133. }  

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

新API使用mapreduce包的例子

文件BookCountNew.java:

[java]  view plain  copy
  1. package bookCount;  
  2.   
  3.   
  4. import java.io.IOException;  
  5.   
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8.   
  9. import org.apache.hadoop.conf.Configured;  
  10.   
  11. import org.apache.hadoop.fs.Path;  
  12.   
  13. import org.apache.hadoop.io.IntWritable;  
  14.   
  15. import org.apache.hadoop.io.LongWritable;  
  16.   
  17. import org.apache.hadoop.io.Text;  
  18.   
  19. import org.apache.hadoop.mapreduce.Job;  
  20.   
  21. import org.apache.hadoop.mapreduce.Mapper;  
  22.   
  23. import org.apache.hadoop.mapreduce.Reducer;  
  24.   
  25. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  26.   
  27. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  28.   
  29. import org.apache.hadoop.util.Tool;  
  30.   
  31. import org.apache.hadoop.util.ToolRunner;  
  32.   
  33. import org.apache.log4j.Logger;  
  34.   
  35. import org.apache.log4j.PropertyConfigurator;  
  36.   
  37.   
  38.   
  39. public class BookCountNew extends Configured implements Tool {  
  40.   
  41.        public static final Logger logger = Logger.getLogger(BookCountNew.class);  
  42.   
  43.   
  44.   
  45.        public static void main(String[] args) throws Exception {  
  46.   
  47.               PropertyConfigurator.configure("conf/log4j.properties");  
  48.   
  49.               logger.info("BookCountNew starting");  
  50.   
  51.               System.setProperty("HADOOP_USER_NAME""hduser");  
  52.   
  53.               Configuration conf = new Configuration();  
  54.   
  55.               int res = ToolRunner.run(conf, new BookCountNew(), args);  
  56.   
  57.               logger.info("BookCountNew end");  
  58.   
  59.               System.exit(res);  
  60.   
  61.        }  
  62.   
  63.   
  64.   
  65.        @Override  
  66.   
  67.        public int run(String[] arg0) throws Exception {  
  68.   
  69.               try {  
  70.   
  71.                      Configuration conf = getConf();  
  72.   
  73.                      Job job = Job.getInstance(conf, "bookCount_new_sample_job");  
  74.   
  75.                      job.setJarByClass(getClass());  
  76.   
  77.                      job.setMapperClass(BookCountMapper.class);  
  78.   
  79.                      job.setMapOutputKeyClass(Text.class);  
  80.   
  81.                      job.setMapOutputValueClass(IntWritable.class);  
  82.   
  83.                      job.setReducerClass(BookCountReducer.class);  
  84.   
  85.                      job.setInputFormatClass(TextInputFormat.class);  
  86.   
  87.                      job.setOutputFormatClass(TextOutputFormat.class);  
  88.   
  89.                      TextInputFormat.addInputPath(job, new Path("booklist.log"));  
  90.   
  91.                      TextOutputFormat.setOutputPath(job, new Path("booklistResultDir"));  
  92.   
  93.                      job.setOutputKeyClass(Text.class);  
  94.   
  95.                      job.setOutputValueClass(IntWritable.class);  
  96.   
  97.                      System.exit(job.waitForCompletion(true) ? 0 : 1);  
  98.   
  99.               } catch (Exception e) {  
  100.   
  101.                      logger.error(e.getMessage());  
  102.   
  103.                      e.printStackTrace();  
  104.   
  105.               }  
  106.   
  107.               return 0;  
  108.   
  109.        }  
  110.   
  111.   
  112.   
  113.        static class BookCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {  
  114.   
  115.               @Override  
  116.   
  117.               public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {  
  118.   
  119.                      context.write(new Text("booknum"), new IntWritable(1));  
  120.   
  121.                      logger.info("foxson_mapper_ok");  
  122.   
  123.                      System.out.println("foxsonMapper");  
  124.   
  125.               }  
  126.   
  127.        }  
  128.   
  129.   
  130.   
  131.        static class BookCountReducer extends Reducer<Text, IntWritable, Text, LongWritable> {  
  132.   
  133.               @Override  
  134.   
  135.               public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
  136.   
  137.                      long sumBookNum = 0;  
  138.   
  139.                      for (IntWritable value : values) {  
  140.   
  141.                             sumBookNum = sumBookNum + 1;  
  142.   
  143.                      }  
  144.   
  145.                      logger.info("foxson_BookCountReducer_ok");  
  146.   
  147.                      context.write(key, new LongWritable(sumBookNum));  
  148.   
  149.                      System.out.println("foxsonReduce");  
  150.   
  151.               }  
  152.   
  153.        }  
  154.   
  155. }  

上面例子大家可以用来学习,这里在交给大家该如何学习查看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的各个函数及实例




给了这么个例子:
  1.   // Create a new Job
  2.      Job job = new Job(new Configuration());
  3.      job.setJarByClass(MyJob.class);
  4.      
  5.      // Specify various job-specific parameters     
  6.      job.setJobName("myjob");
  7.      
  8.      job.setInputPath(new Path("in"));
  9.      job.setOutputPath(new Path("out"));
  10.      
  11.      job.setMapperClass(MyJob.MyMapper.class);
  12.      job.setReducerClass(MyJob.MyReducer.class);

  13.      // Submit the job, then poll for progress until the job is complete
  14.      job.waitForCompletion(true);
复制代码
上面放到eclipse中,一看不对啊
带个横杠,含义就是被弃用了




下面我们继续寻找:
getInstance() 有很多重载函数,这里不需要解释什么是重载吧,面向对象估计大家学习过,重载就是函数名相同,参数个数和类型可能不同。
好吧,我们试一下这个,如上面新api就是采用这种实例化job的。同时这种实例化的方式采用的是工厂模式,工厂模式,大家也可以找找这方面的资料。




寻找api完毕,更多的函数大家可以在找找。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值