import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
Copyright By PowCoder代写 加微信 powcoder
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;
public class MinMax {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, “word count”);
job.setJarByClass(MinMax.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntMinMaxReducer.class);
job.setReducerClass(IntMinMaxReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
public static class TokenizerMapper
extends Mapper
private final static IntWritable counter = new IntWritable(0);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
counter.set( Integer.parseInt(itr.nextToken()) );
context.write(word, counter);
public static class IntMinMaxReducer
extends Reducer
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable
Context context
) throws IOException, InterruptedException {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (IntWritable val : values) {
if ( val.get()> max )
max = val.get();
if ( val.get() < min )
min = val.get();
result.set(max);
context.write(key, result);
result.set(min);
context.write(key, result);
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com