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

在使用Hadoop进行大数据处理时,遇到将LongWritable类型转换为IntWritable类型的错误。通过分析代码,发现直接转换会导致错误。文章详细解释了原因并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

 

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

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

 

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

public static class TempMapper extends Mapper<LongWritable, Text, IntWritable, FloatWritable>{

  @Override
  protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
       //code for getting date and temperature
       String temp = columns.get(3);
       context.write(new IntWritable(year), new FloatWritable(Float.valueOf(temp)));
  }
}

 

 

坑啊。。

package mergeData.mapreduce; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.FileSplit; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; public class MergeDataDriver { /********** Begin **********/ public static class Map extends Mapper{LongWritable, Text, NullWritable,Text>{} /********** End **********/ } /********** Begin **********/ public static class Reduce extends Reducer{ } /********** End **********/ public static void main(String[] args) throws Exception { //创建配置信息 Configuration conf = new Configuration(); // 创建任务 Job job = Job.getInstance(conf); //如果输出目录存在,我们就删除 String outputpath = "/root/files"; Path path = new Path(outputpath); FileSystem fileSystem = path.getFileSystem(conf); if (fileSystem.exists(path)) { fileSystem.delete(path, true); } /********** Begin **********/ //设置执行类 //设置自定义Mapper类 //设置自定义Reducer类(若没用reduce可删除) //设置map函数输出数据的key和value的类型 //设置reduce函数输出数据的key和value的类型(若没用reduce可删除) //设置输入输出路径 /********** End **********/ //提交作业,若成功返回true,失败返回falase boolean b = job.waitForCompletion(true); if (b
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值