Hadoop Part 1: Hello World

Hadoop Hello World:

The Word Count Code:
The word count code is the simplest program to get you started with Map Reduce Framework. The task that a wordcount program performs is as follows:


Given several text files find a count of number of times each word appears in the entire set


It primarily consists of 3 parts:

  1. Driver    : Driver portion of the code contains the configuration details for the Hadoop Job. For example the input path, the output path, number of reducers , mapper class name, reducer class name etc
  2. Mapper  : Role of mapper in word count is to emit <word, 1>  for each word appearing in the document.
  3. Reducer : Role of Reducer in word count is to sum the list of 1's prepared by shuffle and sort phase <word, [1,1,1,1,1,1]>  and emit <word, 6>


It's easier to create an eclipse java project and add relevant hadoop jar files for the code below. 

package com.kush;
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.mapred.*;
import org.apache.hadoop.util.*;
public class WordCount {
 //Mapper Class Begins
  public static class Map extends MapReduceBase implements 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, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
      String line = value.toString();
      StringTokenizer tokenizer = new StringTokenizer(line);
      while (tokenizer.hasMoreTokens()) {
        word.set(tokenizer.nextToken());
        output.collect(word, one);
      }
    }
  }
 //Mapper Class Ends
 //Reducer class begins
  public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
      int sum = 0;
      while (values.hasNext()) {
        sum += values.next().get();
      }
      output.collect(key, new IntWritable(sum));
    }
  }
              //Reducer class ends
              //Driver function begins

  public static void main(String[] args) throws Exception {
    JobConf conf = new JobConf(WordCount.class);
    conf.setJobName("wordcount");
    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(IntWritable.class);
    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);
    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);
    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));
    JobClient.runJob(conf);
  }
              //Driver function ends

}

After resolving the dependencies, we can export a runnable jar file. This jar file can be scp'd to your Hadoop Cluster and can be executed by the following command:

hadoop jar wordcount.jar /user/input /user/output

The output files produced can be listed and opened using the following command:

hadoop fs -ls /user/output
hadoop fs -cat /user/output/part-00000



That's about it! Feel free to ask any questions below!

Comments

Popular posts from this blog

Machine Learning Algorithm timeline

Anomaly Detection