-1
 public class PrimeMapper extends Mapper<LongWritable,IntWritable,IntWritable,NullWritable>
    {
         public void map(LongWritable k,IntWritable val,Context c) throws IOException, InterruptedException
            {
                 int v=val.get();
                 int i=2;
                 if(v==1)
                 c.write(new IntWritable(v), NullWritable.get());
                 for(i=2;i<v;i++)
                     {
                         if(v%i==0)
                         break;
                     }
                 if(v==i)
                 c.write(new IntWritable(v),NullWritable.get());
            }
   }

when i am trying to run this code i getting error of typecasting error .

enter image description here

Kohei TAMURA
  • 4,238
  • 6
  • 20
  • 41

1 Answers1

0

When you use the default input format which is TextInputFormat (extends FileInputFormat<LongWritable, Text>), the mapper expects LongWritable as key and Text as value. If you haven't changed this explicitly in your program, your mapper definition is incorrect.

A correct implementation would look something like this:

public class PrimeMapper extends Mapper<LongWritable,Text,IntWritable,NullWritable>{
   public void map(LongWritable k,Text val,Context c) throws IOException, InterruptedException{
     int v=Integer.parseInt(val.toString().trim());
     int i=2;
     if(v==1)
     c.write(new IntWritable(v), NullWritable.get());
     for(i=2;i<v;i++)
         {
             if(v%i==0)
             break;
         }
     if(v==i)
     c.write(new IntWritable(v),NullWritable.get());
  }
}
philantrovert
  • 8,704
  • 3
  • 25
  • 48