MapReduce的WordCount应用实例

1、新建一个IDEA的Maven工程


2、引入依赖

    


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<LongWritable, Text, Text, IntWritable> {

    /**
     * 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<Text, IntWritable, Text, IntWritable>{

    public void reduce(Text key, Iterable<IntWritable> 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、显示结果

   

    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值