1. 开发环境下载mrunit包
https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit
2. 编写map reduce 程序
1. mapper
/**
* 类说明。
*
* @author East271536394
* @version 2013-4-6 下午02:07:36
*/
public class ReverIndexMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
try {
String[] lineSplit = line.split(" ");
String num1 = lineSplit[0];
String num2 = lineSplit[1];
context.write(new Text(num1), new Text(num2));
} catch (Exception se) {
se.printStackTrace();
}
}
}
2. reduce
/**
* 类说明。
*
* @author East271536394
* @version 2013-4-6 下午02:08:07
*/
public class ReverIndexReduce extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException,
InterruptedException {
String result = "";
String value;
for (Text text : values) {
value = text.toString();
result += value + "|";
}
System.out.println("result:" + result);
context.write(key, new Text(result));
}
}
3. 编写mrtest类
1. mapper test
/**
* 类说明。
*
* @author East271536394
* @version 2013-4-10 下午09:57:48
*/
public class ReverIndexMapperTest extends TestCase {
private Mapper<LongWritable, Text, Text, Text> mapper;
private MapDriver<LongWritable, Text, Text, Text> mapDriver;
@Before
public void init() {
mapper = new ReverIndexMapper();
mapDriver = new MapDriver<LongWritable, Text, Text, Text>(mapper);
}
@Test
public void test() {
try {
String line = "test is";
LongWritable lw = new LongWritable(1);
mapDriver.withInput(lw, new Text(line));
mapDriver.withOutput(new Text("test"), new Text("is"));
mapDriver.runTest();
} catch (Exception se) {
se.printStackTrace();
}
}
}
2. reduce mr test
/**
* 类说明。
*
* @author East271536394
* @version 2013-4-14 上午10:24:07
*/
public class ReverIndexReduceTest extends TestCase {
private Reducer<Text, Text, Text, Text> reduce;
private ReduceDriver<Text, Text, Text, Text> reduceDriver;
@Before
public void init() {
reduce = new ReverIndexReduce();
reduceDriver = new ReduceDriver<Text, Text, Text, Text>(reduce);
}
@Test
public void test() {
try {
String key = "10086";
List<Text> values = new ArrayList<Text>();
values.add(new Text("181"));
values.add(new Text("182"));
values.add(new Text("183"));
reduceDriver.withInput(new Text(key), values);
reduceDriver.withOutput(new Text(key), new Text("181|182|183|"));
reduceDriver.runTest();
} catch (IOException e) {
}
}
}
3. mapreduce test
/**
* 类说明。
*
* @author East271536394
* @version 2013-4-14 下午12:07:27
*/
public class ReverIndexMapReduceTest extends TestCase {
private Reducer<Text, Text, Text, Text> reduce;
private Mapper<LongWritable, Text, Text, Text> mapper;
private MapReduceDriver<LongWritable, Text, Text, Text, Text, Text> mapReduceDriver;
@Before
public void init() {
mapper = new ReverIndexMapper();
reduce = new ReverIndexReduce();
mapReduceDriver = new MapReduceDriver<LongWritable, Text, Text, Text, Text, Text>(mapper, reduce);
}
private String generateKey()
{
StringBuilder sb = new StringBuilder();
sb.append("test is");
return sb.toString();
}
@Test
public void testMapreduce() throws Exception {
init();
String line = generateKey();
LongWritable lw = new LongWritable(1);
mapReduceDriver.withInput(lw, new Text(line));
mapReduceDriver.withOutput(new Text("test"), new Text("is|"));
mapReduceDriver.runTest();
}
}
参考:https://cwiki.apache.org/confluence/display/MRUNIT/MRUnit+Tutorial