hadoop程序MapReduce之WordCount

需求:统计一个文件中所有单词出现的个数。

样板:word.log文件中有hadoop hive hbase hadoop hive

输出:hadoop 2

        hive 2

        hbase 1

MapReduce设计方式:

一、Map过程<k,v>键值队的设计:

1、按行将文本文件切割成 <k1,v1>,k1代表:行在文件中的位置,v1代表:一行数据。多少个<k1,v1>就调用多少次map()方法。

2、在map()方法中将一行数据按照空格继续分割成<k2,v2>,K2代表:分割出来的一个单词,v2代表:一个单词个数,此处就是1。

二、Reduce过程<k,v>键值队的设计:

3、此处会经过一系列处理:比如combine,partition,shuffle等传入Reduce中的键值队<k3,v3> ,k3代表:相同的key合并在一起,v3代表:相同key的value值list<values>,此处     全是1。多少个<k3,v3>就调用多少次reduce()方法。

4、统计出单词个数输出格式<k4,v4>,k4代表:单词,v4代表:单词总个数。

程序实现:

WordCountMapper类

package com.cn;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class WordCountMapper extends Mapper<Object, Text, Text, IntWritable> {

private final static IntWritable one = new IntWritable(1);

private Text word = new Text();

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

StringTokenizer itr = new StringTokenizer(value.toString());

while (itr.hasMoreTokens()) {

word.set(itr.nextToken());

context.write(word, one);

}

}
}

WordCountReducer类

package com.cn;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        int sum = 0;

        for (IntWritable val : values) {

            sum += val.get();

        }

        result.set(sum);

        context.write(key, result);

    }

}

WordCount类

package com.cn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

      public static void main(String[] args) throws Exception {

           Configuration conf = new Configuration();

            String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

             if (otherArgs.length != 2) {

               System.err.println("Usage: wordcount  ");
               System.exit(2);
             }

             /**创建一个job,起个名字以便跟踪查看任务执行情况**/

             Job job = new Job(conf, "word count");

             /**当在hadoop集群上运行作业时,需要把代码打包成一个jar文件(hadoop会在集群分发这个文件),通过job的setJarByClass设置一个类,hadoop根据这个类找到所在的jar文件**/

            job.setJarByClass(WordCount1.class);

            /**设置要使用的map、combiner、reduce类型**/

            job.setMapperClass(WordCountMapper.class);   

            job.setCombinerClass(WordCountReducer.class);

            job.setReducerClass(WordCountReducer.class);

           /**设置map和reduce函数的输入类型,这里没有代码是因为我们使用默认的TextInputFormat,针对文本文件,按行将文本文件切割成 InputSplits, 并用 LineRecordReader 将 InputSplit 解析成 <key,value>: 对,key 是行在文件中的位置,value 是文件中的一行**/

            /**设置map和reduce函数的输出键和输出值类型**/

            job.setOutputKeyClass(Text.class);

            job.setOutputValueClass(IntWritable.class);

            /**设置输入和输出路径**/
            FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
            FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); 
            
           /**提交作业并等待它完成**/
            System.exit(job.waitForCompletion(true) ? 0 : 1);

          }

    }

运用到的hadoop命令:

hadoop fs -mkdir  /tmp/input      

hadoop fs -put /tmp/log/word.log  /tmp/input/   

hadoop jar  /tep/hadoop/WordCount.jar /tmp/input /tmp/output     

记录自己每个学习过程的点点滴滴。最好尝试分析运行过程。

原文地址:https://www.cnblogs.com/xubiao/p/5743184.html