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的更多相关文章

  1. hadoop第一个例子WordCount

    hadoop查看自己空间 http://127.0.0.1:50070/dfshealth.jsp import java.io.IOException; import java.util.Strin ...

  2. ElasticSearch 5学习(5)——第一个例子(很实用)

    想要知道ElasticSearch是如何使用的,最快的方式就是通过一个简单的例子,第一个例子将会包括基本概念如索引.搜索.和聚合等,需求是关于公司管理员工的一些业务. 员工文档索引 业务首先需要存储员 ...

  3. MXNet学习~第一个例子~跑MNIST

    反正基本上是给自己看的,直接贴写过注释后的代码,可能有的地方理解不对,你多担待,看到了也提出来(基本上对未来的自己说的),三层跑到了97%,毕竟是第一个例子,主要就是用来理解MXNet怎么使用. #导 ...

  4. emberjs学习一(环境和第一个例子)

    code { margin: 0; padding: 0; white-space: pre; border: none; background: transparent; } code, pre t ...

  5. Qt之QCustomPlot绘图(一)配置和第一个例子

    最近一个用Qt开发的项目需要绘制坐标曲线,我在老师的指点下使用了QCustomPlot这个插件,使用方法简单,功能还算不错. 可是在网上找了很多资料和博文都只是将官方提供的例子演示一遍,没有系统全面的 ...

  6. 键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试

    键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试 完整源代码 /// /// @file ctrl2cap.c /// @author wowocock /// @date 2009-1 ...

  7. springmvc的介绍和第一个例子

    SpringMVC是Spring 框架自带的一部分. SpringMVC底层基于:Servlet Struts2底层基于:filter struts1底层基于:Servlet spring 各模块 我 ...

  8. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  9. Informatica_(2)第一个例子

    PowerCenter Repository Manager1.启动客户端程序连接服务器打开客户端(PowerCenter Repository Manager)PCRM;存储库--配置域--添加新域 ...

随机推荐

  1. sql中索引不会被用到的几种情况

    1.查询谓词没有使用索引的主要边界,换句话说就是select *,可能会导致不走索引. 比如,你查询的是SELECT * FROM T WHERE Y=XXX;假如你的T表上有一个包含Y值的组合索引, ...

  2. 【Android】解析Paint类中MaskFilter的使用

    目录结构: contents structure [+] EmbossMaskFilter BlurMaskFilter MaskFilter可以用来指定画笔的边缘效果.如果引用开启硬件加速的话,那么 ...

  3. Django Web开发学习笔记(3)

    1.创建一个简单视图 这章是按照DgangoBook的说明.在我们创建的工程目录下面DjangoE_1(这是我为自己的工程命名的名字)新建一个view.py的文件,并在该文件下添加如下代码 from ...

  4. ListView中的Item点击事件和子控件的冲突或者item点击没有反应的解决的方法

    fragment中加入了button和checkbox这些控件.此时这些子控件会将焦点获取到.所以经常当点击item时变化的是子控件.item本身的点击没有响应. 这时候就能够使用descendant ...

  5. 基础006_pg109_IP-Xfft

    作者:桂. 时间:2018-05-09  07:20:48 链接:http://www.cnblogs.com/xingshansi/p/9012232.html 前言 简要记录xilinx FFT的 ...

  6. gSOAP 初体验

    安装 由于本人使用的是 Mac OS 系统,故以 Mac OS 为例说明如何安装 gSOAP. 1)下载 gSOAP 可以在 https://sourceforge.net/projects/gsoa ...

  7. elasticsearch client 为空 错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecut‌​or()Ljava/util/concu‌​rrent/Executor

    错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecut‌​or() ...

  8. Java查找出现的单词

    如何找到一个单词的每个出现? 解决方法 下面的例子演示了如何使用Pattern.compile()方法和m.group()方法找到一个词出现次数. import java.util.regex.Mat ...

  9. mysql语法 -- concat函数

    mysql CONCAT(str1,str2,…)                        返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL.或许有一个或多个参 ...

  10. Spark学习笔记——泰坦尼克生还预测

    package kaggle import org.apache.spark.SparkContext import org.apache.spark.SparkConf import org.apa ...