hadoop运行WordCount程序

1.建立测试程序 WordCount.java

  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5. import java.util.StringTokenizer;  
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.conf.Configured;  
  9. import org.apache.hadoop.fs.Path;  
  10. import org.apache.hadoop.io.IntWritable;  
  11. import org.apache.hadoop.io.LongWritable;  
  12. import org.apache.hadoop.io.Text;  
  13. import org.apache.hadoop.mapred.FileInputFormat;  
  14. import org.apache.hadoop.mapred.FileOutputFormat;  
  15. import org.apache.hadoop.mapred.JobClient;  
  16. import org.apache.hadoop.mapred.JobConf;  
  17. import org.apache.hadoop.mapred.MapReduceBase;  
  18. import org.apache.hadoop.mapred.Mapper;  
  19. import org.apache.hadoop.mapred.OutputCollector;  
  20. import org.apache.hadoop.mapred.Reducer;  
  21. import org.apache.hadoop.mapred.Reporter;  
  22. import org.apache.hadoop.util.Tool;  
  23. import org.apache.hadoop.util.ToolRunner;  
  24.   
  25. public class WordCount extends Configured implements Tool {  
  26.   
  27.     public static class MapClass extends MapReduceBase implements  
  28.             Mapper<LongWritable, Text, Text, IntWritable> {  
  29.   
  30.         private final static IntWritable one = new IntWritable(1);  
  31.         private Text word = new Text();  
  32.   
  33.         public void map(LongWritable key, Text value,  
  34.                 OutputCollector<Text, IntWritable> output, Reporter reporter)  
  35.                 throws IOException {  
  36.             String line = value.toString();  
  37.             StringTokenizer itr = new StringTokenizer(line);  
  38.             while (itr.hasMoreTokens()) {  
  39.                 word.set(itr.nextToken());  
  40.                 output.collect(word, one);  
  41.             }  
  42.         }  
  43.     }  
  44.   
  45.     /** 
  46.      * A reducer class that just emits the sum of the input values. 
  47.      */  
  48.     public static class Reduce extends MapReduceBase implements  
  49.             Reducer<Text, IntWritable, Text, IntWritable> {  
  50.   
  51.         public void reduce(Text key, Iterator<IntWritable> values,  
  52.                 OutputCollector<Text, IntWritable> output, Reporter reporter)  
  53.                 throws IOException {  
  54.             int sum = 0;  
  55.             while (values.hasNext()) {  
  56.                 sum += values.next().get();  
  57.             }  
  58.             output.collect(key, new IntWritable(sum));  
  59.         }  
  60.     }  
  61.   
  62.     static int printUsage() {  
  63.         System.out.println("wordcount [-m <maps>] [-r <reduces>] <input> <output>");  
  64.         ToolRunner.printGenericCommandUsage(System.out);  
  65.         return -1;  
  66.     }  
  67.   
  68.     /** 
  69.      * The main driver for word count map/reduce program. Invoke this method to 
  70.      * submit the map/reduce job. 
  71.      * 
  72.      * @throws IOException 
  73.      *             When there is communication problems with the job tracker. 
  74.      */  
  75.     public int run(String[] args) throws Exception {  
  76.         JobConf conf = new JobConf(getConf(), WordCount.class);  
  77.         conf.setJobName("wordcount");  
  78.   
  79.         // the keys are words (strings)  
  80.         conf.setOutputKeyClass(Text.class);  
  81.         // the values are counts (ints)  
  82.         conf.setOutputValueClass(IntWritable.class);  
  83.   
  84.         conf.setMapperClass(MapClass.class);  
  85.         conf.setCombinerClass(Reduce.class);  
  86.         conf.setReducerClass(Reduce.class);  
  87.   
  88.         List<String> other_args = new ArrayList<String>();  
  89.         for (int i = 0; i < args.length; ++i) {  
  90.             try {  
  91.                 if ("-m".equals(args[i])) {  
  92.                     conf.setNumMapTasks(Integer.parseInt(args[++i]));  
  93.                 } else if ("-r".equals(args[i])) {  
  94.                     conf.setNumReduceTasks(Integer.parseInt(args[++i]));  
  95.                 } else {  
  96.                     other_args.add(args[i]);  
  97.                 }  
  98.             } catch (NumberFormatException except) {  
  99.                 System.out.println("ERROR: Integer expected instead of "  
  100.                         + args[i]);  
  101.                 return printUsage();  
  102.             } catch (ArrayIndexOutOfBoundsException except) {  
  103.                 System.out.println("ERROR: Required parameter missing from "  
  104.                         + args[i - 1]);  
  105.                 return printUsage();  
  106.             }  
  107.         }  
  108.   
  109.         // Make sure there are exactly 2 parameters left.  
  110.         if (other_args.size() != 2) {  
  111.             System.out.println("ERROR: Wrong number of parameters: "  
  112.                     + other_args.size() + " instead of 2.");  
  113.             return printUsage();  
  114.         }  
  115.         FileInputFormat.setInputPaths(conf, other_args.get(0));  
  116.         FileOutputFormat.setOutputPath(conf, new Path(other_args.get(1)));  
  117.   
  118.         JobClient.runJob(conf);  
  119.         return 0;  
  120.     }  
  121.   
  122.     public static void main(String[] args) throws Exception {  
  123.         int res = ToolRunner.run(new Configuration(), new WordCount(), args);  
  124.         System.exit(res);  
  125.     }  
  126.   
  127. }  


编译上述程序,利用下述命令将编译所得的class文件打包成jar包
[plain] view plain copy
  1. jar cvf  WordCount.jar *.class  

2. 建立测试用的输入文件

建立/usr/file01和/usr/file02两个文件,里面随机敲入一些单词。


3. 启动Hadoop。

[plain] view plain copy
  1. $ bin/hadoop namenode -format  
  2. $ bin/start-all.sh  
确保所有节点都可以正常运行,检查 http://localhost:50030/jobtracker.jsphttp://localhost:50070/dfshealth.jsp能否正常显示,以及确认hadoop在启动过程中终端无报错信确息。


4.将测试文件复制到hdfs中

利用下列命令在hdfs中哦该拿创建input文件夹,并将测试输入文件复制到hdfs下的/tmp/input文件夹中

[plain] view plain copy
  1. $ bin/hadoop fs -mkdir /tmp/input  
  2. $ bin/hadoop fs -put file01 /tmp/input/  
  3. $ bin/hadoop fs -put file02 /tmp/input/  


5.运行WordCount程序

第一个参数是MapReduce的输入内容,第二个参数是MapReduce的输出内容

[plain] view plain copy
  1. $ bin/hadoop jar WordCount.jar WordCount /tmp/input /tmp/output  

执行成功后查看output文件夹

[plain] view plain copy
  1. $ bin/hadoop fs -ls /tmp/output/  

显示如下结果:

[plain] view plain copy
  1. Found 2 items  
  2. drwxr-x--- - admin admin 0 2010-09-16 22:43 /tmp/output/_logs  
  3. -rw-r----- 1 admin admin 102 2010-09-16 22:44 /tmp/output/part-00000  

查看执行结果:

[plain] view plain copy
  1. $ bin/hadoop fs -cat /tmp/output/part-00000  

结果为:

[plain] view plain copy
  1. Goodbye 2  
  2. Hadoop  4  
  3. Hello   2 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值