我正在运行用Java编写的基本WordCount作业的修改版本。原始版本运行完美,但是当我尝试为reducer使用自定义输出键(WritableComparable实现)时,我收到以下错误:
java.io.IOException: Data Corruption in map output. Incorrect fileSize 0
自定义输出键是允许根据出现次数进行进一步排序,而不是按字母顺序排列(尚未实现)。
我的源文件:
public class WordCount implements Tool {
private Configuration config;
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
ToolRunner.run(conf, new WordCount(), otherArgs);
}
public int run(String[] otherArgs) throws Exception {
if (otherArgs.length != 1) {
System.err.println("Specify output folder in mtukuacs/output");
System.exit(2);
}
Job job = new Job(config);
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setCombinerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(WordKey.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job, new Path("/app/poc/SampleData/mtukacs/input/"));
FileOutputFormat.setOutputPath(job, new Path("/app/poc/SampleData/mtukacs/output/" + otherArgs[0]));
job.waitForCompletion(true);
return 0;
}
@Override
public void setConf(Configuration conf) {
config = conf;
}
@Override
public Configuration getConf() {
return config;
}
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
public void map(LongWritable 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 Reduce extends Reducer<Text, IntWritable, WordKey, IntWritable> {
private IntWritable result = new IntWritable();
private WordKey wordKey = new WordKey();
@Override
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);
wordKey.set(key, result);
context.write(wordKey, result);
}
}
public class WordKey implements WritableComparable<WordKey> {
private Text keyText;
private IntWritable keyValue;
public WordKey() {
keyText = new Text();
keyValue = new IntWritable();
}
public void set(Text kt, IntWritable kv) {
keyText = kt;
keyValue = kv;
}
@Override
public void write(DataOutput output) throws IOException {
keyText.write(output);
keyValue.write(output);
}
@Override
public void readFields(DataInput input) throws IOException {
keyText.readFields(input);
keyValue.readFields(input);
}
@Override
public int compareTo(WordKey wordKey) {
return keyValue.compareTo(wordKey.keyValue);
}
@Override
public int hashCode() {
return keyText.hashCode() * 163 + keyValue.hashCode();
}
@Override
public boolean equals(Object object) {
if (object instanceof WordKey) {
WordKey wordKey = (WordKey) object;
return keyText.equals(wordKey.keyText) && keyValue.equals(wordKey.keyValue);
}
return false;
}
@Override
public String toString() {
return keyText.toString();
}
}