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个单 ...
随机推荐
- Codeforces 113A-Grammar Lessons(实现)
A. Grammar Lessons time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- IntelliJ IDEA —— Android开发的另一个选择
IntelliJ IDEA —— Android开发的另一个选择 很早就听过Eclipse的大名,但在我初学Java的时候,一位前辈推荐的IDE却是IntelliJ IDEA.因为之前用过JetBra ...
- DB2数据类型
DB2数据库的内置数据类型主要分成数值型(numeric).字符串型(character string).图形字符串(graphic string).二进制字符串型(binary string)或日期 ...
- POJ 2114 Boatherds 划分树
标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...
- C编程的指针涛 ---第九笔记
//这里说的是一个指针,指向算法的应用 //直接排序 //每个排序算法是指针指向的每个元件的特性的方便的交流 //这里的基本思想是,处理的记录的排序n - 1第二选择. //第i次操作选择i大(小)的 ...
- PCIe固态存储和HDD常见的硬盘性能对比测试
2周测试后,导致以下结果 MySQL-OLTP测试结果:(50表.每个表1000广域网数据,1000个线程) TPS:MySQL在PCIe固态存储上执行是在HDD上执行的5.63倍 writes:My ...
- 【POJ3037】Skiing 最短路
题意: 有个n*m的滑雪场,bessie要从(1,1)滑到(n,m),问最小时间. 起始有一个速度v,然后每从一个点A到一个点B(仅仅能上下左右走,每次一格),速度就会乘上2^(权值A-权值B). 然 ...
- java 转成字符串 json 数组和迭代
当你需要转成一串一串的json 排列 .当内容和遍历它们. 首页进口 net.sf.json.JSONArray和net.sf.json.JSONObject 两个jar 包 String str = ...
- Spring之单元测试
引言 是否在程序运行时使用单元测试是衡量一个程序员素质的一个重要指标.使用单元测试既可以让我检查程序逻辑的正确性还可以让我们减少程序测试的BUG,便于调试可以提高我们写程序的效率.以前我们做单元测试的 ...
- Mysql个人语句笔记
--一些简单语句记录: /*mysql*/SHOW DATABASES CREATE DATABASE guoDROP DATABASE guo /*查看创建的数据库*/SHOW CREATE DAT ...