mapReducer第一个例子WordCount
mapreducer第一个例子,主要是统计一个目录下各个文件中各个单词出现的次数。
mapper
package com.mapreduce.wordCount; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; /*
* TextInputFormat中的recorder 每次读取 一个分片中的 一行文本
* 所以map 函数每次读取一行。规定:
* 输入:key: 行偏移量 value:一行的文本
* 输出: key: 一个词 value: 1
*
* map 做个映射。
*/ public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ Text keyOut = new Text();
IntWritable valueOut = new IntWritable(); protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { String line = value.toString();
String[] worlds = line.split(" ");
for( String w:worlds){
keyOut.set(w);
valueOut.set(1);
context.write(keyOut,valueOut);
}
} }
reudcer
package com.mapreduce.wordCount; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
/*
* 输入: 对应maper 的输出 [key: values] {"love":[1,1,1,1,1,1]}
* 输出: 词和每个词的出现次数。
* 中间shuffle 阶段自动排序分区。 因为没有分区,所以输出到一个文件中 // 所以结果文件是按 key 排序的。
*
*/
public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable>{ protected void reduce(Text key, Iterable<IntWritable> value,Context context)
throws IOException, InterruptedException {
int count = ;
for( IntWritable v:value){
count += v.get();
}
context.write(key, new IntWritable(count)); }
}
job 驱动
package com.mapreduce.wordCount; 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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class WordCountDemo { public static void main(String[] args) throws Exception { // 1 获取configuration
Configuration configuration = new Configuration(); // 2 job Job job = Job.getInstance(configuration); // 3 作业jar包 job.setJarByClass(WordCountDemo.class); // 4 map, reduce jar 包
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordReducer.class);
// 5 map 输出类型 job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); // 6 最终 输出类型 (reducer) job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); // 7 inputformatclass , outputformatclass 输入输出入文件类型 可能决定分片信息 job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); // 8 输入输出文件路径 FileInputFormat.setInputPaths(job, new Path("d:/input"));
FileOutputFormat.setOutputPath(job, new Path("d:/output")); // 9 job提交 job.waitForCompletion(true); } }
mapReducer第一个例子WordCount的更多相关文章
- hadoop第一个例子WordCount
hadoop查看自己空间 http://127.0.0.1:50070/dfshealth.jsp import java.io.IOException; import java.util.Strin ...
- ElasticSearch 5学习(5)——第一个例子(很实用)
想要知道ElasticSearch是如何使用的,最快的方式就是通过一个简单的例子,第一个例子将会包括基本概念如索引.搜索.和聚合等,需求是关于公司管理员工的一些业务. 员工文档索引 业务首先需要存储员 ...
- MXNet学习~第一个例子~跑MNIST
反正基本上是给自己看的,直接贴写过注释后的代码,可能有的地方理解不对,你多担待,看到了也提出来(基本上对未来的自己说的),三层跑到了97%,毕竟是第一个例子,主要就是用来理解MXNet怎么使用. #导 ...
- emberjs学习一(环境和第一个例子)
code { margin: 0; padding: 0; white-space: pre; border: none; background: transparent; } code, pre t ...
- Qt之QCustomPlot绘图(一)配置和第一个例子
最近一个用Qt开发的项目需要绘制坐标曲线,我在老师的指点下使用了QCustomPlot这个插件,使用方法简单,功能还算不错. 可是在网上找了很多资料和博文都只是将官方提供的例子演示一遍,没有系统全面的 ...
- 键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试
键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试 完整源代码 /// /// @file ctrl2cap.c /// @author wowocock /// @date 2009-1 ...
- springmvc的介绍和第一个例子
SpringMVC是Spring 框架自带的一部分. SpringMVC底层基于:Servlet Struts2底层基于:filter struts1底层基于:Servlet spring 各模块 我 ...
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2 spring-mv ...
- Informatica_(2)第一个例子
PowerCenter Repository Manager1.启动客户端程序连接服务器打开客户端(PowerCenter Repository Manager)PCRM;存储库--配置域--添加新域 ...
随机推荐
- 【Visual Studio】VS发布应用未能创建默认证书的问题解决方法
解决方法:点击你创建的项目 右键> 属性>签名>从存储区选择>选择证书这时候显示无可用证书 ,然后我从文件区选择了一个结果,又出现了第二个问题.提示我“签名时出错: 指定了无效 ...
- goland激活码
http://idea.youbbs.org
- TypeError: sequence item 0: expected string, Tag found
原始代码: soup = BeautifulSoup(result, 'html.parser') content_list = soup.find_all('p', attrs={"cla ...
- supervisor详解
1.什么是supervisor supervisor是用python写的一个进程管理工具,用来启动,重启,关闭进程. 2.supervisor的安装 pip install supervisor 3. ...
- linux每日命令(19):locate 命令
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...
- spring中定时任务quartz2.2.3
定时任务几种实现方式 Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务,没怎么用过就不说了.Spring3.0以后自带的task,可以将它 ...
- 【九天教您南方cass 9.1】 10 DTM土方计算的四种方法
同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取测量空间中. [点击索取cass教程]5元立得 (给客服说暗号:“ ...
- 配合angularjs中interceptor一劳永逸的加载$ionicloading的方法
在我们日常的项目开发中,每当页面需要和服务端存在交互的时候,为了界面的友好,我们都会在界面中给个loading的加载图标,当从服务端获取到数据或者已经把本地数据送到服务端并且得到相应的回应的时候我们就 ...
- 解决SQLite异常:library routine called out of sequence
在项目开发中,使用SQLite一不小心就会碰到各种DB异常,网上搜了下没有这方面的资料,写出来记录下. 异常信息:android.database.sqlite.SQLiteMisuseExcepti ...
- php框架之odp(一)
熟悉odp框架的使用已经快一个月了,今天总结一下.odp和yii都是成熟的常用的php框架,因为odp是公司内部开发的,且公司内部用odp的较多,所以我就从odp框架入手.仅仅纪录自己的学习体会 一. ...