java.lang.exception:java.lang.classcastexception:org.apache.hadoop.io.text不能强制转换为org.apache.hadoop.io.intwritable

z31licg0  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(718)
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());
            }
   }

当我试图运行这段代码时,我得到了一个类型转换错误的错误。

toe95027

toe950271#

使用默认输入格式textinputformat(extends)时 FileInputFormat<LongWritable, Text> ),Map程序期望 LongWritable 作为 key 以及 Text 作为 value . 如果在程序中没有显式更改,则Map器定义不正确。
正确的实现如下所示:

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());
  }
}

相关问题