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. .NET开发问题汇总

    问题一: mvc 部署到iis 提示错误未能加载文件或程序集System.Web.Http.WebHost 解决方案: Nuget程序包管理 —>程序包管理控制台,运行以下命令即可:Update ...

  2. 被spring和hibernate4逼疯

    spring3.1整合hibernate4,事务都配置上了的,但getCurrentSession()仍然获得不到 以下是各配置 web.xml ? 1 2 3 4 5 6 7 8 9 10 11 1 ...

  3. sqlite相关工具使用

    sqlite3可视化工具 1.sudo apt-get install sqlitebrowser 2.sudo apt-get install sqliteman3.sqlitestudio需要去官 ...

  4. GDC2016 【巫师3 狂猎】的游戏事件工作流

    巫师3 狂猎(The Witcher 3: Wild Hunt )的游戏事件工作流   http://game.watch.impress.co.jp/docs/news/20160320_74916 ...

  5. Asp.net 服务器Application,Session,Cookie,ViewState和Cache区别

    2.8 Context 的使用Context 对象包含与当前页面相关的信息,提供对整个上下文的访问,包括请求.响应.以及上文中的Session 和Application 等信息.可以使用此对象在网页之 ...

  6. thinkphp的save方法失败

    如果用下面的方式更新数据时, $data['link_phone'] = I('post.link_phone'); $flag1 = $order->save ($data); $data一定 ...

  7. ArcGIS Server 缓存服务增加新比例尺缓存

    win10 + Server 10.4 +  ArcMap 10.4  操作简单说明: ①窗口上方Customize栏→Toolbars→ Customize→ 搜索到 manege map serv ...

  8. 【转】 使用Redis的Pub/Sub来实现类似于JMS的消息持久化

    http://blog.csdn.net/canot/article/details/52040415 关于个人对Redis提供的Pub/Sub机制的认识在上一篇博客中涉及到了,也提到了关于如何避免R ...

  9. C# .Net实现URL绝对路径和相对路径之间互相转换

    网站制作开发中,URL的绝对路径和相对路径之间互相转换,是经常需要用到的.以下是在C#.Net下一种实现二者互相转化的方法: [DllImport("shlwapi.dll", C ...

  10. linux OSlab4 添加自定义系统调用

    http://blog.csdn.net/ly01kongjian/article/details/8947285 http://www.cnblogs.com/hoys/archive/2011/0 ...