• 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

Solution

  • Is the Hadoop started?
    jps
  • Start Hadoop if not started already
    start-all.sh
  • You can also using
    start-dfs.sh 
  • Is the Hadoop started now?
    jps
  • 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
    cd
  • 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()) {
             word.set(itr.nextToken());
             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();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    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

Statinfer

Statinfer derived from Statistical inference. We provide training in various Data Analytics and Data Science courses and assist candidates in securing placements.

Contact Us

info@statinfer.com

+91- 9676098897

+91- 9494762485

 

Our Social Links

top
© 2020. All Rights Reserved.