通常情况下,我们需要用小数据集来单元测试我们写好的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. 转:php连接oracle设定字符集,避免乱码

    原文来自于:http://muyu.iteye.com/blog/399884 数据库用oracle,当php连接oracle的时候,最好指定字符集. 查PHP手册,oci_connect的第四个参数 ...

  2. poj 2892 &&hdu 1540 Tunnel Warfare

    http://poj.org/problem?id=2892 #include <cstdio> #include <cstring> #include <algorit ...

  3. 改进了UI的界面

    小峤提醒我,千万要记住, 调用JS之前,一定要先加载哟~~~ <!--[if lt IE 9]> <script src="/static/js/jquery1.11.1. ...

  4. Android 介绍spydroid每个包的大体功能

    看了接近一周的spydroid源代码,对spydroid这个开源项目有了一定的认识.也许有些理解不一定正确,给后来者一点启示.也是自己对rtsp协议,rtp协议的总结. 在windows下,如果安装了 ...

  5. 读取Word文档的标题

    一:描述,将读取的文档标题添加到下拉框中 二:代码 #region 方法:得到Word文档标题的内容 public static List<string> GetTitles(int j, ...

  6. python3、selenium、autoit3,通过flash控件上传文件

    autoit.au3 #include <Constants.au3> WinWait(); //暂停执行脚本,直到上传对话框出现 WinActive("打开") Wi ...

  7. Gas Station——LeetCode

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. yum nfs

    linux下3大文件共享方法 1.NFS NFS服务器配置 编辑/etc/exports,在文件中列出,要共享的目录.书写规则是:共享目录主机(参数).并且每条规则占据一行.例如: /mnt/mp3 ...

  9. Magician - hdu 5316 (区间查询合并)

    题意:有一个区间,然后有两种操作 1. 把a处的值改为b 0,查询区间ab的子序列的最大和,这个比较特殊,子序列里面相邻的数要有不同的奇偶性 ***************************** ...

  10. iOS利用Runtime自定义控制器POP手势动画

    前言 苹果在iOS 7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操 ...