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lLmNsYXNzKTsKam9iLnNldE91dHB1dEtleUNsYXNzKFRleHQuY2xhc3MpOwpqb2Iuc2V0T3V0cHV0dmFsdWVDbGFzcyhJbnRXcml0YWJsZS5jbGFzcyk7CmpvYi5zZXRJbnB1dEZvcm1hdENsYXNzKFRleHRJbnB1dEZvcm1hdC5jbGFzcyk7CmpvYi5zZXRPdXRwdXRGb3JtYXRDbGFzcyhUZXh0SW5wdXRGb3JtYXQuY2xhc3MpOwpQYXRoIG91dHB1dFBhdGggPW5ldyBQYXRoKGFyZ3NbMV0pOwovL0NvbmZpZ3VyaW5nIHRoZSBpbnB1dC9vdXRwdXQgcGF0aCBmcm9tIHRoZSBmaWxlc3lzdGVtIGludG8gdGhlIGpvYgpGaWxlSW5wdXRGb3JtYXQuYWRkSW5wdXRQYXRoKGpvYixuZXcgUGF0aChhcmdzWzBdKSk7CkZpbGVPdXRwdXRGb3JtYXQuc2V0b3V0cHV0UGF0aChqb2IsbmV3IFBhdGgoYXJnc1sxXSkpOwovL2RlbGV0aW5nIHRoZSBvdXRwdXQgcGF0aCBhdXRvbWF0aWNhbGx5IGZyb20gaGRmcyBzbyB0aGF0IHdlIGRvbid0IGhhdmUgdG8gZGVsZXRlIGl0IGV4cGxpY2l0eQpvdXRwdXRQYXRoLmdldEZpbGVTeXN0ZW0oQ29uZikuZGVsZXRlKE91dHB1dFBhdGgpOwovL2V4aXRpbmcgdGhlIGpvYiBvbmx5IGlmIHRoZSBmbGFnIHZhbHVlIGJlY29tZXMgZmFsc2UKU3lzdGVtLmV4aXQoam9iLndhaXRGb3JDb21wbGV0aW9uKHRydWUpPzAgOiAxKTsKfQp9CgoKCg==
import java.io.IOException;
import java.util.StringTokenizer;
import org.appache.hadoop.io.IntWritable;
import org.appache.hadoop.io.LongWritable;
import org.appache.hadoop.io.Text;
import org.appache.hadoop.mapreduce.Mapper;
import org.appache.hadoop.mapreduce.Reducer;
import org.appache.hadoop.conf.configuration;
import org.appache.hadoop.mapreduce.Job;
import org.appache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.appache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.appache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.appache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.appache.hadoop.fs.Path;
public class WordCount
{
public static class Map extends Mapper<Long Writable,Text,IntWritable>
{ public void map(LongWritable Key,Text value,Context context) throws
IOException,InterruptedException{
string line=value.toString();
stringTokenizer tokenizer=new StringTokenizer(line);
while(tokenizer.hasMoreTokens())
{value.set(tokenizer.nexToken());
contex.write(value,new IntWritable(1));
}
}
}
public static class Reduce extends reducerer<Text,IntWritable,Text,IntWritable>
{ public void reduce(TextKey,Iterable<IntWritable>values,Context context)
throws IOException,InterruptedException{
int sum=0;
for(IntWritable x:values)
{
sum+=x.get();
}
context.Write(key,new IntWritable(sum));
}
}
public static void main(String[]args)throws Exception
{Configuration conf=new Configuration();
Job job=new Job(conf,"My Word count program");
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputvalueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextInputFormat.class);
Path outputPath =new Path(args[1]);
//Configuring the input/output path from the filesystem into the job
FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setoutputPath(job,new Path(args[1]));
//deleting the output path automatically from hdfs so that we don't have to delete it explicity
outputPath.getFileSystem(Conf).delete(OutputPath);
//exiting the job only if the flag value becomes false
System.exit(job.waitForCompletion(true)?0 : 1);
}
}