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计算了,今天和大家分享下这个 ...
随机推荐
- [Android] hid设备按键流程简述
hexdump /dev/hidraw0就能看到usbhid设备传输过来的裸流 如:按下Input键 003ae60 0000 0096 8000 006b 0000 0000 0000 0000 * ...
- Spring 3.2 ClassMetadataReadingVisitor 错误
nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.core.type.clas ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- SAX PULL解析实例
XML三种解析方式: SAX解析:基于事件驱动,事件机制基于回调函数的,得到节点和节点之间内容时也会回调事件 PULL解析:相同基于事件驱动,仅仅只是回调时是常量 DOM解析:是先把XML文件装入内存 ...
- 日积月累:ProguardGui进行jar包代码混淆
前面文章<Proguard进行源代码混淆>讲解过怎么使用Proguard工具对Android的源代码进行混淆的方法(感兴趣的朋友可以访问:http://blog.csdn.net/p106 ...
- boost 特点
功能强大 跨平台 开源 免费 构造精巧 C++扩展库
- lubuntu12.11 source.list
deb http://ports.ubuntu.com/ubuntu-ports/ precise main universe deb-src http://ports.ubuntu.com/ubun ...
- js类封装
将js方法封装成类,好处就是团队开发中避免命名冲突,部分类整理代码如下: function LocalStorageHelper() { //检测浏览器是否支持localStorage this.ch ...
- IPC进程间通信 - AIDL+Binder
原理 http://www.linuxidc.com/Linux/2012-07/66195.htm 服务端,客户端处在用户空间,而binder驱动处在内核空间. 服务器端.一个Binder服 ...
- css中的垂直居中方法
单行文字 (外行高度固定) line-height 行高, 将line-height值与外部标签盒子的高度值设置成一致就可以了. height:3em; line-height:3em; 多行文字 图 ...