Steps to Perform MapReduce in HortonWorks Sandbox / CloudEra

On CloudEra Desktop:
create folder input
create text files with name State1
Type in State1: 

Copy State1 file and paste it many times

Open Terminal:
hdfs dfs -ls (Should not get anything)

hdfs dfs -copyFromLocal /home/cloudera/Desktop/input    (copies files to Hadoop File System)
hdfs dfs -ls                                                                    (Should see input folder)
hdfs dfs -ls input                                                            (Should see file list inside input folder)

Locate the mapreduce example jar:
Computer -> File System-> usr-> lib-> hadoop-mapreduce
look for hadoop-mapreduce-examples-2.6.0-cdh5.5.0.jar

hadoop jar /usr/lib/hadoop-mapreduce/hadoop-mapreduce-examples-2.6.0-cdh5.5.0.jar wordcount input output


code + add supporting lib from

To Run Mapreduce:
hadoop jar /usr/lib/hadoop-mapreduce/hadoop-mapreduce-examples- wordcount dft dft-output

hdfs dfs -ls
hdfs dfs -ls dft-output
hdfs dfs -cat dft-output/part-r-00000 | less
hadoop dfs -copyToLocal dft-output/part-00000 (c opy output to local directory dft for future reference)
hdfs dfs -copyToLocal dft-output/part-00000 (c opy output to local directory dft for future reference)
hdfs dfs -rm -r dft-output (To remove the files from dft-ouput and trash it)

MapReduce Code:

import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.mapreduce.*;
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 class Map extends 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, Context context) throws IOException, InterruptedException {
       String line = value.toString();
       StringTokenizer tokenizer = new StringTokenizer(line);
       while (tokenizer.hasMoreTokens()) {
           context.write(word, one);

public static class Reduce extends Reducer<Text, IntWritable, Text, 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, new IntWritable(sum));

public static void main(String[] args) throws Exception {
   Configuration conf = new Configuration();

       Job job = new Job(conf, “wordcount”);




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