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计算了,今天和大家分享下这个 ...
随机推荐
- CentOS 6用snmp配合MRTG显示系统状态
1. 软件安装以yum的方式安装snmp/MRTG 安装SNMP(要记得安装net-snmp-utils,不然snmpwalk将不能使用)yum -y install net-snmp* 安装MRTG ...
- NTOPNG,用来平时优化网站性能,用处大的
最近考察一下NTOPNG和NGX-REQ模块,看哪个对网站优化性能用户更大... 参考URL: http://www.68idc.cn/help/jiabenmake/qita/20150109164 ...
- H.264 Transform
变换是视频.图像编码的核心部分.目前所采用的变换算法都是从傅里叶变换演变而来.单纯的变换并不会导致视频(图像)的码率变小,反而会增大.但是非常巧妙的一点是:变换把图像从空域转换成的时域,把由色块组成的 ...
- 7.4.1 Dumping Data in SQL Format with mysqldump
7.4 Using mysqldump for Backups 使用mysqldump 用于备份: 7.4.1 Dumping Data in SQL Format with mysqldump 7. ...
- oracle number 和sqlserver numeric的区别
number如果不指定范围默认是可以输入所有位数的小数,numeric如果不指定小数默认是不允许输入小数
- C++ Template Specialization (模板特化)
个人理解这个东西说白了就是当模板类(或函数)的类型参数为某特定值时用对应的特化定义代之. 看个例子吧 #include <iostream> using namespace std; te ...
- 最受欢迎linux命令
1. 以 root 帐户执行上一条命令 sudo !! 2. 利用 Python 搭建一个简单的 Web 服务器,可通过 http://$HOSTNAME:8000访问 python -m ...
- js打开新的链接2
window.open打开新的连接时可能会被浏览器拦截掉. 所以采用动态创建a标签的形式. var a = document.createElement('a'); a.href = myUrl; ...
- OperationalError:(1054 - "Unknown column 'game.lable1' in 'field list' ")解决办法
今天白天遇到一个错误,第一次遇到这样的问题,数据库的问题,百度了很多答案也找了很多博客文章看 问题:OperationalError:(1054 - "Unknown column 'gam ...
- python_安装工具easy_install和pip
前言 用python就必须知道easy_install和pip这两个东西啦 easy_insall提供了在线一键安装模块或包的方便方式,而pip是easy_install的改进版,提供更好的提示信息, ...