mapReducer 去重副的单词
需求是: 统计输出某目录文件的所有单词,去除重复的单词。
mapper阶段正常做map工作,映射。 切割单词。 <key,value> --> <word,nullWritable>
reducer阶段,对于同一个key 的一组信息,是只输出第一个。
mapper 和wordcount 的单词数是一样的。
package com.mapreduce.mapper; import java.io.IOException; import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class DistinctMapper extends Mapper<LongWritable, Text, Text, NullWritable>{ Text text = new Text();
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String line = value.toString();
String worlds[] = line.split(" ");
for( String word:worlds ){
text.set(word);
context.write(text, NullWritable.get());
}
} }
reducer 对于同一个key 的一组, 只输出一个就ok 了。( ... ... )
package com.mapreduce.mapper; import java.io.IOException; import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class DistincReducer extends Reducer<Text, NullWritable, Text, NullWritable>{ @Override
protected void reduce(Text key, Iterable<NullWritable> value, Context context)
throws IOException, InterruptedException { context.write(key, NullWritable.get());
} }
job 提交
package com.mapreduce.mapper; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class DriverDemo { public static void main(String[] args) throws Exception, IOException { Configuration configuration = new Configuration(); // 2 job Job job = Job.getInstance(configuration); // 3 作业jar包 job.setJarByClass(DriverDemo.class); // 4 map, reduce jar 包
job.setMapperClass(DistinctMapper.class);
job.setReducerClass(DistincReducer.class);
// 5 map 输出类型 job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class); // 6 最终 输出类型 (reducer) job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class); // 7 inputformatclass , outputformatclass 输入输出入文件类型 可能决定分片信息 job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); // 8 输入输出文件路径 FileInputFormat.setInputPaths(job, new Path("d:/input"));
FileOutputFormat.setOutputPath(job, new Path("d:/output5")); // 9 job提交 job.waitForCompletion(true);
} }
mapReducer 去重副的单词的更多相关文章
- Shell统计每个单词出现的个数
题目链接 题目描述 写一个 bash脚本以统计一个文本文件 nowcoder.txt 中每个单词出现的个数. 为了简单起见,你可以假设: nowcoder.txt只包括小写字母和空格. 每个单词只由小 ...
- 用Python读取一个文本文件并统计词频
刚刚在写文章时360浏览器崩溃了,结果内容还是找回来了,感谢博客园的自动保存功能!!! ------------恢复内容开始------------ 最近在学习Python,自己写了一个小程序,可以从 ...
- Text-CNN-文本分类-keras
Text CNN 1. 简介 TextCNN 是利用卷积神经网络对文本进行分类的算法,由 Yoon Kim 在 "Convolutional Neural Networks for Sent ...
- LeetCode(192. Word Frequency)
192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...
- 20 亿的 URL 集合,如何快速判断其中一个?
假设遇到这样一个问题:一个网站有 20 亿 url 存在一个黑名单中,这个黑名单要怎么存?若此时随便输入一个 url,你如何快速判断该 url 是否在这个黑名单中?并且需在给定内存空间(比如:500M ...
- 一道腾讯面试题:如何快速判断某 URL 是否在 20 亿的网址 URL 集合中?布隆过滤器
何为布隆过滤器 还是以上面的例子为例: 判断逻辑: 多次哈希: Guava的BloomFilter 创建BloomFilter 最终还是调用: 使用: 算法特点 使用场景 假设遇到这样一个问题:一个网 ...
- MapReduce简单执行过程及Wordcount案例
MapReducer运行过程 以单词统计为案例. 假如现在文件中存在如下内容: aa bb aa cc dd aa 当然,这是小文件,如果文件大小较大时会将文件进行 "切片" ,此 ...
- MapReduce编程:单词去重
编程实现单词去重要用到NullWritable类型. NullWritable: NullWritable 是一种特殊的Writable 类型,由于它的序列化是零长度的,所以没有字节被写入流或从流中读 ...
- 倒排索引 获取指定单词的文档集合 使用hash去重单词term 提高数据压缩率的方法
倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址.由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inve ...
随机推荐
- 基于CentOS搭建Nginx 静态网站
系统要求: CentOS 7.2 64 位操作系统 一. 安装 Nginx(在 CentOS 上,可直接使用 yum 来安装 Nginx) yum install nginx -y 安装完成后,使用 ...
- R中统计假设检验总结(一)
先PS一个:考虑到这次的题目本身的特点 尝试下把说明性内容都直接作为备注写在语句中 另外用于说明的部分例子参考了我的教授Guy Yollin在Financial Data Analysis and M ...
- 11G新特性 -- ASM的兼容性
Oracle 11g中,asm同时支持10g和11g数据库.但是asm的版本不能低于数据库的版本. 与兼容性现相关的两个参数: ·compatible.rdbms 支持的最低版本的oracle数据库版 ...
- 赋值文件夹名称为日期的doc命令
copy D:\111.txt d:\%date:~0,4%年%date:~5,2%月%date:~8,2%日.*
- unity, ComputeScreenPos 作用
这帖子讲得很清楚:https://forum.unity3d.com/threads/what-does-the-function-computescreenpos-in-unitycg-cginc- ...
- Android Studio配置Android Annotations框架详解--说说那些坑
我们开发过程中都需要写些findViewByid.serOnclickListener等类似的代码,虽然不费事,但是一个项目下来,工作量还是很大的.为了节省工作量,运生了很多对应的注解框架.网上的博客 ...
- css min-width和max-width
min-width: 浏览器缩小设置min-width,元素最小也是min-width设置的值.设置min-width元素不会压扁. max-width:元素最大宽度
- linux 下的emoji在MariaDB中的字符集修改
在此目录下修改这个文件: 添加一行字符集: 然后重启服务,就OK了
- [svc]tomcat配置文件详解-最简单的基于mvn的war包
tomcat安全管理规范 java&tomcat配置参考(多看看这位大牛的博客,写的很好) Tomcat系列之Java技术详解 http://blog.51cto.com/freeloda/1 ...
- 在Vue项目中使用vw实现移动端适配
有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着viewport单位越来越 ...