MAPPER CLASS
import java.io.IOException;
import java.util.TreeSet;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MapperTopNMR
extends Mapper<LongWritable, Text, NullWritable, Text>
{
//TreeSet<Text> fatcats = new TreeSet<Text>(new SelComp());
TreeSet<Text> fatcats = new TreeSet<Text>();
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException
{
fatcats.add(new Text(value));
if (fatcats.size() > 3)
{
fatcats.remove(fatcats.first());
}
}
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException
{
for ( Text catname : fatcats )
{
context.write( NullWritable.get(),catname);
}
}
}
MRUNIT for MAPPER CLASS
public class TopNMRTest
{
MapDriver<LongWritable, Text, NullWritable, Text> mapDriver;
@Before
public void setUp()
{
MapperTopNMR mapper = new MapperTopNMR();
mapDriver = new MapDriver<LongWritable, Text, NullWritable, Text>();
mapDriver.setMapper(mapper);
}
@Test
public void testMapper() throws IOException
{
mapDriver.withInput(new LongWritable(1), new Text("11"));
mapDriver.withInput(new LongWritable(1), new Text("15"));
mapDriver.withInput(new LongWritable(1), new Text("3"));
mapDriver.withInput(new LongWritable(1), new Text("3"));
mapDriver.withInput(new LongWritable(1), new Text("7"));
mapDriver.withOutput(NullWritable.get(), new Text("7"));
mapDriver.withOutput(NullWritable.get(), new Text("11"));
mapDriver.withOutput(NullWritable.get(), new Text("15"));
mapDriver.runTest();
}
I am expecting result as
(NULL) 7
(NULL) 11
(NULL) 15
but when i printed the output the value which is stored in Treeset is not in
order, it giving the way which is insert for this example
Treeset contains : 11 15 3 7 (another 3 is duplicate it was eliminated).
Note: TreeSet - eliminating duplicate and not giving natural order. Even i tried comparator for TreeSet instance for reversing order it gives the following result
7 3 15 11.
Please help me to get out of this issue.