键入map中的键不匹配:期望org.apache.hadoop.io.IntWritable,收到org.apache.hadoop.io.LongWritable

时间:2018-06-13 04:46:54

标签: java hadoop mapreduce

已经检查了这一点,我无法理解为什么会遇到这个错误。

映射

public class movieMapper extends Mapper<LongWritable, Text, IntWritable, Text> {

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

    String token[]= value.toString().trim().split("::");

    int movieID=Integer.parseInt(token[0].trim());

    context.write(new IntWritable(movieID), new Text(token[1].trim()));

}

}

减速

public class joinReducer extends Reducer<IntWritable, Text, Text, Text> {

public void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException,InterruptedException {
    float avgRating=0.0f;
    int tokenCount = 0;
    float ratingSum=0.0f;
    int count=0;

    String movieName="";

    for(Text val:values) {
        tokenCount+=1;
    }

    //If we have more than 40 views/ratings
    if(tokenCount-1>40) {

        for(Text val:values) {

            String temp = val.toString();


            if(val.equals("1")||val.equals("2")||val.equals("3")||val.equals("4")||val.equals("5")) {

                float tempRating= Float.parseFloat(val.toString().trim());
                ratingSum += tempRating;
                count++;


            }

            else {

                movieName=val.toString().trim();
            }

        }

        avgRating = ratingSum/ (float)count;

        context.write(new Text(Float.toString(avgRating)), new Text(movieName));
    }

}

}

驱动程序配置

Configuration conf= new Configuration();
    String parameter[]= new GenericOptionsParser(conf,args).getRemainingArgs();

    if(parameter.length!=3) {

        System.err.println("Three arguments needed  <File1> <File2> <Out>");
        System.exit(2);
    }


    //set Driver class

    Job job1 = Job.getInstance(conf, "Join");
    job1.setJarByClass(MyDriver.class);
    job1.setReducerClass(joinReducer.class);

    MultipleInputs.addInputPath(job1,  new Path(parameter[0]), TextInputFormat.class, movieMapper.class);
    MultipleInputs.addInputPath(job1,  new Path(parameter[1]), TextInputFormat.class, ratingMapper.class);

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


    job1.setOutputKeyClass(Text.class);
    job1.setOutputValueClass(Text.class);


    FileOutputFormat.setOutputPath(job1, new Path(parameter[2] + "/temp"));

    job1.waitForCompletion(true);
  

18/06/13 09:47:20 INFO mapreduce.Job:在uber模式下运行的job job_1528823320386_0018:false   18/06/13 09:47:20 INFO mapreduce.Job:地图0%减少0%   18/06/13 09:47:24 INFO mapreduce.Job:任务ID:attempt_1528823320386_0018_m_000000_0,状态:未通过   错误:java.io.IOException:键入map中的键不匹配:expected org.apache.hadoop.io.IntWritable,收到org.apache.hadoop.io.LongWritable       at org.apache.hadoop.mapred.MapTask $ MapOutputBuffer.collect(MapTask.java:1069)       at org.apache.hadoop.mapred.MapTask $ NewOutputCollector.write(MapTask.java:712)       at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)       at org.apache.hadoop.mapreduce.lib.map.WrappedMapper $ Context.write(WrappedMapper.java:112)       在org.apache.hadoop.mapreduce.Mapper.map(Mapper.java:124)       在org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)       在org.apache.hadoop.mapreduce.lib.input.DelegatingMapper.run(DelegatingMapper.java:55)       在org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:784)       在org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)       在org.apache.hadoop.mapred.YarnChild $ 2.run(YarnChild.java:168)       at java.security.AccessController.doPrivileged(Native Method)       在javax.security.auth.Subject.doAs(Subject.java:422)       at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1642)       在org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)

     

18/06/13 09:47:25 INFO mapreduce.Job:地图50%减少0%   18/06/13 09:47:29 INFO mapreduce.Job:任务ID:attempt_1528823320386_0018_m_000000_1,状态:未通过   错误:java.io.IOException:键入map中的键不匹配:expected org.apache.hadoop.io.IntWritable,收到org.apache.hadoop.io.LongWritable       at org.apache.hadoop.mapred.MapTask $ MapOutputBuffer.collect(MapTask.java:1069)       at org.apache.hadoop.mapred.MapTask $ NewOutputCollector.write(MapTask.java:712)       at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)       at org.apache.hadoop.mapreduce.lib.map.WrappedMapper $ Context.write(WrappedMapper.java:112)       在org.apache.hadoop.mapreduce.Mapper.map(Mapper.java:124)       在org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)       在org.apache.hadoop.mapreduce.lib.input.DelegatingMapper.run(DelegatingMapper.java:55)       在org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:784)       在org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)       在org.apache.hadoop.mapred.YarnChild $ 2.run(YarnChild.java:168)       at java.security.AccessController.doPrivileged(Native Method)       在javax.security.auth.Subject.doAs(Subject.java:422)       at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1642)       在org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)

1 个答案:

答案 0 :(得分:0)

这个作业中有两个映射器,即movieMapper和ratingMapper。 ratingMapper在函数声明中拼写错误的关键字和地图函数的名称&#39; map&#39;被错误地写为&#39; reduce&#39;。

根据out配置,reducer应该接受IntWritable类型的键,但是得到LongWritable,因此错误。 (TextInputFormat生成LongWritable类型的键和Text类型的值)