Saturday, 21 November 2015

Executing Word Count Program in Hadoop

     
By - Abhishek Sengupta (B14003)

      In this post I will be describing how to run a word count java program in Hadoop. The first step would be to install the Oracle VM Virtual-box (I would recommend using the version 5.0 which has the latest updates).  Once this is done the next step would be to install Horton-works Sandbox with HDP 2.3 and import the settings in the virtual-box. Post this the virtual-box landing screen will appear as follows. You will have to go to settings and set the shared clipboard to "bi-directional". Once this is done the machine has to be started.

      After the hadoop session is started (as shown in the screenshot below), enter the address in the systems local browser. Login to sandbox with username: root and password: hadoop



      I have three programs WordCount, SumReducer and WordMapper written in java and the source codes of them are as follows.

WordCount program
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
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 class WordCount {
 public static void main(String[] args) throws Exception {
        if (args.length != 2) {
          System.out.println("usage: [input] [output]");
          System.exit(-1);
        }
   
   
        Job job = Job.getInstance(new Configuration());
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.setMapperClass(WordMapper.class); 
        job.setReducerClass(SumReducer.class);  

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setJarByClass(WordCount.class);

        job.submit();

 }
}

WordMapper program

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;
public class WordMapper extends Mapper<Object, Text, Text, IntWritable> {
 private Text word = new Text();
 private final static IntWritable one = new IntWritable(1);
  
 @Override
 public void map(Object key, Text value, Context context_W) throws IOException, InterruptedException {
  // Convert Text to String
  String line = value.toString();
  // Clean up the string - remove all non alpha characters
  //line = line.replaceAll("[^a-zA-Z\\s]", "").toLowerCase();
  line = line.replaceAll("[^a-zA-Z0-9\\s]", "").toLowerCase();
  // Break line into words for processing
  StringTokenizer wordList = new StringTokenizer(line);
  while (wordList.hasMoreTokens()) {
   word.set(wordList.nextToken());
   context_W.write(word, one);
  }
 }
}

SumReducer program

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;



public class SumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  
 private IntWritable totalWordCount = new IntWritable();
  
 @Override
 public void reduce(Text key, Iterable<IntWritable> values, Context context_W)
            throws IOException, InterruptedException {
  int wordCount = 0;
  Iterator<IntWritable> it=values.iterator();
  while (it.hasNext()) {
   wordCount += it.next().get();
  }
  totalWordCount.set(wordCount);
  context_W.write(key, totalWordCount);
 }
}

We have to first create a directory WCclasses in the root directory using the following commands.
rm -r WCclasses
mkdir WCclasses

Post this we have to go into the WCclasses directory and store the three java programs using the .java extension. We need to use the text editor to store the source code using the vi command. Create three .java files using the vi command as follows.
vi WordCount.java
vi SumReducer.java
vi WordMapper.java

After creating the .java files in the WCclasses directory, get to the root directory and use the following scripts to compile the java programs.

javac -classpath /usr/hdp/2.3.0.0-2557/hadoop/hadoop-common-2.7.1.2.3.0.0-2557.jar:/usr/hdp/2.3.0.0-2557/hadoop-mapreduce/hadoop-mapreduce-client-core-2.7.1.2.3.0.0-2557.jar:/usr/hdp/2.3.0.0-2557/hadoop-mapreduce/commons-cli-1.2.jar -d WCclasses WordMapper.java

javac -classpath /usr/hdp/2.3.0.0-2557/hadoop/hadoop-common-2.7.1.2.3.0.0-2557.jar:/usr/hdp/2.3.0.0-2557/hadoop-mapreduce/hadoop-mapreduce-client-core-2.7.1.2.3.0.0-2557.jar:/usr/hdp/2.3.0.0-2557/hadoop-mapreduce/commons-cli-1.2.jar -d WCclasses SumReducer.java

javac -classpath /usr/hdp/2.3.0.0-2557/hadoop/hadoop-common-2.7.1.2.3.0.0-2557.jar:/usr/hdp/2.3.0.0-2557/hadoop-mapreduce/hadoop-mapreduce-client-core-2.7.1.2.3.0.0-2557.jar:/usr/hdp/2.3.0.0-2557/hadoop-mapreduce/commons-cli-1.2.jari:WCclasses -d WCclasses WordCount.java

The following screenshots depict the process.




On successful compilation of the .java files, .class files will be created in the WCclasses directory. So we will have three .class files - WordCount.class, WordMapper.class and SumReducer.class. The program complied is executed  by the following codes.

hdfs dfs -rm -r /user/hue/wc-out2
hdfs dfs -ls /user/hue
hdfs dfs -ls /user/hue/wc-inp

The jar files so saved for word counting are reflected in the hadoop libraries by the following code

hadoop jar WordCount.jar WordCount /user/hue/wc-inp /user/hue/wc-out2

The following screenshot demonstrates this process.



      The next step is to log into HUE by using the URL and the credentials shown the following screenshot.


      After logging into HUE the input and the output directories have to be defined and pointed out. This is done using the file browser. The wc-out2 is the file output directory the Word count program.



      After successful compilation the output can be seen in the job browser tab. The user is root and we can see the status as SUCCEEDED under the status bar. We can also see that the WordCount.jar file has been created.

   
The Wordcount of the java program complied above is in hadoop, Hue wc-out2/part-r-00000. The screenshots of the output is as follows.









1 comment: