Ravinder Big Data 4 PDF
Ravinder Big Data 4 PDF
(Once you click on “class” Menu Give the File Name as “WordCount.java”)
Step8:
In the Next Step we will write code for WordMapper.java
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;
import org.apache.hadoop.mapreduce.Reducer.Context;
public class WordMapper extends Mapper<LongWritable, Text , Text, IntWritable>{
@Override
public void map(LongWritable key , Text value , Context context )
throws IOException, InterruptedException {
String line= value.toString();
for (String word : line.split(" ")){
if (word.length() > 0) {
//context.write(new Text("total"), new IntWritable(1));
context.write(new Text(word), new IntWritable(1)); //intermediate output
}
}
}
}
Open WordMapper.java and Write the code shown above.
Lets code WordReducer.java
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException{
int wordCount=0;
for(IntWritable value : values){
wordCount +=value.get();
}
context.write(key, new IntWritable(wordCount));}
}
Lets Export the Jar File of the Project
Then click OK and Press Next
Then Press OK in the next Dialog
Finally Click on Finish Button