Hadoop: LongWritable cannot be cast to org.apache.hadoop.io.IntWritable

写MR Job的时候遇到一个坑爹的异常:

LongWritable cannot be cast to org.apache.hadoop.io.IntWritable

当写Map的时候,key的默认输入就是LongWritable。

因为LongWritable指代Block中的数据偏移量。

所以把它强行转换成Text当然就Error了。。

Java代码  收藏代码
  1. public static class TempMapper extends Mapper<LongWritable, Text, IntWritable, FloatWritable>{  
  2.   
  3.   @Override  
  4.   protected void map(<span style="background-color: #ffff00;">LongWritable key</span>, Text value, Context context)  
  5.                 throws IOException, InterruptedException {  
  6.        //code for getting date and temperature  
  7.        String temp = columns.get(3);  
  8.        context.write(new IntWritable(year), new FloatWritable(Float.valueOf(temp)));  
  9.   }  
  10. }  

坑啊。。

原文地址:https://www.cnblogs.com/1130136248wlxk/p/5010489.html