Mapreduce实例---统计单词个数(wordcount)
来源:互联网 发布:wmf榨汁机好用吗 知乎 编辑:程序博客网 时间:2024/06/05 07:21
一:问题介绍
统计每一个单词在整个数据集中出现的总次数。
数据流程:
二:需要的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类实现:
/* * KEYIN:输入kv数据对中key的数据类型 * VALUEIN:输入kv数据对中value的数据类型 * KEYOUT:输出kv数据对中key的数据类型 * VALUEOUT:输出kv数据对中value的数据类型 */public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{/* * map方法是提供给map task进程来调用的,map task进程是每读取一行文本来调用一次我们自定义的map方法 * map task在调用map方法时,传递的参数: * 一行的起始偏移量LongWritable作为key * 一行的文本内容Text作为value */@Overrideprotected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {//拿到一行文本内容,转换成String 类型String line = value.toString();//将这行文本切分成单词String[] words=line.split(" ");//输出<单词,1>for(String word:words){context.write(new Text(word), new IntWritable(1));}}}
reducer类实现:
/* * KEYIN:对应mapper阶段输出的key类型 * VALUEIN:对应mapper阶段输出的value类型 * KEYOUT:reduce处理完之后输出的结果kv对中key的类型 * VALUEOUT:reduce处理完之后输出的结果kv对中value的类型 */public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{@Override/* * reduce方法提供给reduce task进程来调用 * * reduce task会将shuffle阶段分发过来的大量kv数据对进行聚合,聚合的机制是相同key的kv对聚合为一组 * 然后reduce task对每一组聚合kv调用一次我们自定义的reduce方法 * 比如:<hello,1><hello,1><hello,1><tom,1><tom,1><tom,1> * hello组会调用一次reduce方法进行处理,tom组也会调用一次reduce方法进行处理 * 调用时传递的参数: * key:一组kv中的key * values:一组kv中所有value的迭代器 */protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {//定义一个计数器int count = 0;//通过value这个迭代器,遍历这一组kv中所有的value,进行累加for(IntWritable value:values){count+=value.get();}//输出这个单词的统计结果context.write(key, new IntWritable(count));}}
job提交客户端实现:
public class WordCountJobSubmitter {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf = new Configuration();Job wordCountJob = Job.getInstance(conf);//重要:指定本job所在的jar包wordCountJob.setJarByClass(WordCountJobSubmitter.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.77.70:9000/wordcount/srcdata/");FileOutputFormat.setOutputPath(wordCountJob, new Path("hdfs://192.168.77.70:9000/wordcount/output/"));//提交job给hadoop集群wordCountJob.waitForCompletion(true);}}
四:操作流程
1、将项目打成jar包上传到虚拟机上
2、创建文本数据
3、将文本数据上传到hdfs
4、运行jar文件
5、结果
1 0
- Mapreduce实例---统计单词个数(wordcount)
- 【MapReduce实例】单词统计
- 利用mapreduce统计单词个数
- MapReduce实例----统计个数
- [pig] 统计行数和单词个数wordcount
- MapReduce之单词(字母)个数的统计
- Hadoop实例-----统计单词个数
- Hadoop-MapReduce初步应用-统计单词个数
- 和我一起学Hadoop(五):MapReduce的单词统计,wordcount
- MapReduce之WordCount单词计数(上)
- MapReduce之WordCount单词计数(下)
- pig分析日志脚本(1) 统计行数和单词个数wordcount
- hadoop入门(六)JavaAPI+Mapreduce实例wordCount单词计数详解
- Hadoop入门实例——WordCount统计单词
- 统计单词个数(26)
- 【hadoop】 3002-mapreduce程序统计单词个数示例
- 简单的单词个数统计的mapreduce 的代码实现
- MapReduce之WordCount单词计数
- 不正经运维狗的文档2_8
- Linux 文件系统
- Apache Maven 3.5.0版详述
- Base64 编码和性能,第 2 部分
- HTML协议与相应
- Mapreduce实例---统计单词个数(wordcount)
- fork boom
- "undefined reference to" 问题解决方法
- MySQL索引类型 btree索引和hash索引的区别
- Xi and Bo
- linux学习
- Disruptor消息处理器
- 贝叶斯公式(Bayes)的应用
- GDOI2017游记