You are on page 1of 2

While(itr.

hasmoretokens)
word.set(itr.nestToken());
context.write(new Text(integer.ToString(word.getlength()),one)

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class ClassWordCount{

public static class MyMapper extends Mapper<LongWritable, Text, IntWritable,


IntWritable> {
private final static IntWritable return_one = new IntWritable(1);
private IntWritable w_Length = new IntWritable();

public void map(LongWritable outputkey, Text value, Context context)


throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
for (;tokenizer.hasMoreTokens();) {
w_Length.set(tokenizer.nextToken().length());
context.write(w_Length, return_one);
}
}
}

public static class Int_Reduce


extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable> {
private IntWritable output = new IntWritable();

public void reduce(IntWritable outputkey, Iterable<IntWritable> values,


Context context
) throws IOException, InterruptedException {
int mysum = 0;
for (IntWritable val : values) {
mysum = mysum + val.get();
}
output.set(mysum);
context.write(outputkey, output);
}
}

public static void main(String[] args) throws Exception {


Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(ClassWordCount.class);
job.setMapperClass(MyMapper.class);
job.setCombinerClass(Int_Reduce.class);
job.setReducerClass(Int_Reduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
for (int i = 0; i < otherArgs.length - 1; ++i) {
Path mypath = new Path(otherArgs[i]);
FileInputFormat.addInputPath(job,mypath );
}
FileOutputFormat.setOutputPath(job,
new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

You might also like