MapReduce的WordCount应用实例

1、新建一个IDEA的Maven工程


2、引入依赖

     MapReduce的WordCount应用实例_第1张图片


3、Mapper类

    

package com.motoon;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author rjsong
 */
public class WordCountMapper extends Mapper {

    /**
     * map阶段的业务逻辑就写在自定义的map()方法中
     * maptask会对每一行输入数据调用一次我们自定义的map()方法
     */
    protected void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException{

        //maptask传过来的文本先转换成String
        String line = value.toString();
        //根据空格将这一行分成单词
        String[] words =line.split(" ");
        //将单词输出为<单词,1>
        for (String word:words){
            //将单词作为key,将次数1作为value,以便于后续的数据开发,可以根据单词分发,以便于相同单词会到相同的reduce task
            context.write(new Text(word), new IntWritable(1));
        }
    }
}


4、Reducer类

     

package com.motoon;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
 * @author rjsong
 */
public class WordCountReducer extends Reducer{

    public void reduce(Text key, Iterable values, Context context)
            throws IOException,InterruptedException{
        int count = 0;
        for (IntWritable value:values){
            count+=value.get();
        }
        context.write(key, new IntWritable(count));
    }
}


5、main类

    

package com.motoon;

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;

/**
 * @author rjsong
 */
public class WordCountDriver {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job wordCountJob = Job.getInstance(conf);

        //重要:指定本job所在的jar        wordCountJob.setJarByClass(WordCountDriver.class);
        //设置wordCountJob所用的mapper逻辑类为哪个类
        wordCountJob.setMapperClass(WordCountMapper.class);
        //设置wordCountJob所用的reducer逻辑类为哪个类
        wordCountJob.setReducerClass(WordCountReducer.class);
        //设置map阶段输出的kv数据类型
        wordCountJob.setMapOutputKeyClass(Text.class);
        wordCountJob.setMapOutputValueClass(IntWritable.class);
        //设置最终输出的kv数据类型
        wordCountJob.setOutputKeyClass(Text.class);
        wordCountJob.setOutputValueClass(IntWritable.class);
        //设置要处理的文本数据所存放的路径
        FileInputFormat.setInputPaths(wordCountJob, "hdfs://192.168.61.138:9000/wordcount/input/");
        FileOutputFormat.setOutputPath(wordCountJob, new Path("hdfs://192.168.61.138:9000/wordcount/output/"));
        //提交jobhadoop
        wordCountJob.waitForCompletion(true);
    }
}



6、导出jar包,在工作区间目录下会产生一个out目录,里面有相应的jar文件。

   
    



7、将jar文件上传到linux服务器上

   
   
  

8、创建文本数据
    
   

9、将文本数据上传到hdfs上
    
   
    
   
10、运行jar文件
    
   


11、显示结果

   

    

你可能感兴趣的:(Hadoop)