【问题标题】:Runtime Error in the Max temperature Mapreduce java codeMax temperature Mapreduce java代码中的运行时错误
【发布时间】:2016-01-05 11:28:33
【问题描述】:

我正在运行 mapreduce 代码,我得到的错误是

    Error: java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.IntWritable
        at test.temp$Mymapper.map(temp.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.YarnChild$2.run(YarnChild.java:164)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:415)
        at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
        at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

代码如下:

    package test;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
//import org.apache.hadoop.mapred.JobConf;
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.output.FileOutputFormat;


public class temp {
    public static class Mymapper extends Mapper<Object, Text, IntWritable,Text> {

        public void map(Object key, Text value,Context context) throws IOException, InterruptedException{

            int month=Integer.parseInt(value.toString().substring(17, 19));
            IntWritable mon=new IntWritable(month);
            String temp=value.toString().substring(27,31);
            String t=null;
            for(int i=0;i<temp.length();i++){
                if(temp.charAt(i)==',')
                        break;

                else
                    t=t+temp.charAt(i);
            }

            Text data=new Text(value.toString().substring(22, 26)+t);
            context.write(mon, data);
        }


    }

    public static class Myreducer extends  Reducer<IntWritable,Text,IntWritable,IntWritable> {

        public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
            String temp="";
            int max=0;
            for(Text t:values)
            {
                temp=t.toString();
                if(temp.substring(0, 4)=="TMAX"){

                    if(Integer.parseInt(temp.substring(4,temp.length()))>max){
                        max=Integer.parseInt(temp.substring(4,temp.length()));
                    }
                }
            }

            context.write(key,new IntWritable(max));
        }



        }



    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "temp");
        job.setJarByClass(temp.class);
        job.setMapperClass(Mymapper.class);
        job.setCombinerClass(Myreducer.class);
        job.setReducerClass(Myreducer.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.waitForCompletion(true);

        }

}

输入文件是

USC00300379,19000101,TMAX,-78,,,6, USC00300379,19000101,TMAX,-133,,,6, USC00300379,19000101,TMAX,127,,,6

请回复并提供帮助!

【问题讨论】:

  • 添加了答案。检查它是否有效。

标签: java apache hadoop mapreduce hadoop-streaming


【解决方案1】:

认为您正在使用 TextInputFormat 作为作业的输入格式。这会产生 LongWritable/Text,而 Hadoop 正在从中派生映射输出类。

尝试显式设置地图输出类并移除组合器:

job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);
// job.setCombinerClass(Myreducer.class);

只有当 map 和 reduce 输出兼容时,combiner 才会起作用!

【讨论】:

  • 好的,更新了我的答案。您还需要设置地图输出值类并取消设置组合器。通过这些更改,我让您的代码成功完成!
  • 我做了那个改变,它仍然给出同样的错误@oae,你确定它运行成功了吗?
  • 是的,我复制了您的代码,添加了设置键和值类的 2 行并删除了组合器类!你能仔细看看它是否完全相同的信息?因为在我完成修复之前,我收到了不同的错误消息,它们看起来相同,但看了两次我发现它们略有不同!
【解决方案2】:

当您在驱动程序中设置以下内容时,

job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);

它为 mapper 和 reducer 定义了 output 类,而不仅仅是 reducer。

这意味着你的映射器应该有connect.write(IntWritable, IntWritable),但你已经编码了connect.write(IntWritable, Text)

修复:当你的map输出类型和reduce输出不同时,你需要显式设置mapper的输出类型。因此,在您的驱动程序代码中添加以下内容。

job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);

【讨论】:

    【解决方案3】:

    您在驱动程序中设置了以下内容:

    job.setOutputKeyClass(IntWritable.class);
    job.setOutputValueClass(IntWritable.class);
    

    这意味着,您的 mapper 和 reducer 输出键类都应该是 IntWritable,值类应该是 IntWritable

    减速机不错:

    public static class Myreducer extends  Reducer<IntWritable,Text,IntWritable,IntWritable> 
    

    这里的输出键和值都是IntWritable

    问题出在映射器上:

    public static class Mymapper extends Mapper<Object, Text, IntWritable,Text> 
    

    这里的输出键类是IntWritable。但是,输出值类是Text(预计是IntWritable)。

    如果你的 mapper 的输出 key/value 类与你的 reducer 的输出 key/value 类不同,那么你需要在你的驱动中显式添加以下语句:

    setMapOutputKeyClass();
    setMapOutputValueClass();
    

    在您的代码中进行以下更改:

    • 设置map输出key和value类:在你的情况下,由于你的mapper和reducer输出key和value类不同,需要设置如下:

      job.setMapOutputKeyClass(IntWritable.class);
      job.setMapOutputValueClass(Text.class);
      
      job.setOutputKeyClass(IntWritable.class);
      job.setOutputValueClass(IntWritable.class);
      
    • 禁用组合器:由于您将 Reducer 代码用于您的 Combiner,因此 Combiner 的输出将是 IntwritableIntWritable。但是,Reducer 期望输入为IntWritableText。因此,您将得到以下异常,因为它的值是IntWritable 而不是Text

      Error: java.io.IOException: wrong value class: class org.apache.hadoop.io.IntWritable is not class org.apache.hadoop.io.Text
      

      要消除此错误,您需要禁用Combiner

      job.setCombinerClass(Myreducer.class);
      
    • 不要使用reducer作为combiner:如果你确实需要使用combiner,那就写一个combiner,它的输出key/value是IntWritableText

    【讨论】:

      【解决方案4】:

      这是我所做的更改。

      public static void main(String[] args) throws Exception {
      
              Configuration conf = new Configuration();
              Job job = Job.getInstance(conf, "temp");
      
              job.setJarByClass(Temp.class);
      
              job.setMapperClass(Mymapper.class);
              job.setReducerClass(Myreducer.class);
      
              job.setMapOutputKeyClass(IntWritable.class);
              job.setMapOutputValueClass(Text.class);
      
              job.setOutputKeyClass(IntWritable.class);
              job.setOutputValueClass(IntWritable.class);
      
              FileInputFormat.addInputPath(job, new Path(args[0]));
              FileOutputFormat.setOutputPath(job, new Path(args[1]));
      
              job.setNumReduceTasks(1);
              job.waitForCompletion(true);
          }
      

      输出: 10 0

      有关解释,请关注 Manjunath Ballur 的帖子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-02
        • 1970-01-01
        • 2015-06-03
        • 2023-03-24
        • 2016-03-31
        • 1970-01-01
        • 2015-01-27
        相关资源
        最近更新 更多