2012-06-13 56 views
1

我想寫簡單的地圖減少程序找到最大使用新API的素數(0.20.2)。這是我的地圖和減少類的樣子......「類型不匹配的關鍵字從地圖:預期org.apache.hadoop.io.IntWritable,接收org.apache.hadoop.io.LongWritable」 - 每件事看起來正確

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); 

} 

} 

我仍然得到關於在地圖

java.io.IOException異常關鍵的失配誤差如何:從地圖鍵類型不匹配:預計組織.apache.hadoop.io.IntWritable,收到org.apache.hadoop.io.LongWritable at org.apache.hadoop.mapred.MapTask $ MapOutputBuffer.collect(MapTask.java:1034) at org.apache.hadoop.mapred .MapTask $ NewOutputCollector.write(MapTask.java:595) at org.apache.hadoop.mapreduce.TaskInputOutputContext.write(TaskInputOutputContext.java:80) at org.apache.hadoop.mapreduce.Mapper.map(Mapper.java:124) at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:144) at org.apache.hadoop.mapred。 MapTask.runNewMapper(MapTask.java:668) at org.apache.hadoop.mapred.MapTask.run(MapTask.java:334) at org.apache.hadoop.mapred.Child $ 4.run(Child.java:270 ) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:396) at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java: 1109) at org.apache.hadoop.mapred.Child.main(Child.java:264) 2012-06-13 14:27:21,116 INFO org.apache.hadoop.mapred.Task:Runnning cleanup for the task

有人可以建議什麼是錯的。我已經嘗試過所有的鉤子和騙子。

回答

8

您尚未在您的主塊中配置Mapper或Reducer類,因此正在使用默認映射器 - 即所謂的標識映射器 - 它接收到的每對輸入都是輸出(因此LongWritable作爲輸出鍵):

job.setMapperClass(PrimeNumberMap.class); 
job.setReducerClass(PrimeNumberReduce.class); 
+0

非常感謝...這是我錯過了什麼。 – NipsRock

+0

如何配置它們? –

0
  1. 映射器應定義爲下面,

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

    代替

    public class PrimeNumberMap extends Mapper<LongWritable, Text, IntWritable, IntWritable> { 
    
  2. 正如在評論中提到的,你應該先定義mapper和reducer。

    job.setMapperClass(PrimeNumberMap.class); 
    job.setReducerClass(PrimeNumberReduce.class); 
    

請參考Hadoop的權威指南第三版,第2章,第24頁

0

我在Hadoop中的MapReduce程序中的新的手。

映射時,我使用IntWritable,但我減少了IntWritable格式的值,並在上下文寫入中使用DoubleWritable之前將結果轉換爲雙倍值。

運行時失敗。

我的方法來處理隱蔽INT在地圖翻一番減少是:

Mapper(LongWritable,Text,Text,DoubleWritable) 
Reducer(Text,DoubleWritable,Text,DoubleWritable) 
job.setOutputValueClass(DoubleWritable.Class) 
相關問題