雲端計算(二十三)-編寫WordCount並使用MRUnit測試
阿新 • • 發佈:2019-02-01
public class MRTest {
MapDriver<Object, Text, Text, IntWritable> mapDriver;
ReduceDriver<Text, IntWritable, Text, IntWritable> redeceDriver;
MapReduceDriver<Object, Text,Text, IntWritable,Text, IntWritable> mapReduceDriver;
@Before
public void setUp(){
MapperClass mapper = new MapperClass();
ReducerClass reducer = new ReducerClass();
mapDriver = MapDriver.newMapDriver(mapper);
redeceDriver = ReduceDriver.newReduceDriver(reducer);
mapReduceDriver = MapReduceDriver.newMapReduceDriver(mapper, reducer);
}
@Test
public void testMapper() throws IOException{
mapDriver.withInput(new IntWritable(1), new Text("du kai is a good boy"));
mapDriver.withOutput(new Text("du"), new IntWritable(1))
.withOutput(new Text("kai"), new IntWritable(1))
.withOutput(new Text("is"), new IntWritable(1))
.withOutput(new Text("a"), new IntWritable(1))
.withOutput(new Text("good"), new IntWritable(1))
.withOutput(new Text("boy"), new IntWritable(1));
mapDriver.runTest();
}
@Test
public void testReduce() throws Exception{
List<IntWritable> values = new ArrayList<IntWritable>();
values.add(new IntWritable(1));
values.add(new IntWritable(1));
redeceDriver.withInput(new Text("6"), values).
withOutput(new Text("6"), new IntWritable(2))
.runTest();
}
@Test
public void test() throws IOException{
String line = "Dukai is a great boy is it not";
List<Pair<Text, IntWritable>> out = mapReduceDriver.withInput(new IntWritable(1) ,new Text(line)).run();
List<Pair<Text, IntWritable>> expected = new ArrayList<Pair<Text, IntWritable>>();
expected.add(new Pair(new Text("Dukai"),new IntWritable(1)));
expected.add(new Pair(new Text("a"),new IntWritable(1)));
expected.add(new Pair(new Text("boy"),new IntWritable(1)));
expected.add(new Pair(new Text("great"),new IntWritable(1)));
expected.add(new Pair(new Text("is"),new IntWritable(2)));
expected.add(new Pair(new Text("it"),new IntWritable(1)));
expected.add(new Pair(new Text("not"),new IntWritable(1)));
ExtendedAssert.assertListEquals(expected, out);
}
}
MapDriver<Object, Text, Text, IntWritable> mapDriver;
ReduceDriver<Text, IntWritable, Text, IntWritable> redeceDriver;
MapReduceDriver<Object, Text,Text, IntWritable,Text, IntWritable> mapReduceDriver;
@Before
public void setUp(){
MapperClass mapper = new MapperClass();
ReducerClass reducer = new ReducerClass();
mapDriver = MapDriver.newMapDriver(mapper);
redeceDriver = ReduceDriver.newReduceDriver(reducer);
mapReduceDriver = MapReduceDriver.newMapReduceDriver(mapper, reducer);
}
@Test
public void testMapper() throws IOException{
mapDriver.withInput(new IntWritable(1), new Text("du kai is a good boy"));
mapDriver.withOutput(new Text("du"), new IntWritable(1))
.withOutput(new Text("kai"), new IntWritable(1))
.withOutput(new Text("is"), new IntWritable(1))
.withOutput(new Text("a"), new IntWritable(1))
.withOutput(new Text("good"), new IntWritable(1))
.withOutput(new Text("boy"), new IntWritable(1));
mapDriver.runTest();
}
@Test
public void testReduce() throws Exception{
List<IntWritable> values = new ArrayList<IntWritable>();
values.add(new IntWritable(1));
values.add(new IntWritable(1));
redeceDriver.withInput(new Text("6"), values).
withOutput(new Text("6"), new IntWritable(2))
.runTest();
}
@Test
public void test() throws IOException{
String line = "Dukai is a great boy is it not";
List<Pair<Text, IntWritable>> out = mapReduceDriver.withInput(new IntWritable(1) ,new Text(line)).run();
List<Pair<Text, IntWritable>> expected = new ArrayList<Pair<Text, IntWritable>>();
expected.add(new Pair(new Text("Dukai"),new IntWritable(1)));
expected.add(new Pair(new Text("a"),new IntWritable(1)));
expected.add(new Pair(new Text("boy"),new IntWritable(1)));
expected.add(new Pair(new Text("great"),new IntWritable(1)));
expected.add(new Pair(new Text("is"),new IntWritable(2)));
expected.add(new Pair(new Text("it"),new IntWritable(1)));
expected.add(new Pair(new Text("not"),new IntWritable(1)));
ExtendedAssert.assertListEquals(expected, out);
}
}