Hadoop2.6.0版本MapReudce示例之WordCount(一)

一、准备测试数据

        1、在本地Linux系统/var/lib/Hadoop-hdfs/file/路径下准备两个文件file1.txt和file2.txt,文件列表及各自内容如下图所示:

        2、在hdfs中,准备/input路径,并上传两个文件file1.txt和file2.txt,如下图所示:

        二、编写代码,封装Jar包并上传至linux

        将代码封装成TestMapReduce.jar,并上传至linux的/usr/local路径下,如下图所示:

        三、运行命令

        执行命令如下:hadoop jar /usr/local/TestMapReduce.jar com.jngreen.mapreduce.test.WordCount /input/file1.txt /input/file2.txt /output/output

        命令执行过程截图如下:

        四、查看运行结果

        查看hdfs输出路径/output下的结果,如下图所示:

        运行结果为Hello 4、Hadoop 1、Man 1、Boy 1、Word 1,完全正确!

        五、WordCount展示

        源码如下:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.util.StringTokenizer;  
  3.   
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.mapreduce.Job;  
  9. import org.apache.hadoop.mapreduce.Mapper;  
  10. import org.apache.hadoop.mapreduce.Reducer;  
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  13.   
  14. public class WordCount {  
  15.   
  16.   // TokenizerMapper作为Map阶段,需要继承Mapper,并重写map()函数  
  17.   public static class TokenizerMapper   
  18.        extends Mapper<Object, Text, Text, IntWritable>{  
  19.       
  20.     private final static IntWritable one = new IntWritable(1);  
  21.     private Text word = new Text();  
  22.         
  23.     public void map(Object key, Text value, Context context  
  24.                     ) throws IOException, InterruptedException {  
  25.         
  26.       // 用StringTokenizer作为分词器,对value进行分词  
  27.       StringTokenizer itr = new StringTokenizer(value.toString());  
  28.         
  29.       // 遍历分词后结果  
  30.       while (itr.hasMoreTokens()) {  
  31.             
  32.         // 将String设置入Text类型word  
  33.         word.set(itr.nextToken());  
  34.         // 将(word,1),即(Text,IntWritable)写入上下文context,供后续Reduce阶段使用  
  35.         context.write(word, one);  
  36.       }  
  37.     }  
  38.   }  
  39.     
  40.   // IntSumReducer作为Reduce阶段,需要继承Reducer,并重写reduce()函数  
  41.   public static class IntSumReducer   
  42.        extends Reducer<Text,IntWritable,Text,IntWritable> {  
  43.     private IntWritable result = new IntWritable();  
  44.   
  45.     public void reduce(Text key, Iterable<IntWritable> values,   
  46.                        Context context  
  47.                        ) throws IOException, InterruptedException {  
  48.       int sum = 0;  
  49.       // 遍历map阶段输出结果中的values中每个val,累加至sum  
  50.       for (IntWritable val : values) {  
  51.         sum += val.get();  
  52.       }  
  53.         
  54.       // 将sum设置入IntWritable类型result  
  55.       result.set(sum);  
  56.         
  57.       // 通过上下文context的write()方法,输出结果(key, result),即(Text,IntWritable)  
  58.       context.write(key, result);  
  59.     }  
  60.   }  
  61.   
  62.   public static void main(String[] args) throws Exception {  
  63.     // 加载hadoop配置  
  64.     Configuration conf = new Configuration();  
  65.       
  66.     // 校验命令行输入参数  
  67.     if (args.length < 2) {  
  68.       System.err.println("Usage: wordcount <in> [<in>...] <out>");  
  69.       System.exit(2);  
  70.     }  
  71.       
  72.     // 构造一个Job实例job,并命名为"word count"  
  73.     Job job = new Job(conf, "word count");  
  74.       
  75.     // 设置jar  
  76.     job.setJarByClass(WordCount.class);  
  77.       
  78.     // 设置Mapper  
  79.     job.setMapperClass(TokenizerMapper.class);  
  80.     // 设置Combiner  
  81.     job.setCombinerClass(IntSumReducer.class);  
  82.     // 设置Reducer  
  83.     job.setReducerClass(IntSumReducer.class);  
  84.     // 设置OutputKey  
  85.     job.setOutputKeyClass(Text.class);  
  86.     // 设置OutputValue  
  87.     job.setOutputValueClass(IntWritable.class);  
  88.       
  89.     // 添加输入路径  
  90.     for (int i = 0; i < args.length - 1; ++i) {  
  91.       FileInputFormat.addInputPath(job, new Path(args[i]));  
  92.     }  
  93.       
  94.     // 添加输出路径  
  95.     FileOutputFormat.setOutputPath(job,  
  96.       new Path(args[args.length - 1]));  
  97.       
  98.     // 等待作业job运行完成并退出  
  99.     System.exit(job.waitForCompletion(true) ? 0 : 1);  
  100.   }  
  101. }  
原文地址:https://www.cnblogs.com/jirimutu01/p/5556330.html