mrunit测试

Map/Reduce 单元测试

About MRUnit

world count 测试

package com.irwin.hadoop;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.TaskAttemptID;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl;
import org.apache.hadoop.mrunit.mapreduce.MapDriver;
import org.apache.hadoop.mrunit.mapreduce.MapReduceDriver;
import org.apache.hadoop.mrunit.mapreduce.ReduceDriver;
import org.apache.hadoop.util.ReflectionUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.irwin.hadoop.WordCount.TokenizerMapper;
import com.irwin.hadoop.WordCount.IntSumReducer;


public class WordCountTest extends Assert{

	private MapDriver<Object, Text, Text, IntWritable> mapDriver;
	private ReduceDriver<Text, IntWritable, Text, IntWritable> reduceDriver;
	private static final IntWritable one = new IntWritable(1);
	MapReduceDriver<Object, Text, Text, IntWritable, Text, IntWritable> mapReduceDriver;
	
	@Before
	public void setUp(){
		Mapper<Object, Text, Text, IntWritable> mapper = new TokenizerMapper();
		mapDriver = MapDriver.newMapDriver(mapper);
		Reducer<Text, IntWritable, Text, IntWritable> reducer = new IntSumReducer();
		reduceDriver = ReduceDriver.newReduceDriver(reducer);
		mapReduceDriver = MapReduceDriver.newMapReduceDriver(mapper, reducer);
	}
	
	@Test
	public void testReducer() throws Exception{
		List<IntWritable> values = new ArrayList<IntWritable>();
	    values.add(new IntWritable(1));
	    values.add(new IntWritable(1));
	    reduceDriver.withInput(new Text("hello"), values);
	    reduceDriver.withOutput(new Text("hello"), new IntWritable(2));
	    reduceDriver.runTest();
	}
	
	  @Test
	  public void testMapReduce() throws IOException {
	    mapReduceDriver.withInput(new LongWritable(1), new Text("cat cat dog"));
	    mapReduceDriver.addOutput(new Text("cat"), new IntWritable(2));
	    mapReduceDriver.addOutput(new Text("dog"), new IntWritable(1));
	    mapReduceDriver.runTest();
	  }
	
	@Test
	public void testMapper() throws Exception {
		LineRecordReader reader = getBytesRecordReader();
		int counter = 0;
		while (reader.nextKeyValue()) {
			mapDriver.withInput(reader.getCurrentKey(),reader.getCurrentValue());
			StringTokenizer itr = new StringTokenizer(reader.getCurrentValue().toString());
			while (itr.hasMoreElements()) {
				mapDriver.withOutput(new Text(itr.nextToken()), one);
				
			}
			counter++;
		}
		assertEquals(3, counter);
		// auto assert output 
		mapDriver.runTest();
	}
	
	private static LineRecordReader getBytesRecordReader()throws IOException, InterruptedException {
		Configuration conf = new Configuration(false);
		conf.set("fs.default.name", "file:///");

		File testFile = new File("src\\test\\resources\\test_data\\text\\hello.text");
		Path path = new Path(testFile.getAbsoluteFile().toURI());
		FileSplit split = new FileSplit(path, 0, testFile.length(), null);

		TextInputFormat inputFormat = ReflectionUtils.newInstance(TextInputFormat.class, conf);
		TaskAttemptContext context = new TaskAttemptContextImpl(conf,new TaskAttemptID());
		LineRecordReader reader = (LineRecordReader) inputFormat.createRecordReader(split, context);

		reader.initialize(split, context);
		return reader;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值