Running a MapReduce Job
In this step, you will learn how to run a MapReduce job on the data stored in HDFS, leveraging the power of parallel processing to analyze large datasets efficiently.
MapReduce is a programming model for processing large datasets in parallel across a cluster of machines. It consists of two main phases:
-
Map: The input data is split into smaller chunks, and each chunk is processed by a separate task called a "mapper." The mapper processes the data and emits key-value pairs.
-
Reduce: The output from the mappers is sorted and grouped by key, and each group is processed by a separate task called a "reducer." The reducer combines the values associated with each key and produces the final result.
Let's run a simple MapReduce job that counts the occurrences of words in a text file. First, create a Java file named WordCount.java
with the following content:
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);
}
}
Next, compile the Java file:
mkdir ~/wordcount
javac -source 8 -target 8 -classpath $(hadoop classpath) -d ~/wordcount WordCount.java
jar -cvf ~/wordcount.jar -C ~/wordcount .
Finally, run the MapReduce job:
hadoop jar ~/wordcount.jar WordCount /home/hadoop/input/file.txt /home/hadoop/output
The WordCount
class defines a MapReduce job that counts the occurrences of words in a text file. The TokenizerMapper
class tokenizes each line of input text and emits (word, 1) key-value pairs. The IntSumReducer
class sums up the values (counts) for each word and emits the final (word, count) pairs.
The Java file is compiled and packaged into a JAR file, which is then executed using the hadoop jar
command. The input file path (/home/hadoop/input/file.txt
) and output directory path (/home/hadoop/output
) are provided as arguments.