通常情况下,我们需要用小数据集来单元测试我们写好的map函数和reduce函数。而一般我们可以使用Mockito框架来模拟OutputCollector对象(Hadoop版本号小于0.20.0)和Context对象(大于等于0.20.0)。

下面是一个简单的WordCount例子:(使用的是新API)

在开始之前,需要导入以下包:

1.Hadoop安装目录下和lib目录下的所有jar包。

2.JUnit4

3.Mockito

map函数:

  1. public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
  2. );
  3. private Text word = new Text();
  4. @Override
  5. protected void map(LongWritable key, Text value,Context context)
  6. throws IOException, InterruptedException {
  7. String line = value.toString();     // 该行的内容
  8. String[] words = line.split(";");   // 解析该行的单词
  9. for(String w : words) {
  10. word.set(w);
  11. context.write(word,one);
  12. }
  13. }
  14. }

reduce函数:

  1. public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  2. @Override
  3. protected void reduce(Text key, Iterable<IntWritable> values,Context context)
  4. throws IOException, InterruptedException {
  5. ;
  6. Iterator<IntWritable> iterator = values.iterator();       // key相同的值集合
  7. while(iterator.hasNext()) {
  8. int one = iterator.next().get();
  9. sum += one;
  10. }
  11. context.write(key, new IntWritable(sum));
  12. }
  13. }

测试代码类:

  1. public class WordCountMapperReducerTest {
  2. @Test
  3. public void processValidRecord() throws IOException, InterruptedException {
  4. WordCountMapper mapper = new WordCountMapper();
  5. Text value = new Text("hello");
  6. org.apache.hadoop.mapreduce.Mapper.Context context = mock(Context.class);
  7. mapper.map(null, value, context);
  8. ));
  9. }
  10. @Test
  11. public void processResult() throws IOException, InterruptedException {
  12. WordCountReducer reducer = new WordCountReducer();
  13. Text key = new Text("hello");
  14. // {"hello",[1,1,2]}
  15. ),new IntWritable(1),new IntWritable(2));
  16. org.apache.hadoop.mapreduce.Reducer.Context context = mock(org.apache.hadoop.mapreduce.Reducer.Context.class);
  17. reducer.reduce(key, values, context);
  18. ));     // {"hello",4}
  19. }
  20. }

具体就是给map函数传入一行数据-"hello"

map函数对数据进行处理,输出{"hello",0}

reduce函数接受map函数的输出数据,对相同key的值求和,并输出。

MapReduce UnitTest的更多相关文章

  1. Effective_Python mapreduce

    完全吊炸天构造器的写法... import os import threading,time class GenericInputData(object): def read(self): raise ...

  2. Mapreduce的文件和hbase共同输入

    Mapreduce的文件和hbase共同输入 package duogemap;   import java.io.IOException;   import org.apache.hadoop.co ...

  3. mapreduce多文件输出的两方法

    mapreduce多文件输出的两方法   package duogemap;   import java.io.IOException;   import org.apache.hadoop.conf ...

  4. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  5. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  6. [Hadoop in Action] 第5章 高阶MapReduce

    链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter   1.链接MapReduce作业   [顺序链接MapReduce作业]   mapreduce-1 | mapr ...

  7. MapReduce

    2016-12-21  16:53:49 mapred-default.xml mapreduce.input.fileinputformat.split.minsize 0 The minimum ...

  8. python_单元测试unittest

    Python自带一个单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一些校验返回的结果方法和一些用例执行前的初始化操作. 步骤1:首先引入unittest模块--import un ...

  9. 使用mapreduce计算环比的实例

    最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...

随机推荐

  1. 测试和恢复性的争论:面向对象vs.函数式编程

    Michael Feathers最近的博文在博客社区引发了一场异常激烈的论战.Feathers发表言论说一些面向对象编程语言的内嵌特性有助于测试的进行,并且使用面向对象编程语言编写的代码更容易恢复. ...

  2. windows10 预览版 中英文官方下载地址+激活密钥+网盘分享

    windows10 预览版 中英文官方下载地址+激活密钥+网盘分享 产品密钥:NKJFK-GPHP7-G8C3J-P6JXR-HQRJR 英语 64 位 (x64)  http://iso.esd.m ...

  3. ExtJS 4 MVC架构讲解

    大规模客户端应用通常不好实现不好组织也不好维护,因为功能和人力的不断增加,这些应用的规模很快就会超出掌控能力,ExtJS 4 带来了一个新的应用架构,不但可以组织代码,还可以减少实现的内容新的应用架构 ...

  4. 编写SQL

    create table student ( sno char(9) primary key, sname char(20) unique, ssex char(2), sage smallint, ...

  5. Ubuntu14.04下安装ns2.35

    我选择的版本是2.35最新版本,安装环境是Ubuntu 14.04. 1.下载ns2的安装包,这里我选择的是ns-allinone-2.35.tar.gz压缩格式的all in one安装包,all ...

  6. "解密"微信开放高级接口 企业如何应对

    今天(2013年10月29日)腾讯终于对外公开了微信公众平台最新的接口,一石激起千层浪,对于很多微信公众平台的运营人员来说,今天是令人兴奋的一天!微信在向申请服务号的企业开发了大量接口.用户不想输入文 ...

  7. Android中自定义Activity和Dialog的位置大小背景和透明度等

    1.自定义Activity显示样式 先在res/values下建colors.xml文件,写入: view plainprint? 1. <?xml version="1.0" ...

  8. Linux查看端口信息命令

    netstat -tlnp|grep 端口 eg: netstat -tlnp|grep 9889

  9. intellij安装Scala及Python插件

    1.下载intellij及Scala和Python插件 intellij的下载地址:https://www.jetbrains.com/idea/download/#section=windows S ...

  10. Android(java)学习笔记255:JNI之JNI概念

    1. JNI是什么? java native interface (java本机接口) 比如方法声明: public final native Class<?>  getClass(): ...