我是Hadoop的新手。我一直在嘗試運行着名的「WordCount」程序 - 該程序在列表中的字數爲 使用Hadoop-0.20.2的文件。 我正在使用單節點羣集。運行使用Hadoop0.20.2的標準「WordCount」程序時沒有得到正確的輸出
Follwing是我的程序:
import java.io.File; import java.io.IOException; import java.util。*;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf。 ; import org.apache.hadoop.io。; import org.apache.hadoop.mapreduce。*; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
公共類字計數{
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
while (values.hasNext()) {
++sum ;
}
context.write(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "wordcount");
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setJarByClass(WordCount.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setMapperClass(Map.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setNumReduceTasks(5);
job.waitForCompletion(true);
}
}
假設輸入文件是A.TXT當運行使用Hadoop-0.20這個程序,其具有以下內容
ABCDABCD
。 2(爲了清楚起見未示出命令),來的輸出是 A 1 A 1 B 1 B! C 1 C 1 D! d 1
這是wrong.The實際輸出應該是: A 2 B 2 的C 2 d 2
這 「字計數」 的程序是非常標準的program.I真的不知道是什麼這段代碼錯了。 我已經正確編寫了像mapred-site.xml,core-site.xml等所有配置文件的內容。
如果有人能幫助我,我將不勝感激。
謝謝。