java.lang.Exception:java.lang.ClassCastException:org.apache.hadoop.io.Text无法强制转换为org.apache.hadoop.io.IntWritable

时间:2017-07-21 12:51:35

标签: java hadoop exception mapper reducers

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

当我尝试运行此代码时,我收到类型错误的错误。

enter image description here

1 个答案:

答案 0 :(得分:0)

当您使用TextInputFormat(扩展FileInputFormat<LongWritable, Text>)的默认输入格式时,映射器预期LongWritablekeyText为{{1} }}。如果您尚未在程序中明确更改此设置,则您的映射器定义不正确。

正确的实现看起来像这样:

value