MapReduce UnitTest
通常情况下,我们需要用小数据集来单元测试我们写好的map函数和reduce函数。而一般我们可以使用Mockito框架来模拟OutputCollector对象(Hadoop版本号小于0.20.0)和Context对象(大于等于0.20.0)。
下面是一个简单的WordCount例子:(使用的是新API)
在开始之前,需要导入以下包:
1.Hadoop安装目录下和lib目录下的所有jar包。
2.JUnit4
3.Mockito
map函数:
- public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
 - );
 - private Text word = new Text();
 - @Override
 - protected void map(LongWritable key, Text value,Context context)
 - throws IOException, InterruptedException {
 - String line = value.toString(); // 该行的内容
 - String[] words = line.split(";"); // 解析该行的单词
 - for(String w : words) {
 - word.set(w);
 - context.write(word,one);
 - }
 - }
 - }
 
reduce函数:
- public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
 - @Override
 - protected void reduce(Text key, Iterable<IntWritable> values,Context context)
 - throws IOException, InterruptedException {
 - ;
 - Iterator<IntWritable> iterator = values.iterator(); // key相同的值集合
 - while(iterator.hasNext()) {
 - int one = iterator.next().get();
 - sum += one;
 - }
 - context.write(key, new IntWritable(sum));
 - }
 - }
 
测试代码类:
- public class WordCountMapperReducerTest {
 - @Test
 - public void processValidRecord() throws IOException, InterruptedException {
 - WordCountMapper mapper = new WordCountMapper();
 - Text value = new Text("hello");
 - org.apache.hadoop.mapreduce.Mapper.Context context = mock(Context.class);
 - mapper.map(null, value, context);
 - ));
 - }
 - @Test
 - public void processResult() throws IOException, InterruptedException {
 - WordCountReducer reducer = new WordCountReducer();
 - Text key = new Text("hello");
 - // {"hello",[1,1,2]}
 - ),new IntWritable(1),new IntWritable(2));
 - org.apache.hadoop.mapreduce.Reducer.Context context = mock(org.apache.hadoop.mapreduce.Reducer.Context.class);
 - reducer.reduce(key, values, context);
 - )); // {"hello",4}
 - }
 - }
 
具体就是给map函数传入一行数据-"hello"
map函数对数据进行处理,输出{"hello",0}
reduce函数接受map函数的输出数据,对相同key的值求和,并输出。
MapReduce UnitTest的更多相关文章
- Effective_Python mapreduce
		
完全吊炸天构造器的写法... import os import threading,time class GenericInputData(object): def read(self): raise ...
 - Mapreduce的文件和hbase共同输入
		
Mapreduce的文件和hbase共同输入 package duogemap; import java.io.IOException; import org.apache.hadoop.co ...
 - mapreduce多文件输出的两方法
		
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
 - mapreduce中一个map多个输入路径
		
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
 - Hadoop 中利用 mapreduce 读写 mysql 数据
		
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
 - [Hadoop in Action] 第5章 高阶MapReduce
		
链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter 1.链接MapReduce作业 [顺序链接MapReduce作业] mapreduce-1 | mapr ...
 - MapReduce
		
2016-12-21 16:53:49 mapred-default.xml mapreduce.input.fileinputformat.split.minsize 0 The minimum ...
 - python_单元测试unittest
		
Python自带一个单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一些校验返回的结果方法和一些用例执行前的初始化操作. 步骤1:首先引入unittest模块--import un ...
 - 使用mapreduce计算环比的实例
		
最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...
 
随机推荐
- 转:php连接oracle设定字符集,避免乱码
			
原文来自于:http://muyu.iteye.com/blog/399884 数据库用oracle,当php连接oracle的时候,最好指定字符集. 查PHP手册,oci_connect的第四个参数 ...
 - poj 2892 &&hdu 1540 Tunnel Warfare
			
http://poj.org/problem?id=2892 #include <cstdio> #include <cstring> #include <algorit ...
 - 改进了UI的界面
			
小峤提醒我,千万要记住, 调用JS之前,一定要先加载哟~~~ <!--[if lt IE 9]> <script src="/static/js/jquery1.11.1. ...
 - Android 介绍spydroid每个包的大体功能
			
看了接近一周的spydroid源代码,对spydroid这个开源项目有了一定的认识.也许有些理解不一定正确,给后来者一点启示.也是自己对rtsp协议,rtp协议的总结. 在windows下,如果安装了 ...
 - 读取Word文档的标题
			
一:描述,将读取的文档标题添加到下拉框中 二:代码 #region 方法:得到Word文档标题的内容 public static List<string> GetTitles(int j, ...
 - python3、selenium、autoit3,通过flash控件上传文件
			
autoit.au3 #include <Constants.au3> WinWait(); //暂停执行脚本,直到上传对话框出现 WinActive("打开") Wi ...
 - Gas Station——LeetCode
			
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
 - yum nfs
			
linux下3大文件共享方法 1.NFS NFS服务器配置 编辑/etc/exports,在文件中列出,要共享的目录.书写规则是:共享目录主机(参数).并且每条规则占据一行.例如: /mnt/mp3 ...
 - Magician - hdu 5316 (区间查询合并)
			
题意:有一个区间,然后有两种操作 1. 把a处的值改为b 0,查询区间ab的子序列的最大和,这个比较特殊,子序列里面相邻的数要有不同的奇偶性 ***************************** ...
 - iOS利用Runtime自定义控制器POP手势动画
			
前言 苹果在iOS 7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操 ...