2013-08-17 23 views
-1

這是我在map/reduce中的第一個程序。我試圖計算文件中元音和輔音的數量,而不是傳統的字數統計程序。以下是我的代碼。地圖Reduce程序只給出Mapper輸出

映射器:

公共類VowelConsMapper延伸映射{

public void map(LongWritable mapKey,Text mapValue,Context context) throws IOException, InterruptedException{ 

    String line = mapValue.toString(); 
    String[] letters = line.split(""); 

    for(String letter : letters){ 
     System.out.println(letter); 
     if(letter!=" "){ 
      if(isVowel(letter)) 
       context.write(new Text("Vowel"), new IntWritable(1)); 
      else 
       context.write(new Text("Consonant"), new IntWritable(1)); 
     } 
    } 
} 

private boolean isVowel(String letter) { 
    // TODO Auto-generated method stub 
    if(letter.equalsIgnoreCase("a")||letter.equalsIgnoreCase("e")||letter.equalsIgnoreCase("i")||letter.equalsIgnoreCase("o")||letter.equalsIgnoreCase("u")) 
     return true; 
    else 
     return false; 
} 

}

減速機:

public class VowelConsReducer extends Reducer<Text, IntWritable, Text, IntWritable> { 
    public void reducer(Text letterType,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException{ 
     int sum = 0; 
     for(IntWritable value:values){ 
      sum += value.get(); 
     } 
     System.out.println(letterType+"  "+sum); 
     context.write(letterType, new IntWritable(sum)); 
    } 
} 

司機:

公共V類owelConsDriver {

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

    Configuration conf = new Configuration(); 
    conf.addResource(new Path("/home/hadoop/hadoop-1.0.3/conf/core-site.xml")); 
    Job job = new Job(conf,"VowelConsDriver"); 

    job.setJarByClass(VowelConsDriver.class); 
    job.setMapperClass(VowelConsMapper.class); 
    job.setReducerClass(VowelConsReducer.class); 

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

    // TODO: specify output types 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(IntWritable.class); 

    FileInputFormat.addInputPath(job, new Path("/user/hadoop/WCinput")); 
    FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/VowelConsOP1")); 

    job.waitForCompletion(true); 
} 

}

這是給以下O/P: 輔音1 輔音1 輔音1 ............... .. ............. ............... ................ ... ............ ............... 元音1 元音1 元音1 元音1 ............... ............... ............... 。 ............... ............... ...............

因爲我在期待輔音&元音每個類別的總數 對不起,如果我沒有正確格式化代碼... &在此先感謝!

回答

1

的減少簽名是方法是「public void reduce()」而不是「public void reducer()」 上述變化將會給你預期的輸出!

0

啓動

public void reducer(
       ^

該行應

@Override 
public void reduce(

預期減速沒有被調用,因爲你misnamed the method,所以不是你得到default implementation。默認的實現只轉儲鍵和值出:

for(VALUEIN value: values) { 
    context.write((KEYOUT) key, (VALUEOUT) value); 
} 

在你的情況下,鍵/值對("Vowel", 1)("Consonant", 1)所以這說明你的輸出。

這就是爲什麼你應始終使用@Override annotation當你覆蓋的方法。編譯器會告訴你,reducer實際上並不是重寫任何方法。