import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
public class PaperCount {
public static class Map extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text authorYear = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line, "|");
// Extract fields from input line
String[] authors = tokenizer.nextToken().split(",");
String title = tokenizer.nextToken();
String conference = tokenizer.nextToken();
String year = tokenizer.nextToken();
// Emit key-value pairs for each author in the list
for (String author : authors) {
authorYear.set(year + "|" + author.trim());
context.write(authorYear, one);
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
// Output author-year count if it is greater than zero
if (sum > 0) {
context.write(key, new IntWritable(sum));
}
}
}
}