Word Count
Word Count
Word Count
DRIVER:
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import java.io.*;
import java.util.*;
public class wc{
public static void main(String args[]) throws Exception
{
Configuration conf=new Configuration();
Job job=Job.getInstance(conf,"xyz");
//job.setJarByClass(WordCount.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//job.setNumReduceTasks(2);
job.setMapperClass(wcmapper.class);
job.setReducerClass(wcreducer.class);
//job.setPartitionerClass(wcpartition.class);
//job.setCombinerClass(wccombiner.class);
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
job.setJarByClass(wc.class);
job.waitForCompletion(true);
}
}
MAPPER:
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.LongWritable;
import java.io.*;
public class wcmapper extends
Mapper<LongWritable,Text,Text,LongWritable>
{
LongWritable one=new LongWritable(1);
REDUCER:
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.LongWritable;
import java.io.*;
public class wcreducer extends
Reducer<Text,LongWritable,Text,LongWritable>
{
LongWritable totalWc=new LongWritable();
public void reduce(Text
key,Iterable<LongWritable>values,Context context)throws
IOException,InterruptedException
{
long wordcount=0;
for(LongWritable val:values)
{
wordcount=wordcount+val.get();
}
totalWc.set(wordcount);
context.write(key,totalWc);
}
}
INPUT:
hadoop programs
this is hadoop lab
we are working on map reduce
OUTPUT: