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个单 ...
随机推荐
- jQuery.extend()源码解读
// extend方法为jQuery对象和init对象的prototype扩展方法// 同时具有独立的扩展普通对象的功能jQuery.extend = jQuery.fn.extend = funct ...
- IOS发展--他们控制的定义
有没有这样的要求,,自定义panel,里面放几个控件,在多个页面中使用此panel. 有三个观点来解决这个问题: 1.自己继承UIView写一个类,在它是在代码的形式加入需要控制.完成布局. 2.使用 ...
- STL源代码分析——STL算法sort排序算法
前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...
- MySQL安装指南
近期领导突然说要用MySQL,我立刻当天晚上就研究了一下. http://www.mysql.com/这是官网,还好能够訪问.好多年前已经被oracle收购.分为企业版和社区版: MySQL Ente ...
- Arraylist、Linkedlist遍历方式性能分析
本文主要介绍ArrayList和LinkedList这两种list的常用循环遍历方式,各种方式的性能分析.熟悉java的知道,常用的list的遍历方式有以下几种: 1.for-each List< ...
- 安装Windows7操作系统 - 初学者系列 - 学习者系列文章
Windows7是一款不错的操作系统,不过它的销量远没有XP那么大,但是不失为一款好的操作系统.下面就对Windows7操作系统的安装做下介绍. 1. 同样的,BIOS里设置光驱启动,把操作系统盘装 ...
- leetcode[87] Partition List
题目:给定一个链表和一个数x,将链表中比x小的放在前面,其他的放在后头.例如: Given 1->4->3->2->5->2 and x = 3,return 1-> ...
- Python装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...
- javascript Function.prototype.bind
语法: fn.bind(obj,arg1,arg2,arg3...) bind是es5新增的方法,顾名思义,它的作用是将函数绑定到某个对象上,就像是某个对象调用方法一样.其本质还是改变了该函数的上下文 ...
- beanutils中类型转换
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Servl ...