WordCount程序代码解
package com.bigdata.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.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount {
/**
* 设置Map方法
* @author hxiuz
*
*/
private static class WCMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ private Text mapOutkey = new Text(); //设置输出key的格式
private final static IntWritable mapOutvalue = new IntWritable(1); //设置输出value的格式并赋值1
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException { //key即行偏移量
String input = value.toString(); //读入value数据
String[] inArr = input.split(" "); //按空格分割
for(String str:inArr) {
mapOutkey.set(str); //给key赋值
context.write(mapOutkey, mapOutvalue); //写入
}
}
} /**
* 设置Reduce方法
* @author hxiuz
*
*/
private static class WCReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable redOutvalue = new IntWritable(); @Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0; //计数变量
for(IntWritable value:values) {
sum += value.get(); //遍历集合values并将计数累加
} redOutvalue.set(sum); //给输出value赋值为sum
context.write(key, redOutvalue); //写入
}
} /**
* 主方法入口
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length!=2) {
System.out.println("Usage:wordcount <in> <out>");
return ;
}
Configuration conf = new Configuration(); //读取配置文件
try {
//新建一个job任务实例 并通过类设置jar
Job job = Job.getInstance(conf, WordCount.class.getSimpleName());
job.setJarByClass(WordCount.class); //设置输入路径
Path inputPath = new Path(args[0]);
FileInputFormat.addInputPath(job, inputPath); //设置map类
job.setMapperClass(WCMapper.class);
//设置map输出的格式
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); //设置reduce类
job.setReducerClass(WCReducer.class);
//设置reduce输出的格式
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //设置输出路径
Path outputPath = new Path(args[1]);
FileOutputFormat.setOutputPath(job, outputPath); //提交任务
boolean jobStatus = job.waitForCompletion(true); //判断程序是否正常退出
System.exit(jobStatus ? 0 : 1); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
WordCount程序代码解的更多相关文章
- 解决在windows的eclipse上面运行WordCount程序出现的一系列问题详解
一.简介 要在Windows下的 Eclipse上调试Hadoop2代码,所以我们在windows下的Eclipse配置hadoop-eclipse-plugin- 2.6.0.jar插件,并在运行H ...
- 大数据之路week07--day03(Hadoop深入理解,JAVA代码编写WordCount程序,以及扩展升级)
什么是MapReduce 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃. MapReduce方法则是: 1.给在座的所有玩家中分配这摞牌 2.让每个玩家数自己手中的牌有几 ...
- Bullet核心类介绍(Bullet 2.82 HelloWorld程序及其详解,附程序代码)
实验平台:win7,VS2010 先上结果截图: 文章最后附有生成该图的程序. 1. 刚体模拟原理 Bullet作为一个物理引擎,其任务就是刚体模拟(还有可变形体模拟).刚体模拟,就是要计算预测物体的 ...
- c语言—栈区,堆区,全局区,文字常量区,程序代码区 详解
转:http://www.cnblogs.com/xiaowenhui/p/4669684.html 一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分1.栈区(sta ...
- Eclipse环境搭建并且运行wordcount程序
一.安装Hadoop插件 1. 所需环境 hadoop2.0伪分布式环境平台正常运行 所需压缩包:eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz 在Linu ...
- Hadoop集群WordCount运行详解(转)
原文链接:Hadoop集群(第6期)_WordCount运行详解 1.MapReduce理论简介 1.1 MapReduce编程模型 MapReduce采用"分而治之"的思想,把对 ...
- (三)配置Hadoop1.2.1+eclipse(Juno版)开发环境,并运行WordCount程序
配置Hadoop1.2.1+eclipse(Juno版)开发环境,并运行WordCount程序 一. 需求部分 在ubuntu上用Eclipse IDE进行hadoop相关的开发,需要在Eclip ...
- 在Pycharm上编写WordCount程序
本篇博客将给大家介绍怎么在PyCharm上编写运行WordCount程序. 第一步 下载安装PyCharm 下载Pycharm PyCharm的下载地址(Linux版本).下载完成后你将得到一个名叫: ...
- 软件工程:Wordcount程序作业
由于时间的关系,急着交作业,加上这一次也不是那么很认真的去做,草草写了“Wordcount程序”几个功能,即是 .txt文件的读取,能计算出文件内容的单词数,文件内容的字符数,及行数. 这次选用C来做 ...
随机推荐
- PhpStorm如何下载github上的代码到本地
1.看着菜单栏有一个VCS(Virus Capture Scripter)集群服务器的选项,选择其下面的Checkout from Version Control,然后 (1)选择GIT:输入git的 ...
- ubuntu14.04 安装Jenkins
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - sudo sh -c 'ec ...
- FineUICore已发布,跨平台速度快(现在可申请试用)!
为什么选择ASP.NET Core 2.0?=================== 速度快,ASP.NET Core 的运行速度是 ASP.NET 4.6 的 6 - 23倍. 跨平台,可在Windo ...
- TCP/IP读书笔记(4) IPv4和IPv6 路由选择
TCP/IP读书笔记(4) IPv4和IPv6 路由选择 网络层是位于链路层之上,TCP/IP模型中网络层的核心协议是IP协议(Internet protocol). 目前主流的IP协议是IPv4(I ...
- Centos小白学习
目录 查看机器设备信息 有线网络设置(必须) 设置主机名(必须) 设置sudo用户,一般默认只有root可以使用(必须) yum更新源(必须) 安装完Centos后桌面宽度不能自适应 Centos安装 ...
- Android Scheme协议与应用全解析
URL Scheme 的作用 客户端应用可以向操作系统注册一个 URL Scheme,该 Scheme 用于从浏览器或其他应用中启动本应用. 通过指定的 URL 字段,可以让应用在被调起后直接打开某些 ...
- ADS1.2中RO base与RW base
ARM映像文件 ARM中的各种源文件(包括汇编文件,C语言程序及C++程序等)经过ARM编译器编译后生成ELF(Executable and linking format)格式的目标文件.这些目标文件 ...
- Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field
1 错误描述 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.s ...
- zTree实现地市县三级级联Service接口
zTree实现地市县三级级联Service接口 ProvinceService.java: /** * @Title:ProvinceService.java * @Package:com.gwtjs ...
- SCADA系统
简介编辑 在电力系统中,SCADA系统应用最为广泛,技术发展也最为成熟.它在远动系统中占重要地位,可以对现场 SCADA系统 的运行设备进行监视和控制,以实现数据采集.设备控制.测量.参数调节以及各类 ...