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计算了,今天和大家分享下这个 ...
随机推荐
- how to count uv area
先放着,空了再整理.... fn getModeUvVolumetric mode chang= ----得到UV使用率( --global facesNumSum = meshop.getnumfa ...
- cf B. I.O.U.
http://codeforces.com/contest/376/problem/B #include <cstdio> #include <cstring> #includ ...
- 关于如何在C语言中嵌入汇编命令
转载自:http://www.keil.com/support/docs/2308.htm C51: GETTING INLINE ASSEMBLY TO WORK Information in th ...
- 利用Keil u4调试,精确实现软件延时
用定时器延时,有时候显得有点麻烦,我们不如考虑软件精确延时,软件延时无非就是利用for或while多重循环.以前用到延时函数时,都是从网上下载别人写好的延时子程序.延时5ms,400ms,1s,……, ...
- ehci符合USB2.0,uhci,ohci,
uhci ohci ehci他们都是主机控制器的规格,OHCI主要为非PC系统上以及带有SiShe ALi芯片组的 PC主板上的USB芯片,UHCI大多为Intel和Via主板上的USB控制器 ...
- 修改CKFinder上传路径
一.CKFinder的若干问题 1.单独使用 ckfinder从原fckeditor分离出来以后可以单独使用,通常我习惯于在工具栏中添加ckfinder.dll,这样以后要使用ckfinder直接从工 ...
- 【转】在Eclipse环境下配置Servlet开发环境
配置这个真心坑···浪费我一下午时间,而且去网上找了一圈资料发现都不靠谱呀= = 于是自己写一个,防止忘记了 一.配置前准备 你需要下载三个东西 Eclipse J2EE:http://www.ecl ...
- JS能力测评试题
找出元素 item 在给定数组 arr 中的位置 function indexOf(arr, item) { return arr.indexOf(item); } function indexOf( ...
- String Format for DateTime [C#]
This example shows how to format DateTime using String.Format method. All formatting can be done als ...
- [Javascript] JavaScript Array Methods in Depth - push
Array push is used to add elements to the end of an Array. In this lesson we'll see how the push met ...