Ich versuche mapreduce zu lernen. Beim Starten des WordCount-Beispiels, wie in MapReduce WordCount gezeigt, wenn ich den Code in Eclipse ausführe, war die Ausgabe die korrekte Wortzahl. I/p Dateiinhalt war wie folgt: -Mapreduce WordCount Beispiel mit falschem Ausgang
Hallo Welt Bye Welt
Es ist Ausgang war
Bye 1
Hallo 1
World 2
Danach teste ich den Code, indem ich das Leerzeichen nach jedem Wort in der Eingabedatei durch Komma ersetze.
Jetzt habe ich die Eingabe auf die gleiche wie zuvor zurückgesetzt, aber jetzt WordCount in der Ausgabe ist das doppelte des erwarteten Ergebnisses.
Bye 2
Hallo 2
Welt 4
My-Code ist wie folgt:
public static class TokenizerMapper extends Mapper<Object, Text, Text,IntWritable>{
public static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()){
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
int sum=0;
for(IntWritable val:values){
sum +=val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] str) throws Exception{
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(str[0]));
FileOutputFormat.setOutputPath(job,new Path(str[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
Kann jemand auch erklären, wie Werte für die einzelnen gruppiert werden durch Wort in Reducer-Methode, da es die Summe jedes Werts für das bestimmte Wort ausführt .Wo es überprüft, dass zwei Zählungen für dasselbe Wort vorhanden sind.
Dank
löschen Sie die tmp-Datei im selben Eingangsordner haben muss:) – vefthym
@vethym Ja, du hattest Recht. Es wurde eine tmp-Datei erstellt. Können Sie mir bitte mitteilen, warum diese tmp-Datei erstellt wurde. – Manish