Hi. This is the code I used and it worked. Go ahead and try this:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
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.mrunit.mapreduce.*;
import org.apache.hadoop.mrunit.types.Pair;
import org.junit.Before;
import org.junit.Test;
public class DataMaper extends TestCase {
public static class myMap extends
Mapper<LongWritable, Text, Text, IntWritable> {
Text day = new Text();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] line = value.toString().split(",");
int val = Integer.parseInt(line[0]);
day.set(line[1]);
context.write(day, new IntWritable(val));
}
}
public static class myreducer extends
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> val, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable i : val) {
sum += i.get();
}
context.write(key, new IntWritable(sum));
}
}
MapDriver<LongWritable, Text, Text, IntWritable> mapDriver;
ReduceDriver<Text, IntWritable, Text, IntWritable> reducerdriver;
MapReduceDriver<LongWritable, Text, Text, IntWritable, Text, IntWritable> mapreducedriver;
@Before
public void setUp() {
myMap mapper = new myMap();
mapDriver = MapDriver.newMapDriver(new myMap());
myreducer reducer = new myreducer();
reducerdriver = ReduceDriver.newReduceDriver(new myreducer());
mapreducedriver = MapReduceDriver.newMapReduceDriver(mapper, reducer);
}
@Test
public void testSimple() throws Exception {
mapreducedriver.withMapper(new myMap());
mapreducedriver.withInput(new LongWritable(1), new Text("1,sunday"));
mapreducedriver.withInput(new LongWritable(1), new Text("2,sunday"));
mapreducedriver.withReducer(new myreducer());
mapreducedriver.withOutput(new Text("sunday"), new IntWritable(3));
mapreducedriver.runTest();
}
}