首页 文章

“键入 Map 中的键不匹配:期望org.apache.hadoop.io.IntWritable,收到org.apache.hadoop.io.LongWritable” - 每件事看起来都是正确的

提问于
浏览
1

我正在尝试编写简单的map reduce程序,以使用新的API(0.20.2)找到最大的素数 . 这就是我的Map和reduce类的样子......

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

public void map (LongWritable key, Text Kvalue,Context context) throws IOException,InterruptedException
{
    Integer value = new Integer(Kvalue.toString());
    if(isNumberPrime(value))
    {
            context.write(new IntWritable(value), new IntWritable(new Integer(key.toString())));
    }
}

boolean isNumberPrime(Integer number)
{
    if (number == 1) return false;
     if (number == 2) return true;

     for (int counter =2; counter<(number/2);counter++)
     {
         if(number%counter ==0 )
             return false;
     }
            return true;

}
}
public class PrimeNumberReduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>  {

public void reduce ( IntWritable primeNo, Iterable<IntWritable> Values,Context context) throws IOException ,InterruptedException
{
    int maxValue = Integer.MIN_VALUE;
    for (IntWritable value : Values)
    {
        maxValue=  Math.max(maxValue, value.get());
    }
    //output.collect(primeNo, new IntWritable(maxValue));
    context.write(primeNo, new IntWritable(maxValue));  }

}  

 public static void main(String[] args)  throws IOException, InterruptedException, ClassNotFoundException{

    if (args.length ==0)
    {
        System.err.println(" Usage:\n\tPrimenumber <input Directory> <output Directory>");
        System.exit(-1);
    }
    Job job = new Job();
    job.setJarByClass(Main.class);

    job.setJobName("Prime");
    // Creating job configuration object

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

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

    job.setOutputKeyClass(IntWritable.class);
    job.setOutputValueClass(IntWritable.class);
    String star ="*********************************************";
    System.out.println(star+"\n Prime number computer \n"+star);
    System.out.println(" Application started ... keeping fingers crossed :/ ");
    System.exit(job.waitForCompletion(true)?0:1);

}

}

我仍然收到关于 Map 密钥不匹配的错误

java.io.IOException:键入map中的键不匹配:期望org.apache.hadoop.io.IntWritable,收到org.apache.hadoop.mapred.MapTask上的org.apache.hadoop.io.LongWritable $ MapOutputBuffer.collect(位于org.apache的org.apache.hadoop.mapred.MapTask $ NewOutputCollector.write(MapTask.java:595)org.apache的org.apache.hadoop.mapreduce.TaskInputOutputContext.write(TaskInputOutputContext.java:80)中的MapTask.java:1034)位于org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java)org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:144)的.hadoop.mapreduce.Mapper.map(Mapper.java:124) :668)atg.apache.hadoop.mapred.MapTask.run(MapTask.java:334)atg.apache.hadoop.mapred.Child $ 4.run(Child.java:270)at java.security.AccessController.doPrivileged (本机方法)位于org.apache.hadoop.mapred.Child的org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1109)的javax.security.auth.Subject.doAs(Subject.java:396) .main(Child.java:264)2012-06-13 14:27:21,116 INFO org.apache.hadoop.mapre d.Task:为任务运行清理

有人可以建议有什么不对 . 我试过所有的钩子和骗子 .

3 回答

  • 8

    您没有在主块中配置Mapper或reducer类,因此正在使用默认的Mapper(称为标识映射器) - 它输出的每一对都是输出(因此LongWritable作为输出键):

    job.setMapperClass(PrimeNumberMap.class);
    job.setReducerClass(PrimeNumberReduce.class);
    
  • 0
    • 映射器应定义如下,
    public class PrimeNumberMap extends Mapper<**IntWritable**, Text, IntWritable, IntWritable> {
    

    代替

    public class PrimeNumberMap extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
    
    • 正如在评论中提到的那样,你应该定义mapper和reducer .
    job.setMapperClass(PrimeNumberMap.class);
    job.setReducerClass(PrimeNumberReduce.class);
    

    请参阅Hadoop权威指南第3版,第2章,第24页

  • 0

    我是hadoop mapreduce程序的新手 .

    映射时,我使用 IntWritable 但是我减少了 IntWritable 格式的值,并在上下文写入中使用 DoubleWritable 之前将结果转换为double .

    运行时失败 .

    我在map中处理covert int的方法是double in double:

    Mapper(LongWritable,Text,Text,DoubleWritable)
    Reducer(Text,DoubleWritable,Text,DoubleWritable)
    job.setOutputValueClass(DoubleWritable.Class)
    

相关问题