【Mapreduce】以逗号为分隔符的WordCount词频统计
来源:互联网 发布:hse培训矩阵课件 编辑:程序博客网 时间:2024/05/21 17:04
对原有的WordCount程序进行小修小改。将原本以空格、回车识别单词的WordCount,改成以逗号、回车识别单词的WordCount。以说明Map/Redure到底在做一件什么事。
代码修改之后如下:
import java.io.IOException;import java.util.StringTokenizer;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.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;public class WordCount {//Mapper处理类public static class TokenizerMapper extendsMapper<Object, Text, Text, IntWritable> {private final static IntWritable one = new IntWritable(1);//相当于int one=1;相当于int result;但在mapreducer中,放在context的整形,需要是IntWritableprivate Text word = new Text();//相当于string word="",相当于int result;但在mapreducer中,放在context的字符串,需要是Textpublic void map(Object key, Text value, Context context)throws IOException, InterruptedException {StringTokenizer itr = new StringTokenizer(value.toString(),",");//itr是一个根据,分割之后,遍历分割之后的元素的游标,同://string[] temp=string.split(',');//for(int itr=0;itr<temp.length;itr++)while (itr.hasMoreTokens()) {word.set(itr.nextToken());//相当于string word=itr.nextToken()context.write(word, one);}}}//Reducer处理类public static class IntSumReducer extendsReducer<Text, IntWritable, Text, IntWritable> {private IntWritable result = new IntWritable();//相当于int result;但在mapreducer中,放在context的整形,需要是IntWritablepublic void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum += val.get();}result.set(sum);//相当于int result=sumcontext.write(key, result);}}//主函数,唯一需要修改的部分,就是指明Map/Reduce两个处理类public static void main(String[] args) throws Exception {//指定动作,必须有这几行代码//初始化Configuration conf = new Configuration();String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if (otherArgs.length != 2) {System.err.println("Usage: wordcount <in> <out>");System.exit(2);}Job job = new Job(conf, "word count");job.setMapperClass(TokenizerMapper.class);//指明Map处理类,关键job.setReducerClass(IntSumReducer.class);//指明Reduce处理类,关键//指明输出流job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//指明输入文件是刚刚接受的第0个参数FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//指明输入文件是刚刚接受的第1个参数System.exit(job.waitForCompletion(true) ? 0 : 1);//提交任务,执行//指定动作,必须有这几行代码}}
具体的原理如下图所示:
可以看到,我们程序猿如果需要进行Mapreduce其主要核心就是写好分割过程Map与合并过程Reduce。
Map、Reduce本身就是一个对数据字典遍历的过程。
而Mapreduce在开始,将输入文件,根据回车分成一个个value,在Map到Reduce中途做了一件将写入Context中Key值相同的项的value合并起来。这些都是一些自动完成的过程。当然,处理速度非常快,可以据此,作一些大数据的统计。
0 0
- 【Mapreduce】以逗号为分隔符的WordCount词频统计
- C++读取以逗号为分隔符的数值
- c++读取以逗号为分隔符的一串数字
- C++读取以逗号为分隔符的数值
- sql将列转换为以逗号分隔的字符串
- js以逗号分隔的字符串怎么转化为数组
- 【python】输出列表元素,以空格/逗号为分隔符
- Hadoop最基本的wordcount(统计词频)
- 以逗号为分隔的文本文件怎样用EXCEL打开为表格的形式
- 截取以逗号分隔的字符串
- sql查询以逗号分隔的字段
- sql查询以逗号分隔的字段
- MapReduce实现词频统计
- 在WordCount基础上改进,实现以词频为键值,并按词频降序排列
- Oracle将以逗号分隔的数字字符串根据字典表转换为中文字符串
- Oracle 联接查询,字表多行字段合并为一行,以逗号分隔的查询SQL
- 12.MapReduce第2部分(WordCount词频统计、自然连接)
- C++分割字符串(以逗号分隔符为例,分割得到相应数字)
- Git-仓库状态
- Android WebView Memory Leak WebView内存泄漏
- groovy语法基础
- VisualStudio2010配置OpenCV的一种一劳永逸的方法
- Android 异步通信原理机制-- handler
- 【Mapreduce】以逗号为分隔符的WordCount词频统计
- js实现上传图片实时预览,兼容chrome IE firefox多浏览器
- iGia:smart home security kit
- 使用Quartz2.1.6+Spring3.0实现任务调度简单示例
- Swift方法的简写
- quartz 使用笔记
- LintCode :背包问题
- POJ 1470Closest Common Ancestors(LCA)
- 如何在低版本中适用Android 5.0设计Material风格