Mapper

// map的数量与数的分片有关系
public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{ protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] words = StringUtils.split(line, " ");
for (String word : words) {
context.write(new Text(word), new LongWritable(1));
}
}
}

  

Reducer 

public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable> {

	@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException {
long count = 0;
for (LongWritable l : values) {
count ++;
}
context.write(key, new LongWritable(count));
}
}

Runner

public class WCRunner {

	public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(WCRunner.class); job.setMapperClass(WCMapper.class);
job.setReducerClass(WCReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
// 设置reduce的数量,对应的会生成设置数量的文件,每个文件的内容是根据
// job.setPartitionerClass(HashPartitioner.class);中的Partitioner确定 job.setNumReduceTasks(10);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

  

public class WCRunner2 extends Configured implements Tool{

	public int run(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(WCRunner2.class); job.setMapperClass(WCMapper.class);
job.setReducerClass(WCReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) throws Exception {
ToolRunner.run(new WCRunner2(), args);
} }

执行:  hadoop jar wc.jar com.easytrack.hadoop.mr.WCRunner2  /wordcount.txt /wc/output4

hadoop wordcount的更多相关文章

  1. Eclipse执行Hadoop WordCount

    前期工作 我的Eclipse是安装在Windows下的,通过Eclipse执行程序连接Hadoop, 需要让虚拟机的访问地址和本机的访问地址保持在同一域内,虚拟机的地址更改前面的文章介绍过了,如果想改 ...

  2. Hadoop wordcount Demon

    搭建完成Hadoop后,第一个demon,wordcount.此处参考:http://blog.csdn.net/wangjia55/article/details/53160679 wordcoun ...

  3. Hadoop WordCount程序

    一.把所有Hadoop的依赖jar包导入buildpath,不用一个一个调,都导一遍就可以,因为是一个工程,所以覆盖是没有问题的 二.写wordcount程序 1.工程目录结构如下: 2.写mappe ...

  4. Hadoop WordCount单词计数原理

    计算文件中出现每个单词的频数 输入结果按照字母顺序进行排序 编写WordCount.java 包含Mapper类和Reducer类 编译WordCount.java javac -classpath ...

  5. hadoop wordcount程序缺陷

    在wordcount 程序的main函数中,没有读取运行环境中的各种参数的值,全靠hadoop系统的默认参数跑起来,这样做是有风险的,最突出的就是OOM错误. 自己在刚刚学习hadoop编程时,就是模 ...

  6. Hadoop - WordCount代码示例

    文章来源:http://www.itnose.net/detail/6197823.html import java.io.IOException; import java.util.Iterator ...

  7. hadoop WordCount例子详解。

    [学习笔记] 下载hadoop-2.7.4-src.tar.gz,拷贝hadoop-2.7.4-src.tar.gz中hadoop-mapreduce-project\hadoop-mapreduce ...

  8. hadoop安装与WordCount例子

    1.JDK安装 下载网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html  ...

  9. hadoop的wordcount例子运行

    可以通过一个简单的例子来说明MapReduce到底是什么: 我们要统计一个大文件中的各个单词出现的次数.由于文件太大.我们把这个文件切分成如果小文件,然后安排多个人去统计.这个过程就是”Map”.然后 ...

随机推荐

  1. visual studio 中使用的插件介绍

    Highlight all occurrences of selected word 高亮代码 Indent Guides 代码的开头结尾连接竖线..是代码更清洗 PHP Tools for visu ...

  2. XML解析器(转)

    常见C/C++ XML解析器有tinyxml.XERCES.squashxml.xmlite.pugxml.libxml等等,这些解析器有些是支持多语言的,有些只是单纯C/C++的.如果你是第一次接触 ...

  3. wordpress the_date 方法 偶尔为空的问题

    估计很多人遇到这个问题: 一来是the_title(),the_permalink(),the_date()一路用下来,很正常也很正确 不爱读官方文档,因为文档中有个特别提示 文档:http://co ...

  4. memcached学习笔记5--socke操作memcached 缓存系统

    使用条件:当我们没有权限或者不能使用服务器的时候,我们需要用socket操作memcached memcached-client操作 特点: 无需开启memcache扩展 使用fsocketopen( ...

  5. Oracle数据库--SQL函数

    Oracle SQL函数  1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii( ...

  6. 基于LR的HTTP协议接口性能测试脚本实例

    背景介绍 XXX项目性能测试中新增业务场景:XX设备的在线激活,因为存在多用户同时在线激活,故需进行性能测试以确认后台服务器系统在多用并发时功能是否正常,性能指标是否满足规格要求.用户使用场景为用户通 ...

  7. MySQL出现大量unauthenticated user的问题

    发现这算属MySQL的一个bug,不管连接是通过hosts还是ip的方式,MySQL都会对DNS做反查,IP到DNS,由于反查的接续速度过 慢(不管是不是isp提供的dns服务器的问题或者其他原因), ...

  8. wdate-year-month-week-gategory-amount-coin

    ---2016-12-02 19:46:39 the whole table DISTINCT field SUM(field) COUNT(field) --- 888983 rows OK SEL ...

  9. CSS子元素margin-top对于父元素的影响

    父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. html代码如下 <styl ...

  10. hibernate多SessionFactory配置

    <bean id="aDataSource" class="org.apache.commons.dbcp.BasicDataSource" destro ...