Hadoop word count

0 votes
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));
      }
    }
  }
}
Apr 10, 2023 in Java by Beautiful

edited 5 days ago 8 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP