0

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.

Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
  • Where is the treeset declared, is it an instance variable? – Gyanendra Dwivedi Sep 09 '15 at 16:01
  • Please have a look on the code. – selvam raman Sep 10 '15 at 07:10
  • Did you try adding your elements into treeset as Intwritable instead of text. I guess since you are using text it is not sorting properly. – Vignesh I Sep 10 '15 at 09:01
  • I changed the like below TreeSet fatcats = new TreeSet(); fatcats.add(new IntWritable(Integer.parseInt(value.toString()))); Its Working fine. Do you have any idea why Text of value is not sorted order in TreeSet in mapper function. When i tried the same in plain jave it was working fine for even TEXT of TreeSet. Thank you for your comments – selvam raman Sep 11 '15 at 05:27

0 Answers0