1.
Map Reduce program to count the number of occurrences of each word in a
given input text.
[Link]
package wordcount;
import [Link]. *;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
public class driver
{
public static void main(String args[]) throws IOException
{
JobConf conf=new JobConf([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](conf, new Path(args[0]));
[Link](conf,new Path(args[1]));
[Link](conf);
}
}
[Link]
package wordcount;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class mapper extends MapReduceBase implements Mapper<LongWritable, Text, Text,
IntWritable> {
// Static final variable for the count of 1
private final static IntWritable one = new IntWritable(1);
// Reusable Text object to hold each word
private Text word = new Text();
// The map function
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output,
Reporter reporter)
throws IOException {
// Convert the input value (line of text) to a string
String line = [Link]();
// Tokenize the line into words
StringTokenizer tokenizer = new StringTokenizer(line);
// Iterate through the tokens (words)
while ([Link]()) {
// Set the current word into the Text object
[Link]([Link]());
// Collect the word and emit (word, 1) as key-value pairs
[Link](word, one);
}
}
}
[Link]
package wordcount;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class reducer extends MapReduceBase implements Reducer<Text, IntWritable, Text,
IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable>
output,
Reporter reporter) throws IOException {
int sum = 0;
// Sum up the counts for each word
while ([Link]()) {
sum += [Link]().get();
}
// Emit the word with the total count
[Link](key, new IntWritable(sum));
}
}
Steps to run
1. Create a New File named [Link]
2. Copy the Below code and Paste inside [Link] and save that File.
export JAVA_HOME=$(readlink -f $(which javac) | awk 'BEGIN {FS="/bin"} {print $1}')
export PATH=$(echo $PATH):$(pwd)/bin
export CLASSPATH=$(hadoop classpath)
3. Execute the [Link] File using following command source [Link].
4. Verify JAVA_HOME variable to be set to Java Path and PATH variable has your USN
Hadoop Folder.
If any previous PATH set to Hadoop Folder remove that inside .bashrc file.
5. Verify Hadoop is Installed or not by executing hadoop [Link] command gives
Information about
Hadoop command then Hadoop is Successfully Installed.
6. Create a folder word count and move to that folder.
7. Make the [Link] , [Link] and [Link] files.
8. Compile all java files ([Link] [Link] [Link])
javac -d . *.java
9. Set driver class in manifest
echo Main-Class: [Link] > [Link]
10. Create an executable jar file
jar cfm [Link] [Link] word count/*.class
11. [Link] is input file for Oddeven create Input File
echo “hello good morning, hello have a nice day” > [Link]
12. Run the jar file
hadoop jar [Link] [Link] output
13. To see the Output
cat output/*