Hadoop 统计文件中某个单词出现的次数
如文件word.txt内容如下:
what is you name?
my name is zhang san。
要求统计word.txt中出现“is”的次数?
代码如下:
PerWordMapper
package com.hadoop.wordcount; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class PerWordMapper extends Mapper<Object, Text, Text, IntWritable> { public Text keyText = new Text();
public IntWritable intValue = new IntWritable(1); @Override
protected void map(Object key, Text value,
Context context)
throws IOException, InterruptedException {
String str = value.toString();
StringTokenizer to = new StringTokenizer(str);
while (to.hasMoreTokens()) {
String t = to.nextToken();
//此处为判断统计字符串的地方
if(t.equals("is")){
keyText = new Text(t);
context.write(keyText, intValue);
} }
}
}
PerWordReducer
package com.hadoop.wordcount; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class PerWordReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public IntWritable intValue = new IntWritable(0);
@Override
protected void reduce(Text key, Iterable<IntWritable> value,
Context context)
throws IOException, InterruptedException {
int sum = 0;
while(value.iterator().hasNext()){
sum += value.iterator().next().get();
}
intValue.set(sum);
context.write(key, intValue);
} }
PerWordCount
package com.hadoop.wordcount; import java.io.IOException; 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;
import org.apache.hadoop.util.GenericOptionsParser; import com.hadoop.mapreducer.MapperClass;
import com.hadoop.mapreducer.ReducerClass;
import com.hadoop.mapreducer.WordCount; public class PerWordCount {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
System.out.println("otherArgs.length:"+otherArgs.length);
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(PerWordCount.class);
job.setMapperClass(PerWordMapper.class);
job.setCombinerClass(PerWordReducer.class);
job.setReducerClass(PerWordReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
} }
Hadoop 统计文件中某个单词出现的次数的更多相关文章
- python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
- Java笔记13:统计文件中每个字符出现的次数
一.代码实现 import java.io.*; import java.util.*; /** 功能:统计文件中每个字符出现的次数 思路: 1.定义字符读取(缓冲)流 2.循环读取文件里的字符,用一 ...
- Scala快速统计文件中特定单词,字符的个数
val fileContent=Source.fromFile("/home/soyo/桌面/ss5.txt").getLines.mkString(",") ...
- linux命令统计文件中某个字符串出现的次数
1.使用grep linux grep命令在我的随笔linux分类里有过简单的介绍,这里就只简单的介绍下使用grep命令统计某个文件这某个字符串出现的次数,首先介绍grep命令的几个参数,详细参数请自 ...
- Java 中统计文件中出现单词的次数练习
统计英文article.txt文件中出现hello这个单词的次数 这个是article.txt文件内容 { hello The Royal Navy is trying hello to play h ...
- 【面试题总结】1、统计字符串中某个单词出现的次数(1-C++实现)
[解决方法一]C++ map解决 一.map中的find函数: 用于查找map中是否包含某个关键字条目,传入的参数是要查找的key,最后返回一个迭代器,如果没有找到,则返回的迭代器等于end()返回的 ...
- linux中统计文件中一个字符串出现的次数
要统计一个字符串出现的次数,这里现提供自己常用两种方法: 1. 使用vim统计 用vim打开目标文件,在命令模式下,输入 :%s/objStr//gn 2. 使用grep: grep -o objSt ...
- 软件工程-构建之法 WordCount小程序 统计文件中字符串个数,单词个数,词频,行数
一.前言 在之前写过一个词频统计的C语言课设,别人说你一个大三的怎么写C语言课程,我只想说我是先学习VB,VB是我编程语言的开始,然后接触到C语言及C++:再后来我是学习C++,然后反过来学习C语言, ...
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...
随机推荐
- 注意,WebDeploy服务会占用80端口。(Windows关闭了IIS,80端口任然被占用)
最近遇到一个很奇怪的事情,Windows上的 IIS 网站 全关掉了,80端口仍然被占用.然后我新装了一台服务器,一个一个组件地装,装一个测一次,最后发现,WebDeploy这个组件,会占用80端口. ...
- Grub启动配置文件
和许多其他linux发行版一样,Fedora使用Grub作为32位和64位X86系统的启动加载器(bootloader).grub的配置文件主要是/boot/grub/grub.conf,而/boot ...
- Sql开发技巧
原文:Sql开发技巧 简介 本文主要介绍下述几个技巧: 使用Row_Number分页 事务 根据条件刷选记录的技巧 分页 主要是使用了Row_Number()这个函数.一般如下: declare @P ...
- 使用jQuery热门功能实现
非常多站点上都有返回顶部的效果,本文阐述怎样使用jquery实现返回顶部button. 首先须要在顶部加入例如以下html元素: <p id="back-to-top"> ...
- 读书笔记—CLR via C#反射
前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...
- Oracle数据库面试题
1.取出表中第31到40行的记录 mysql方案:select * from t order by id limit 30,10 oracle方案: select t2.* from (select ...
- Linux根目录下文件说明
/bin:存放最常用命令: /boot:启动Linux的核心文件: /dev:设备文件: /etc:存放各种配置文件: /home:用户主目录: /lib:系统最基本的动态链接共享库: /mnt:一般 ...
- OpenGL Development Cookbook chapter7部分翻译
让我们通过以下简单步骤开始我们的配方: 1.通过读取外部的体数据文件,并通过该加载数据集数据转换成一个OpenGL纹理.也使硬件的mipmap生成.通常情况下,从使用一个横截面中获得的体积数据文件存储 ...
- 变易算法 - STL算法
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...
- 【欧拉计划4】Largest palindrome product
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1371281760.html 原创:[欧 ...