MapReduce关于类型转换报错记录

简介: MapReduce关于类型转换报错记录

MapReduce关于类型转换报错记录



ad61faea5ea343a28fe757431d8517ed.jpeg



0. 写在前面

  • 实验环境:Ubuntu Kylin16.04
  • Hadoop版本:2.7.2
  • IDE:Eclipse3.8


1. 程序代码


Mapper端


importorg.apache.hadoop.io.IntWritable;
importorg.apache.hadoop.io.LongWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Mapper;
publicstaticclassWordCountMapperextendsMapper<LongWritable, Text, Text, IntWritable> {
@Overrideprotectedvoidmap(LongWritablekey, Textvalue, Contextcontext) throwsIOException, InterruptedException {
String[] strs=value.toString().split(" ");
for (Stringstr : strs) {
context.write(newText(str), newIntWritable(1));
  }
    }
}


Reducer端

importorg.apache.hadoop.io.IntWritable;
importorg.apache.hadoop.io.LongWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Reducer;
publicstaticclassWordCountReduceextendsReducer<Text, IntWritable, Text, IntWritable> {
@Overrideprotectedvoidreduce(Textkey, Iterable<IntWritable>values, Contextcontext)
throwsIOException, InterruptedException {
intsum=0;
for (IntWritableval : values) {
System.out.println("<"+key+","+val+">");
sum+=val.get();
  }
context.write(key, newIntWritable(sum));
    }
}


Driver端

importjava.io.IOException;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.IntWritable;
importorg.apache.hadoop.io.LongWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Job;
importorg.apache.hadoop.mapreduce.Mapper;
importorg.apache.hadoop.mapreduce.Reducer;
importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
publicclassTxtCntDemo {
publicstaticvoidmain(String[] args) throwsException {
args=newString[] { "/input", "/output"};
Configurationconf=newConfiguration();
Jobjob=Job.getInstance(conf);/job.setJarByClass(TxtCntDemo.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(WordCountMap.class);
job.setReducerClass(WordCountReduce.class);
job.setPartitionerClass(MyPartitioner.class);
job.setNumReduceTasks(4);
FileInputFormat.addInputPath(job, newPath(args[0]));
FileOutputFormat.setOutputPath(job, newPath(args[1]));
job.waitForCompletion(true);
    }
}



「错误描述」

java.lang.Exception: java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.hadoop.io.IntWritable
java.lang.Exception: java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.IntWritable


mapper、reducer、driver分开成3个文件,报Text不可转换成IntWritable,还有LongWritable不能转换成IntWritable的错误


关于第二个错误:Mapper端执行时,key的默认输入是LongWritable类型,把LongWritable类型强行转换成Text类型自然就Error了。


java.lang.Exception: java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.hadoop.io.IntWritable
  at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
  at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.hadoop.io.IntWritable
  at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:715)
  at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)
  at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112)
  at cn.mr.WordCountMapper.map(WordCountMapper.java:15)
  at cn.mr.WordCountMapper.map(WordCountMapper.java:1)
  at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
  at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
  at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
  at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:243)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  at java.lang.Thread.run(Thread.java:748)



但是同样的代码mapper、reducer、driver直接放在一个文件下就顺利执行MR得出结果


这个属实给我整不会了 😢 😢



2. 参考


https://www.cnblogs.com/1130136248wlxk/p/5010489.html


记录一下

目录
相关文章
|
分布式计算 Ubuntu Hadoop
MapReduce报错:「MKDirs failed to create file」
MapReduce报错:「MKDirs failed to create file」
331 0
MapReduce报错:「MKDirs failed to create file」
|
分布式计算 Ubuntu IDE
执行MapReduce报错:无法分配内存 (errno=12)
执行MapReduce报错:无法分配内存 (errno=12)
234 0
执行MapReduce报错:无法分配内存 (errno=12)
|
分布式计算 资源调度 Java
执行MapReduce的Jar包报错:Exception in thread main ExitCodeException exitCode=1
执行MapReduce的Jar包报错:Exception in thread main ExitCodeException exitCode=1
139 0
执行MapReduce的Jar包报错:Exception in thread main ExitCodeException exitCode=1
|
数据采集 分布式计算 搜索推荐
Hadoop学习---7、OutputFormat数据输出、MapReduce内核源码解析、Join应用、数据清洗、MapReduce开发总结(一)
Hadoop学习---7、OutputFormat数据输出、MapReduce内核源码解析、Join应用、数据清洗、MapReduce开发总结(一)
|
存储 分布式计算 Hadoop
Hadoop基础学习---6、MapReduce框架原理(一)
Hadoop基础学习---6、MapReduce框架原理(一)
|
存储 分布式计算 Hadoop
【Hadoop】一个例子带你了解MapReduce
【Hadoop】一个例子带你了解MapReduce
98 1
|
数据采集 缓存 分布式计算
Hadoop学习---7、OutputFormat数据输出、MapReduce内核源码解析、Join应用、数据清洗、MapReduce开发总结(二)
Hadoop学习---7、OutputFormat数据输出、MapReduce内核源码解析、Join应用、数据清洗、MapReduce开发总结(二)
|
分布式计算 Hadoop 数据处理
Hadoop基础学习---6、MapReduce框架原理(二)
Hadoop基础学习---6、MapReduce框架原理(二)
|
分布式计算 资源调度 Hadoop
Hadoop基础学习---5、MapReduce概述和WordCount实操(本地运行和集群运行)、Hadoop序列化
Hadoop基础学习---5、MapReduce概述和WordCount实操(本地运行和集群运行)、Hadoop序列化
下一篇
无影云桌面