分享

MapReduce 异常 LongWritable cannot be cast to Text

xioaxu790 发表于 2014-9-7 20:12:23 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 1 9622
问题导读
1、如何用MapReduce程序统计分类情况?
2、为什么需要明确声明输入的格式?







有一个txt文件,内容的格式是这样子的:
  1. <span style="font-size:18px;">深圳文化衫订做   5729944  
  2. 深圳厂家t恤批发    5729945  
  3. 深圳定做文化衫 5729944  
  4. 文化衫厂家   5729944  
  5. 订做文化衫   5729944  
  6. 深圳t恤厂家  5729945</span>  
复制代码



前面是搜索关键词,后面的是所属的分类ID,以tab分隔,想统计分类情况。于是用下面的MapReduce程序跑了下:
  1. <span style="font-size:18px;">import java.io.IOException;  
  2. import java.util.*;  
  3.   
  4. import org.apache.hadoop.fs.Path;  
  5. import org.apache.hadoop.conf.*;  
  6. import org.apache.hadoop.io.*;  
  7. import org.apache.hadoop.mapreduce.*;  
  8. import org.apache.hadoop.mapreduce.lib.input.*;  
  9. import org.apache.hadoop.mapreduce.lib.output.*;  
  10. import org.apache.hadoop.util.*;  
  11.   
  12. public class  ClassCount extends Configured implements Tool  
  13. {  
  14.     public static class ClassMap   
  15.         extends Mapper<Text ,Text,Text,IntWritable>  
  16.     {  
  17.         private static final IntWritable one = new IntWritable(1);  
  18.         private Text word = new Text();  
  19.   
  20.         public void map(Text key,Text value,Context context)  
  21.             throws IOException,InterruptedException  
  22.         {  
  23.             String eachLine = value.toString();  
  24.             StringTokenizer tokenizer = new StringTokenizer(eachLine,"\n");  
  25.             while(tokenizer.hasMoreTokens())  
  26.             {  
  27.                 StringTokenizer token = new StringTokenizer(tokenizer.nextToken(),"\t");  
  28.                 String keyword = token.nextToken();//i don't use it now.  
  29.                 String classId = token.nextToken();  
  30.                 word.set(classId);  
  31.                 context.write(word,one);  
  32.             }  
  33.         }  
  34.     }  
  35.   
  36.     public static class Reduce   
  37.         extends Reducer<Text,IntWritable,Text,IntWritable>  
  38.     {  
  39.         public void reduce(Text key,Iterable<IntWritable> values,Context context)  
  40.             throws IOException,InterruptedException  
  41.         {  
  42.             int sum = 0;  
  43.             for(IntWritable val : values)  
  44.                 sum += val.get();  
  45.             context.write(key,new IntWritable(sum));  
  46.         }  
  47.     }  
  48.     public int run(String args[]) throws Exception{  
  49.         Job job = new Job(getConf());  
  50.         job.setJarByClass(ClassCount.class);  
  51.         job.setJobName("classCount");  
  52.          
  53.         job.setMapperClass(ClassMap.class);  
  54.         job.setReducerClass(Reduce.class);  
  55.          
  56.         job.setInputFormatClass(TextInputFormat.class);  
  57.         job.setOutputFormatClass(TextOutputFormat.class);  
  58.   
  59.         FileInputFormat.setInputPaths(job,new Path(args[0]));  
  60.         FileOutputFormat.setOutputPath(job,new Path(args[1]));  
  61.   
  62.         boolean success = job.waitForCompletion(true);  
  63.         return success ? 0 : 1;  
  64.     }  
  65.     public static void main(String[] args) throws Exception  
  66.     {  
  67.         int ret = ToolRunner.run(new ClassCount(),args);  
  68.         System.exit(ret);  
  69.     }  
  70. }  
  71. </span>  
复制代码



抛出如下异常:
  1. java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.Text
复制代码



我以为输入的键是文本就用Text来作为key,但貌似不是这样子的,map方法把文件的行号当成key,所以要用LongWritable。
但是改过来之后,报了下面的异常:
  1. 14/04/25 17:21:15 INFO mapred.JobClient: Task Id : attempt_201404211802_0040_m_000000_1, Status : FAILED  
  2. java.io.IOException: Type mismatch in value from map: expected org.apache.hadoop.io.Text, recieved org.apache.hadoop.io.IntWritable  
复制代码



这个就更加直观了,需要在run方法中添加下面的两行以明确声明输入的格式。
  1. job.setMapOutputKeyClass(Text.class);  
  2.        job.setMapOutputValueClass(IntWritable.class);   
复制代码




已有(1)人评论

跳转到指定楼层
leeworld88 发表于 2014-9-9 11:10:33
把第15行改一下 extends Mapper<LongWritable, Text,Text,IntWritable>, 第20 行 把 Text key 也改为 LongWritable Key . 再 试一试。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条