buildtestproject.MyFirstMapReduce.java Source code

Java tutorial

Introduction

Here is the source code for buildtestproject.MyFirstMapReduce.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package buildtestproject;

/*
import java.io.IOException;
import java.util.Iterator;
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.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
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.mapred.Mapper;
import org.apache.hadoop.mapred.Reducer;
*/
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

/**
 *
 * @author shriroop_joshi
 */
public class MyFirstMapReduce {

    public static class TokenizerMapper extends MapReduceBase implements Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        /*
                public void map(Object key, Text value, Reducer.Context context
                ) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());
            context.write(word, one);
        }
                }
        */
        @Override
        public void map(Object key, Text value, OutputCollector<Text, IntWritable> oc, Reporter rprtr)
                throws IOException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                oc.collect(word, one);
                //context.write(word, one);
            }
        }
    }

    public static class IntSumReducer extends MapReduceBase
            implements Reducer<Text, IntWritable, Text, IntWritable> {

        private IntWritable result = new IntWritable();

        /*
                public void reduce(Text key, Iterable<IntWritable> values,
            Reducer.Context context
                ) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
        result.set(sum);
        context.write(key, result);
                }
            
        */
        @Override
        public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> oc,
                Reporter rprtr) throws IOException {
            int sum = 0;
            while (values.hasNext()) {
                sum += values.next().get();
            }
            result.set(sum);
            oc.collect(key, result);
            //context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        //Configuration conf = new Configuration();
        JobConf conf = new JobConf(MyFirstMapReduce.class);
        //Job job = Job.getInstance(conf, "word-count-one");
        conf.setJobName("word-count-one");

        conf.setMapperClass(TokenizerMapper.class);
        conf.setCombinerClass(IntSumReducer.class);
        conf.setReducerClass(IntSumReducer.class);

        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(IntWritable.class);

        conf.setInputFormat(TextInputFormat.class);
        conf.setOutputFormat(TextOutputFormat.class);

        //        job.setJarByClass(MyFirstMapReduce.class);
        //        job.setMapperClass(TokenizerMapper.class);
        //        job.setCombinerClass(IntSumReducer.class);
        //        job.setReducerClass(IntSumReducer.class);
        //        
        //        job.setOutputKeyClass(Text.class);
        //        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(conf, new Path(args[0]));
        FileOutputFormat.setOutputPath(conf, new Path(args[1]));

        //        FileInputFormat.addInputPath(job, new Path(args[0]));
        //        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        JobClient.runJob(conf);

        //        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}