Aim: Design a distributed application using MapReduce under Hadoop for:
        a) Character counting in a given text file.
        b) Counting no. of occurrences of every word in a given text file.

Requirement:
  1. Hadoop - http://www.professionalcipher.com/2018/01/how-to-install-hadoop-on-ubuntu-1604.html
  2. Eclipse - http://www.professionalcipher.com/2018/03/installation-of-eclipse-on-ubuntu.html
  3. Create sample.txt and write any thing in it and paste in home folder. Example: Online platform for education, java, design, programs, assignments, projects, source code, software, information technology, books, engineering stuff. 
ERROR:
If you are getting error regarding java.lang.UnsupportedClassVersionError
Then go to the project -> Properties -> Java Compiler -> Uncheck Compiliance for execution environment -> Select 1.6 enivronment ->Apply Close and run again the project


Steps:
# Open terminal
whoami

# It will display your user name, we will use it later.

# Open eclipse->new java project->project name exp5a ->new class-> CharMap

# Add following code in that class

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class CharMap extends Mapper<LongWritable, Text, Text, IntWritable> 
{
 public void map(LongWritable key, Text value, Context context)
   throws IOException, InterruptedException {
  String line = value.toString();
  char[] carr = line.toCharArray();
  for (char c : carr) {
   System.out.println(c);
   context.write(new Text(String.valueOf(c)), new IntWritable(1));
  }
 }
}

# Save the file

# It will display some errors, so we are going to import three jar files in our project.

# Copy hadoop-mapreduce-client-core-2.7.1.jar from ~/hadoop/share/hadoop/mapreduce directory # In eclipse-> right click on exp5a project- >paste

# Right click on pasted hadoop-mapreduce-client-core-2.7.1.jar-> Buid path-> add to buid path

#Copy hadoop-common-2.7.1.jar from ~/hadoop/share/hadoop/common directory

# In eclipse-> right click on exp5a project- >paste

# Right click on pasted hadoop-common-2.7.1.jar-> Buid path-> add to buid path

#Copy commons-cli-1.2.jar from ~/hadoop/share/hadoop/common/lib directory

# In eclipse-> right click on exp5a project- >paste

# Right click on pasted commons-cli-1.2.jar-> Buid path-> add to buid path

# In eclipse->right click on project exp5a ->new class-> CharReduce

# Add following code in that class

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

public class CharReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
 public void reduce(Text key, Iterable<IntWritable> values, Context context)
   throws IOException, InterruptedException {
  int count = 0;
  IntWritable result = new IntWritable();
  for (IntWritable val : values) {
   count += val.get();
   result.set(count);
  }
  context.write(key, result);
 }
}

# Save the file

# In eclipse->right click on project exp5a ->new class-> CharCount

# Add following code in that class

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 CharCount 
{
 public static void main(String[] args) throws Exception
 { 
  // TODO Auto-generated method stub
  Configuration conf = new Configuration();
  @SuppressWarnings("deprecation")
  Job job = new Job(conf, "Charcount");
  job.setJarByClass(CharCount.class);
  job.setMapperClass(CharMap.class);
  job.setReducerClass(CharReduce.class);
  job.setInputFormatClass(TextInputFormat.class);
  job.setOutputFormatClass(TextOutputFormat.class);
  job.setMapOutputKeyClass(Text.class);
  job.setMapOutputValueClass(IntWritable.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);
 }
}

# Save the file

# In eclipse->Right click on project exp5a-> export->java->jar file->next-> select the export destination -> /home/ your_user_name /exp5a.jar -> next -> next -> select main class ->browse - > CharCount - > finish

# exp5a.jar file will be created in your home folder

# Open terminal

# Now Start NameNode daemon and DataNode daemon:
~/hadoop/sbin/start-dfs.sh

# Make the HDFS directories required to execute MapReduce jobs
~/hadoop/bin/hdfs dfs -mkdir /user
~/hadoop/bin/hdfs dfs -mkdir /user/your_user_name

# Put sample.txt file in hdfs
~/hadoop/bin/hdfs dfs -put ~/sample.txt input_data

# Perform MapReduce job
~/hadoop/bin/hadoop jar ~/exp5a.jar input_data output_data

# Output
~/hadoop/bin/hdfs dfs -cat output_data/*
#
 Our task is done, so delete the distributed files (input_data & output_data)
~/hadoop/bin/hdfs dfs -rm -r input_data output_data

#STOP HADOOP
~/hadoop/sbin/stop-dfs.sh


Counting no. of occurrences of every word in a given text file.

Reference: https://sl6it.files.wordpress.com/2015/12/mapreduce-wordcount-steps1.pdf

WordCount.java

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;
import org.apache.hadoop.util.GenericOptionsParser;

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();
  String[] otherArgs = new GenericOptionsParser(conf, args)
    .getRemainingArgs();
  if (otherArgs.length < 2) {
   System.err.println("Usage: wordcount <in> [<in>...] <out>");
   System.exit(2);
  }
  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);
  for (int i = 0; i < otherArgs.length - 1; ++i) {
   FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
  }
  FileOutputFormat.setOutputPath(job, new Path(
    otherArgs[otherArgs.length - 1]));
  System.exit(job.waitForCompletion(true) ? 0 : 1);
 }
}