二、手写WordCount

一:问题介绍

统计每一个单词在整个数据集中出现的总次数。

 

数据流程:

 

 

二:需要的jar包

Hadoop-2.4.1\share\hadoop\hdfs\hadoop-hdfs-2.4.1.jar
hadoop-2.4.1\share\hadoop\hdfs\lib\所有jar包

hadoop-2.4.1\share\hadoop\common\hadoop-common-2.4.1.jar
hadoop-2.4.1\share\hadoop\common\lib\所有jar包

hadoop-2.4.1\share\hadoop\mapreduce\除hadoop-mapreduce-examples-2.4.1.jar之外的jar包
hadoop-2.4.1\share\hadoop\mapreduce\lib\所有jar包

 

 

三:代码

mapper类实现:

 

 
  1. /*

  2. * KEYIN:输入kv数据对中key的数据类型

  3. * VALUEIN:输入kv数据对中value的数据类型

  4. * KEYOUT:输出kv数据对中key的数据类型

  5. * VALUEOUT:输出kv数据对中value的数据类型

  6. */

  7. public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

  8.  
  9. /*

  10. * map方法是提供给map task进程来调用的,map task进程是每读取一行文本来调用一次我们自定义的map方法

  11. * map task在调用map方法时,传递的参数:

  12. * 一行的起始偏移量LongWritable作为key

  13. * 一行的文本内容Text作为value

  14. */

  15. @Override

  16. protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {

  17. //拿到一行文本内容,转换成String 类型

  18. String line = value.toString();

  19. //将这行文本切分成单词

  20. String[] words=line.split(" ");

  21.  
  22. //输出<单词,1>

  23. for(String word:words){

  24. context.write(new Text(word), new IntWritable(1));

  25. }

  26. }

  27. }

 

reducer类实现:

 

 
  1. /*

  2. * KEYIN:对应mapper阶段输出的key类型

  3. * VALUEIN:对应mapper阶段输出的value类型

  4. * KEYOUT:reduce处理完之后输出的结果kv对中key的类型

  5. * VALUEOUT:reduce处理完之后输出的结果kv对中value的类型

  6. */

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

  8. @Override

  9. /*

  10. * reduce方法提供给reduce task进程来调用

  11. *

  12. * reduce task会将shuffle阶段分发过来的大量kv数据对进行聚合,聚合的机制是相同key的kv对聚合为一组

  13. * 然后reduce task对每一组聚合kv调用一次我们自定义的reduce方法

  14. * 比如:<hello,1><hello,1><hello,1><tom,1><tom,1><tom,1>

  15. * hello组会调用一次reduce方法进行处理,tom组也会调用一次reduce方法进行处理

  16. * 调用时传递的参数:

  17. * key:一组kv中的key

  18. * values:一组kv中所有value的迭代器

  19. */

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

  21. //定义一个计数器

  22. int count = 0;

  23. //通过value这个迭代器,遍历这一组kv中所有的value,进行累加

  24. for(IntWritable value:values){

  25. count+=value.get();

  26. }

  27.  
  28. //输出这个单词的统计结果

  29. context.write(key, new IntWritable(count));

  30. }

  31. }

 

job提交客户端实现:

 

 
  1. public class WordCountJobSubmitter {

  2.  
  3. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

  4. Configuration conf = new Configuration();

  5. Job wordCountJob = Job.getInstance(conf);

  6.  
  7. //重要:指定本job所在的jar包

  8. wordCountJob.setJarByClass(WordCountJobSubmitter.class);

  9.  
  10. //设置wordCountJob所用的mapper逻辑类为哪个类

  11. wordCountJob.setMapperClass(WordCountMapper.class);

  12. //设置wordCountJob所用的reducer逻辑类为哪个类

  13. wordCountJob.setReducerClass(WordCountReducer.class);

  14.  
  15. //设置map阶段输出的kv数据类型

  16. wordCountJob.setMapOutputKeyClass(Text.class);

  17. wordCountJob.setMapOutputValueClass(IntWritable.class);

  18.  
  19. //设置最终输出的kv数据类型

  20. wordCountJob.setOutputKeyClass(Text.class);

  21. wordCountJob.setOutputValueClass(IntWritable.class);

  22.  
  23. //设置要处理的文本数据所存放的路径

  24. FileInputFormat.setInputPaths(wordCountJob, "hdfs://192.168.77.70:9000/wordcount/srcdata/");

  25. FileOutputFormat.setOutputPath(wordCountJob, new Path("hdfs://192.168.77.70:9000/wordcount/output/"));

  26.  
  27. //提交job给hadoop集群

  28. wordCountJob.waitForCompletion(true);

  29. }

  30. }

 

 

 

四:操作流程

1、将项目打成jar包上传到虚拟机上

 

2、创建文本数据

 

3、将文本数据上传到hdfs

 

4、运行jar文件

 

5、结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值