這是我在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 ............... ............... ............... 。 ............... ............... ...............
因爲我在期待輔音&元音每個類別的總數 對不起,如果我沒有正確格式化代碼... &在此先感謝!