0% found this document useful (0 votes)
4 views7 pages

Java Code Summary

The document provides a detailed explanation of a Hadoop MapReduce program for word counting, including the Java code structure, the roles of the mapper and reducer classes, and the main method that configures and runs the job. It also includes a breakdown of two Bash scripts: one for compiling the Java code into a JAR file and another for setting up the Hadoop environment, transferring input data, executing the job, and retrieving output results. Overall, it serves as a comprehensive guide to understanding and executing a Hadoop MapReduce word counting application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Code Summary

The document provides a detailed explanation of a Hadoop MapReduce program for word counting, including the Java code structure, the roles of the mapper and reducer classes, and the main method that configures and runs the job. It also includes a breakdown of two Bash scripts: one for compiling the Java code into a JAR file and another for setting up the Hadoop environment, transferring input data, executing the job, and retrieving output results. Overall, it serves as a comprehensive guide to understanding and executing a Hadoop MapReduce word counting application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

explanation of each part of the Java code you provided, which is a Hadoop MapReduce program

for word counting:


javaCopy code
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

• Import statements: These lines include necessary import statements to use various classes
and libraries required for Hadoop MapReduce.

javaCopy code
public class WordCount {

• WordCount class declaration: This is the main class that contains the MapReduce job
configuration and main method.

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

• TokenizerMapper class definition: This is the mapper class that extends Hadoop's
Mapper class. It defines the map function, which takes as input key-value pairs
(Object, Text) and emits key-value pairs of type (Text, IntWritable).

javaCopy code
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

• one and word variables: These are used to store the value 1 as an IntWritable and
words as Text respectively, which will be emitted by the mapper.

javaCopy code
public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {
• map method declaration: This is the map function, which takes the input key, input value (a
line of text), and a Context object. The map function is responsible for processing each
line of input and emitting key-value pairs.

javaCopy code
StringTokenizer itr = new StringTokenizer([Link]());

• StringTokenizer: It is used to split the input line into individual words.


[Link]() converts the input line (a Text object) into a string for
tokenization.

javaCopy code
while ([Link]()) {
[Link]([Link]());
[Link](word, one);
}

• Tokenization loop: This loop iterates through each word in the input line and emits key-
value pairs for each word. [Link]([Link]()) sets the current word as
the key, and [Link](word, one) emits the key-value pair where the key is
the word and the value is 1 to represent the word count.

The code for IntSumReducer and the main method is similar to what you explained for the
TokenizerMapper. The IntSumReducer class defines the reduce function, and the main
method configures the MapReduce job, sets input and output paths, and specifies the mapper and
reducer classes.

In summary, this code defines a complete Hadoop MapReduce program for word counting. The
TokenizerMapper tokenizes input text lines into words and emits key-value pairs, and the
IntSumReducer aggregates the counts for each word. The main method sets up and runs the
MapReduce job.

Certainly! Let's continue explaining the remaining parts of the code line by line:
javaCopy code
public static class IntSumReducer extends Reducer<Text, IntWritable, Text,
IntWritable> {

• IntSumReducer class definition: This is the reducer class that extends Hadoop's
Reducer class. It defines the reduce function, which takes key-value pairs of type
(Text, Iterable<IntWritable>) as input and emits key-value pairs of type
(Text, IntWritable) as output.
javaCopy code
private IntWritable result = new IntWritable();

• result variable: This IntWritable variable is used to store the final count for each
word before emitting it as output.

javaCopy code
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {

• reduce method declaration: This is the reduce function, which takes the input key, an
iterable collection of values, and a Context object. The goal of this function is to sum up
the values associated with the same key (word) and emit the word along with its total
count.

javaCopy code
int sum = 0;
for (IntWritable val : values) {
sum += [Link]();
}
[Link](sum);
[Link](key, result);

• Reducing logic: In this loop, it iterates through the values for a specific key (word) and
accumulates the counts (which are IntWritable values) into the sum variable. After
summing all the counts, it sets the result IntWritable to the total count (sum) and
then emits the key-value pair where the key is the word (key) and the value is the total
count (result). This is how the reducer calculates the total count for each unique word.

javaCopy code
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = [Link](conf, "word count");
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](job, new Path(args[0]));
[Link](job, new Path(args[1]));
[Link]([Link](true) ? 0 : 1);
}

• main method: This is the main entry point for your MapReduce program. Here's a
breakdown of what it does:
• It starts by creating a Hadoop Configuration object to configure the job.

• Then, it creates a new Job instance with a given configuration and job name ("word
count").

• [Link]([Link]); specifies the JAR file that


contains your MapReduce program's compiled code.

• [Link]([Link]); sets the mapper


class.

• [Link]([Link]); sets the combiner


class (a local reducer that runs on the mapper nodes to optimize data transfer).

• [Link]([Link]); sets the reducer class.

• [Link]([Link]); and
[Link]([Link]); specify the output
key and value types.

• [Link](job, new Path(args[0]));


specifies the input path (the first command-line argument).

• [Link](job, new Path(args[1]));


specifies the output path (the second command-line argument).

• Finally, [Link]([Link](true) ? 0 : 1);


submits the job to Hadoop and exits the program with a status code based on the
job's success (0 for success, 1 for failure).

This main method sets up the entire MapReduce job, including configuration, input and output
paths, and the classes to use for mapping, reducing, and combining. When you run this program, it
orchestrates the entire MapReduce process.

[Link]
break down the Bash script line by line:

1. #!/bin/bash

• This line is called a shebang, and it specifies the interpreter that should be used to
execute the script, in this case, it's /bin/bash .
2. user_name=$(whoami)

• This line uses the whoami command to get the current username and stores it in the
user_name variable.
3. sourceCodeName='WordCount'
• This line assigns the string 'WordCount' to the variable sourceCodeName .
This variable is used to specify the name of the source code file without the
extension.
4. export HADOOP_CLASSPATH=$(hadoop classpath)

• This line sets the HADOOP_CLASSPATH environment variable by running the


hadoop classpath command and capturing its output. The
HADOOP_CLASSPATH is used to specify the classpath required for Hadoop to find
classes and libraries.
5. mkdir ${sourceCodeName}_classes

• This line creates a directory with the name ${sourceCodeName}_classes . $


{sourceCodeName} is replaced with the value 'WordCount' , so this
command creates a directory named WordCount_classes . This directory will
be used to store the compiled Java class files.
6. javac -classpath ${HADOOP_CLASSPATH} -d $
{sourceCodeName}_classes ${sourceCodeName}.java

• This line compiles the Java source code file ${sourceCodeName}.java using
the javac compiler. Here's what each part of the command does:
• -classpath ${HADOOP_CLASSPATH} : Specifies the classpath
required for compilation. It includes the Hadoop libraries.
• -d ${sourceCodeName}_classes : Specifies the output directory for
the compiled class files. In this case, it's the WordCount_classes
directory created earlier.
• ${sourceCodeName}.java : Specifies the source code file to compile,
which is [Link] .
7. jar -cvf ${sourceCodeName}.jar -C $
{sourceCodeName}_classes/ .

• This line creates a JAR (Java Archive) file named ${sourceCodeName}.jar


from the compiled class files in the ${sourceCodeName}_classes directory.
Here's what each part of the command does:
• -cvf : Options for creating the JAR file. -c indicates creating a new JAR
file, -v enables verbose output, and -f specifies the JAR file's name.
• ${sourceCodeName}.jar : Specifies the name of the JAR file to create.
• -C ${sourceCodeName}_classes/ . : This part specifies that the
JAR should include all the files in the ${sourceCodeName}_classes
directory. -C is used to change to the specified directory before including its
contents, and . includes all files and subdirectories.
In summary, this Bash script compiles a Java source code file named [Link] , creates
a directory for the compiled class files, and then packages those class files into a JAR file named
[Link] . The classpath for compilation includes the necessary Hadoop libraries to build
a Hadoop MapReduce program.

[Link]
each line of this Bash script and explain its purpose:

1. user_name=$(whoami)

• This line uses the whoami command to get the current username and stores it in the
user_name variable.
2. sourceCodeName='WordCount'

• This line assigns the string 'WordCount' to the sourceCodeName variable.


This variable is used to specify the name of the Hadoop job and related directories.
3. hdfs dfs -mkdir /user/$user_name/$sourceCodeName

• This line creates a directory in HDFS under the user's home directory with a
structure like /user/$user_name/$sourceCodeName . It's used to organize
HDFS directories for your Hadoop job.
4. hdfs dfs -mkdir /user/$user_name/$sourceCodeName/input

• This line creates an input directory under the previously created directory in
HDFS. It's used to store input data for your Hadoop job.
5. hdfs dfs -put input/* /user/$user_name/$sourceCodeName/input

• This line copies the contents of the input directory (on the local file system) to the
HDFS input directory you just created. It's used to transfer the input data from the
local file system to HDFS so that your Hadoop job can process it.
6. hadoop jar ${sourceCodeName}.jar $sourceCodeName
/user/$user_name/$sourceCodeName/input
/user/$user_name/$sourceCodeName/output

• This line runs a Hadoop MapReduce job. Here's a breakdown:


• hadoop jar : This command runs a Hadoop job with the specified JAR
file.
• ${sourceCodeName}.jar : This specifies the JAR file containing your
MapReduce program.
• $sourceCodeName : This is the name of your Hadoop job (the main class).
• /user/$user_name/$sourceCodeName/input : This is the input
directory path in HDFS.
• /user/$user_name/$sourceCodeName/output : This is the output
directory path in HDFS where the job's results will be stored.
7. The lines commented out with # are not executed when the script runs, so they are for
reference or documentation purposes.
8. hdfs dfs -get /user/$user_name/$sourceCodeName/output .

• This line copies the output directory from HDFS back to the local file system, using
the hdfs dfs -get command. The output of your Hadoop job will be retrieved
and stored in the current directory on the local file system.

In summary, this Bash script is used to prepare your Hadoop environment, transfer input data from
the local file system to HDFS, run a Hadoop MapReduce job, retrieve the results from HDFS back
to the local file system, and organize directories for the Hadoop job.

You might also like