通常情况下,我们需要用小数据集来单元测试我们写好的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. facebook分块加载,页面优化,BigPipe,简单实例

    <!DOCTYPE html><html><head><meta charset=”utf-8″><title>BigPipe Demo 3 ...

  2. 给 chorme Developer Tool F12 开发者工具.加入更酷的代码着色

    地址:https://github.com/mauricecruz/chrome-devtools-zerodarkmatrix-theme 默认样式 替换目录为: mac ~/Library/App ...

  3. (2015年郑州轻工业学院ACM校赛题)I 旋转图像

    矩阵旋转,写一个转 90° 的函数就行了, 注意每次要将 长和宽的值进行交换就行了. #include<stdio.h> #include<iostream> #include ...

  4. 用Visual Studio创建集成了gtest的命令行工程

    gtest代码库中的sample代码 在gtest的代码库中,包含了10个sample的代码,覆盖了gtest的常见用法,sample的代码位于以下文件夹: gtest\samples 由于gtest ...

  5. DLL入门浅析(2)——如何使用DLL

    转载自:http://www.cppblog.com/suiaiguo/archive/2009/07/20/90621.html 上文我简单的介绍了如何建立一个简单DLL,下面再我简单的介绍一下如何 ...

  6. UVA 327 -Evaluating Simple C Expressions(栈)

    Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C expre ...

  7. M - Escape - HDU 3605 - (缩点+最大流SAP)

    题目大意:2012世界末日来了,科学家发现了一些星球可以转移人口,不过有的人可以在一些星球上生存有的人不行,而且每个星球都有一定的承载量,现在想知道是否所有的人都可以安全转移呢? 输入:首先输入一个N ...

  8. 使用javascript判断浏览器类型

    之前在项目中遇到过要针对不同浏览器做不同的一些js或者css操作,后来某个朋友也突然问到这个问题,所以,整理了一下,在这里留个笔记,方便以后使用. 使用javascript判断浏览器类型: funct ...

  9. mvc系统过滤器

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  10. maven + selenium + jenkins 教程收集

    maven + selenium + jenkins 教程收集 Complete Guide for Selenium integration with jenkins Maven http://le ...