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.support.design.widget.AppBarLayout 在android5.0+底部显示空白条问题
在最外层使用 RelativeLayout作为根节点,同时设置 android:fitsSystemWindows="true"问题解决. <?xml version=&qu ...
- @action 注解
================================================= 下载 注解配置 private String fileName; private String co ...
- mysql 主从 配置和同步管理
首先呢,需要有两个mysql服务器.如果做测试的话可以在同一台机器上装两个mysql服务程序,注意要两个运行程序的端口不能一样.我用的是一个是默认的3306,从服务器用的是3307端口. 在主服务创建 ...
- 【HDOJ】2424 Gary's Calculator
大数乘法加法,直接java A了. import java.util.Scanner; import java.math.BigInteger; public class Main { public ...
- (2015年郑州轻工业学院ACM校赛题)I 旋转图像
矩阵旋转,写一个转 90° 的函数就行了, 注意每次要将 长和宽的值进行交换就行了. #include<stdio.h> #include<iostream> #include ...
- Oracle执行计划——使用index full scan的几种情况
常见有三种情况都有用到indexfull scan. 1. 查询列就是索引列 2. 对索引列进行order by时 3. 对索列进行聚合计算时
- Managing Hierarchical Data in MySQL
Managing Hierarchical Data in MySQL Introduction Most users at one time or another have dealt with h ...
- jquery animate
$(".logo").animate( { opacity: .25, //将不透明度逐渐变成.25 height: 0 //高度逐渐变成0 }, { duration: 1000 ...
- hdu 4540 威威猫系列故事——打地鼠 dp小水题
威威猫系列故事——打地鼠 Time Limit: 300/100 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...
- 一个非常有趣的算法程序(有趣只针对程序猿)就是Josephus问题
大概花了一个晚上搭一个中午的时间,完善了一个关于Josephus的程序,这个Josephus游戏可是非常经典的算法,作为一个想从事软件的人最好能够理解一下,毕竟这个计算机教材上也讲过类似题目,具体的关 ...