`
柯小芍
  • 浏览: 12996 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

Hadoop学习笔记之WordCount

 
阅读更多

        WordCount是hadoop的一个入门程序,其地位相当与java中的HelloWorld,它使用map/reduce对文本中出现的单词进行计数。

 

    程序分三个部分:

    Map类  Reduce类 和主函数

    Map类:Map类继承Mapreducebase类并实现了Mapper接口,其中 MapReduceBase类:实现了Mapper和Reducer接口的基类(其中的方法只是实现接口,而未作任何事情)。Map在实现Mapper接口之后重写其map方法,将输入的<k1/v1>进行处理生成<k2/v2>作为reduce输入。

  Reduce类:同样继承MapReduceBase类。实现Reducer接口,重写reduce()方法,将输入的<k2/v2>进行处理生成<k3/v3>输出。

  主函数 :主函数是程序的入口,生成JobConf对象向hadoop框架描述map/reduce执行的工作。

 

源码详细分析:一、Map类, Map类实现了Mapper接口并对map()方法进行了重写。

 public static class Map extends MapReduceBase implements  
            Mapper<LongWritable, Text, Text, IntWritable> 
 //Mapper<k1,v1,k2,v2> k  v 表示输入和输出的数据类型,其中k1应为Object或者LongWritable
,因为map在读取文本时是一行一行读取的。
    {  
        private final static IntWritable one = new IntWritable(1);//将每个单词的数量都设成1.  
        private Text word = new Text();  //Text一个变量相当于String,用来存储传来的文本的值
        public void map(LongWritable key, Text value,  
                OutputCollector<Text, IntWritable> output, Reporter reporter)  
                throws IOException  //具体的map函数在每个节点上执行,将<k1/v1>处理成<k2/v2>用output.collect()输出给
Reduce。
        {  
            String line = value.toString();  //将Text类型转换成String
            StringTokenizer tokenizer = new StringTokenizer(line); //再转换成StringTokenizer 
            while (tokenizer.hasMoreTokens())  //当文本有分割符时
            {  
                word.set(tokenizer.nextToken());  //返回从当前位置到下一个分割符的字符串
                output.collect(word, one);  
            }  
        }  
    }  
比如一行文本: welcome to lanjie  laijie lead to success
Map进行处理之后的结果是:
welcome 1
to 1
lanjie 1
lanjie 1
lead 1
to 1
success 1

二、Reduce类

 public static class Reduce extends MapReduceBase implements  
            Reducer<Text, IntWritable, Text, IntWritable>  //<Text, IntWritable, Text, IntWritable>字段对应
着<k2,v2,k3,v3>
    {  
        public void reduce(Text key, Iterator<IntWritable> values,  
                OutputCollector<Text, IntWritable> output, Reporter reporter)  
                throws IOException  //Text key, Iterator<IntWritable> values 对应从map传来的<k2/v2>,
 OutputCollector<Text, IntWritable> output用来保存结果
        {  
            int sum = 0;  
            while (values.hasNext())  
            {  
                sum += values.next().get();  //计算每个key的数量,得到总数保存在sum中
            }  
            output.collect(key, new IntWritable(sum)); //将数据写到文件中 
        }  
    }  
比如:welcome to lanjie laijie lead to success
输出
lanjie 2
lead 1
success 1
to 2
welcome 1
系统默认排序为升序。
        

三、主函数

 //程序的入口,即住函数
    public static void main(String[] args) throws Exception  
    {  
    	String path1="hdfs://master:9000/xiong/test";//要统计的文件
    	String path2="hdfs://master:9000/xiong/result";//保存结果的文件
    	
        /** 
         * JobConf:map/reduce的job配置类,向hadoop框架描述map-reduce执行的工作 
         * 构造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等 
         */  
        JobConf conf = new JobConf(WordCount.class);  
        conf.setJobName("wordcount");           //设置一个用户定义的job名称  
  
        conf.setOutputKeyClass(Text.class);    //为job的输出数据设置Key类  
        conf.setOutputValueClass(IntWritable.class);   //为job输出设置value类  
  
        conf.setMapperClass(Map.class);         //为job设置Mapper类  
        conf.setCombinerClass(Reduce.class);      //为job设置Combiner类  
        conf.setReducerClass(Reduce.class);        //为job设置Reduce类  
  
        conf.setInputFormat(TextInputFormat.class);    //为map-reduce任务设置InputFormat实现类  
        conf.setOutputFormat(TextOutputFormat.class);  //为map-reduce任务设置OutputFormat实现类  
  
        /** 
         * InputFormat描述map-reduce中对job的输入定义 
         * setInputPaths():为map-reduce job设置路径数组作为输入列表 
         * setInputPath():为map-reduce job设置路径数组作为输出列表 
         */  
        FileInputFormat.setInputPaths(conf, new Path(path1));  
        FileOutputFormat.setOutputPath(conf, new Path(path2));  
  
        JobClient.runJob(conf);         //运行一个job  
    }  
    
    

}

具体完整代码如下:

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

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;


public class WordCount{
    public static class Map extends MapReduceBase implements  
            Mapper<LongWritable, Text, Text, IntWritable>  
    {  
        private final static IntWritable one = new IntWritable(1);  
        private Text word = new Text();  
        public void map(LongWritable key, Text value,  
                OutputCollector<Text, IntWritable> output, Reporter reporter)  
                throws IOException  
        {  
            String line = value.toString();  
            StringTokenizer tokenizer = new StringTokenizer(line);  
            while (tokenizer.hasMoreTokens())  
            {  
                word.set(tokenizer.nextToken());  
                output.collect(word, one);  
            }  
        }  
    }  
    /**
     * 这是一个实现reduce的类
     * @author hadoop
     *
     */
    public static class Reduce extends MapReduceBase implements  
            Reducer<Text, IntWritable, Text, IntWritable>  
    {  
        public void reduce(Text key, Iterator<IntWritable> values,  
                OutputCollector<Text, IntWritable> output, Reporter reporter)  
                throws IOException  
        {  
            int sum = 0;  
            while (values.hasNext())  
            {  
                sum += values.next().get();  
            }  
            output.collect(key, new IntWritable(sum)); //将数据写到文件中 
        }  
    }  
    //程序的入口,即住函数
    public static void main(String[] args) throws Exception  
    {  
    	String path1="hdfs://master:9000/xiong/test";//要统计的文件
    	String path2="hdfs://master:9000/xiong/result";//保存结果的文件
    	
        /** 
         * JobConf:map/reduce的job配置类,向hadoop框架描述map-reduce执行的工作 
         * 构造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等 
         */  
        JobConf conf = new JobConf(WordCount.class);  
        conf.setJobName("wordcount");           //设置一个用户定义的job名称  
  
        conf.setOutputKeyClass(Text.class);    //为job的输出数据设置Key类  
        conf.setOutputValueClass(IntWritable.class);   //为job输出设置value类  
  
        conf.setMapperClass(Map.class);         //为job设置Mapper类  
        conf.setCombinerClass(Reduce.class);      //为job设置Combiner类  
        conf.setReducerClass(Reduce.class);        //为job设置Reduce类  
  
        conf.setInputFormat(TextInputFormat.class);    //为map-reduce任务设置InputFormat实现类  
        conf.setOutputFormat(TextOutputFormat.class);  //为map-reduce任务设置OutputFormat实现类  
  
        /** 
         * InputFormat描述map-reduce中对job的输入定义 
         * setInputPaths():为map-reduce job设置路径数组作为输入列表 
         * setInputPath():为map-reduce job设置路径数组作为输出列表 
         */  
        FileInputFormat.setInputPaths(conf, new Path(path1));  
        FileOutputFormat.setOutputPath(conf, new Path(path2));  
  
        JobClient.runJob(conf);         //运行一个job  
    }  
    
    

}
 

 

   

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics