• No products in the cart.

301.2.2-Map Reduce for word count


LAB: Map Reduce for word count

  • Dataset User_Reviews/User_Reviews.txt
  • Move the data to hdfs
  • Write a word count program to count the frequency of each word
  • Take the final output inside a text file


  • Is the Hadoop started?
  • Start Hadoop if not started already
  • You can also using
  • Is the Hadoop started now?
  • check your files on hdfs
    hadoop fs -ls /
  • Bring the data onto hadoop HDFS
    hadoop fs -copyFromLocal /home/hduser/datasets/User_Reviews/User_Reviews.txt /user_review_hdfs
  • Check the data file on HDFS
    hadoop fs -ls /
  • check your current working directory
  • Goto hadoop bin
    cd /usr/local/hadoop/bin/

It is imporatant to make your PWD(present working directory) as $hadoop/bin

Open an editor with a file name WordCount.java

sudo gedit WordCount.java

Copy the below java code, paste in your file and save your file

import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {
    public static class TokenizerMapper
        extends Mapper<Object, Text, Text, IntWritable>{

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public void map(Object key, Text value, Context context)
        throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
             context.write(word, one);
    public static class IntSumReducer
            extends Reducer<Text,IntWritable,Text,IntWritable> {
            private IntWritable result = new IntWritable();

            public void reduce(Text key, Iterable<IntWritable> values,
            Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            context.write(key, result);

    public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);

To compile this program, use the below command

hadoop com.sun.tools.javac.Main WordCount.java

Create the jar file which is named as wc.jar

jar cf wc.jar WordCount*.class

Run wordcount program, output will be automaically routed to

hadoop jar wc.jar WordCount /user_review_hdfs /usr/review_count_out

Have a look at the output

hadoop fs -cat /usr/review_count_out/part-r-00000

Part of the output

We can take the output to a text file

mkdir /home/hduser/Output/

hadoop fs -cat /usr/review_count_out/part-r-00000 >> /home/hduser/Output/review_word_count.txt

22nd March 2018
0 responses on "301.2.2-Map Reduce for word count"

Leave a Message


Statinfer derived from Statistical inference is a company that focuses on the data science training and R&D.We offer training on Machine Learning, Deep Learning and Artificial Intelligence using tools like R, Python and TensorFlow

Contact Us

We Accept

Our Social Links

How to Become a Data Scientist?

© 2020. All Rights Reserved.