Hadoop MapReduce如何进行WordCount自主编译运行

云计算 Hadoop
上次我们已经搭建了Hadoop的伪分布式环境,并且运行了一下Hadoop自带的例子–WordCount程序,展现良好。但是大多数时候还是得自己写程序,编译,打包,然后运行的,所以做一次自编译打包运行的实验。

上次我们已经搭建了Hadoop的伪分布式环境,并且运行了一下Hadoop自带的例子–WordCount程序,展现良好。但是大多数时候还是得自己写程序,编译,打包,然后运行的,所以做一次自编译打包运行的实验。

编辑程序

在Eclipse或者NetBeans中编辑WordCount.java程序,用IDE的好处就是我们可以更方便的选择各种依赖的jar包,并且它会帮我们编译好,我们只需要去workspace中拿出class文件打包就好了,或者直接打包就行。而不用在命令行输入很多依赖jar包去打包,这样更加省事。

1.新建Java Project,名为WordCount,然后建立一个叫test的package,新建WordCount.java,编辑好。结构如下:

 2.这时候我们的workspace/WordCount/bin/test目录下自动生成了编译好的三个class文件。

 3.将class文件打包。如下图所示,在bin/test目录下输入

  1. $ jar cvf WordCount.jar test/ 

即可将class文件打包为WordCount.jar.

 

4.运行hdfs:

  1. $ cd /usr/local/hadoop 
  2. $ ./sbin/start-dfs.sh 
  3. $ jps    //检查是否启动NameNode,DataNode等 

5.往HDFS上的input文件夹中put一个文本文件或者xml文件,上篇文章有讲。比如:

  1. $ hadoop fs -put /usr/local/hadoop/etc/hadoop/*.xml input 

6.运行WordCount.jar

  1. $ cd ~/workspace/WordCount/bin   //进入到WordCount.jar所在目录 
  2. $ hadoop jar WordCount.jar test.WordCount input output 
  3. $ hadoop fs -cat output/part-r-00000   //查看输出 

 

 

 

 

7.关闭hdfs

  1. $ cd /usr/local/hadoop 
  2. $ ./sbin/stop-dfs.sh 

下次运行时须将output目录删除。

到此,我们就编译运行成功了。还是挺简单的。毕竟WordCount是hadoop界的Helloworld啊。

以后我们编写hadoop程序,只需要按这个过程编译打包运行一下就可以了。

一个错误

之前没有指定包,而是放在默认包内的时候,运行

  1. hadoop jar WordCount.jar WordCount input output 

会出现:

  1. Exception in thread "main" java.lang.ClassNotFoundException: WordCount 

的错误,后来将WordCount.java重新写在一个package(test)中就不再有这个问题了。

即第三个参数一定要是入口类,比如程序属于包test,那么第三个参数须是 test.WordCount 。

WordCount 代码

下面的代码下载自网上,我看他还写了很多注释,就直接拿来用了。

  1. package test; 
  2.  
  3. import java.io.IOException; 
  4. import java.util.StringTokenizer; 
  5. import org.apache.hadoop.conf.Configuration; 
  6. import org.apache.hadoop.fs.Path; 
  7. import org.apache.hadoop.io.IntWritable; 
  8. import org.apache.hadoop.io.Text; 
  9. import org.apache.hadoop.mapred.JobConf; 
  10. import org.apache.hadoop.mapreduce.Job; 
  11. import org.apache.hadoop.mapreduce.Mapper; 
  12. import org.apache.hadoop.mapreduce.Reducer; 
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
  15. import org.apache.hadoop.util.GenericOptionsParser; 
  16.  
  17. public class WordCount { 
  18.  
  19.  /**  
  20.      * MapReduceBase类:实现了Mapper和Reducer接口的基类(其中的方法只是实现接口,而未作任何事情)  
  21.      * Mapper接口:  
  22.      * WritableComparable接口:实现WritableComparable的类可以相互比较。所有被用作key的类应该实现此接口。  
  23.      * Reporter 则可用于报告整个应用的运行进度,本例中未使用。   
  24.      *   
  25.      */   
  26.     public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { 
  27.      
  28.   /**  
  29.        * LongWritable, IntWritable, Text 均是 Hadoop 中实现的用于封装 Java 数据类型的类,这些类实现了WritableComparable接口,  
  30.        * 都能够被串行化从而便于在分布式环境中进行数据交换,你可以将它们分别视为long,int,String 的替代品。  
  31.        */  
  32.         private final static IntWritable one = new IntWritable(1); 
  33.         private Text word = new Text();      //Text 实现了BinaryComparable类可以作为key值 
  34.     
  35.     /**  
  36.      * Mapper接口中的map方法:  
  37.      * void map(K1 key, V1 value, OutputCollector<K2,V2> output, Reporter reporter)  
  38.      * 映射一个单个的输入k/v对到一个中间的k/v对  
  39.      * 输出对不需要和输入对是相同的类型,输入对可以映射到0个或多个输出对。  
  40.      * OutputCollector接口:收集Mapper和Reducer输出的<k,v>对。  
  41.      * OutputCollector接口的collect(k, v)方法:增加一个(k,v)对到output  
  42.      */   
  43.         public void map(Object key, Text value, Context context) throws IOException, InterruptedException { 
  44.          
  45.         /** 
  46.          * 原始数据: 
  47.          * c++ java hello 
  48. world java hello 
  49. you me too 
  50. map阶段,数据如下形式作为map的输入值:key为偏移量 
  51. 0  c++ java hello 
  52.             16 world java hello 
  53.             34 you me too 
  54.  
  55.          */ 
  56.  
  57.             StringTokenizer itr = new StringTokenizer(value.toString());  //得到什么值 
  58.       
  59.             while (itr.hasMoreTokens()) { 
  60.                 word.set(itr.nextToken()); 
  61.                 context.write(word, one); 
  62.             } 
  63.         } 
  64.     } 
  65.    
  66.     public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { 
  67.         private IntWritable result = new IntWritable(); 
  68. /** 
  69.  * reduce过程是对输入数据解析形成如下格式数据: 
  70.  * (c++ [1]) 
  71.  * (java [1,1]) 
  72.  * (hello [1,1]) 
  73.  * (world [1]) 
  74.  * (you [1]) 
  75.  * (me [1]) 
  76.  * (you [1]) 
  77.  * 供接下来的实现的reduce程序分析数据数据 
  78.  *  
  79.  */ 
  80.         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { 
  81.             int sum = 0
  82.             for (IntWritable val : values) { 
  83.                 sum += val.get(); 
  84.             } 
  85.             result.set(sum); 
  86.             context.write(key, result); 
  87.         } 
  88.     } 
  89.  
  90.     public static void main(String[] args) throws Exception { 
  91.    
  92.    /**  
  93.        * JobConf:map/reduce的job配置类,向hadoop框架描述map-reduce执行的工作  
  94.        * 构造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等  
  95.        */   
  96.         Configuration conf = new Configuration(); 
  97.         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); 
  98.         //这里需要配置参数即输入和输出的HDFS的文件路径 
  99.         if (otherArgs.length != 2) { 
  100.             System.err.println("Usage: wordcount <in> <out>"); 
  101.             System.exit(2); 
  102.         } 
  103.         Job job = new Job(conf, "word count");      // Job(Configuration conf, String jobName) 
  104.         job.setJarByClass(WordCount.class); 
  105.     job.setMapperClass(TokenizerMapper.class);  // 为job设置Mapper类  
  106.     job.setCombinerClass(IntSumReducer.class);  // 为job设置Combiner类   
  107.     job.setReducerClass(IntSumReducer.class);   // 为job设置Reduce类    
  108.     job.setOutputKeyClass(Text.class);          // 设置输出key的类型 
  109.     job.setOutputValueClass(IntWritable.class); // 设置输出value的类型 
  110.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  111.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); 
  112.     System.exit(job.waitForCompletion(true) ? 0 : 1); 
  113.     } 

 

 

 

 

 

 

责任编辑:赵宁宁 来源: Whatbeg's blog
相关推荐

2010-06-03 11:01:32

Hadoop安装部署

2010-06-03 10:04:26

Hadoop安装

2010-01-20 13:29:40

C++环境

2010-02-05 13:44:36

Dalvik虚拟机

2012-06-29 10:58:27

Hadoop集群

2023-09-27 15:34:48

数据编程

2010-08-05 09:46:45

FlexAIR文件打包

2010-06-03 14:42:47

Hadoop分布式集群

2017-08-04 10:47:20

2021-04-24 23:06:47

JavaScript编程语言

2012-08-08 09:53:23

HadoopMapReduce

2013-01-28 10:11:24

敏捷设计敏捷开发

2010-02-22 16:05:40

Python配置

2013-10-17 23:12:12

Windows 8.1Windows 8.1

2017-07-28 11:31:59

iOS结构优化项目

2021-08-26 10:05:31

APP安全加密网络攻击

2009-12-08 11:34:40

WCF Windows

2010-02-01 10:21:36

Python编码转换

2010-09-17 15:36:24

2023-03-24 16:18:08

微服务架构
点赞
收藏

51CTO技术栈公众号