Creating Your First Map Reduce Programme

Opening the New Java Project wizard

The New Java Project wizard can be used to create a new java project. There are many ways to open this wizard:
  • By clicking on the File menu and choosing New > Java Project
  • By right clicking anywhere in the Project Explorer and selecting New > Java Project
  • By clicking on the New button ( ) in the Tool bar and selecting Java Project

Using the New Java Project wizard

The New Java Project Wizard has two pages.
On the first page:
  • Enter the Project Name
  • Select the Java Runtime Environment (JRE) or leave it at the default
  • Select the Project Layout which determines whether there would be a separate folder for the sources code and class files. The recommended option is to create separate folders for sources and class files.

    You can click on the Finish button to create the project or click on the Next button to change the java build settings.
    On the second page you can change the Java Build Settings like setting the Project dependency (if there are multiple projects) and adding additional jar files to the build path.

     Writing the Mapper Class

    As we all start up with writing some basic code for map reduce hence we will write a Word Count program which will simply count the number of words in a file and give a out put.

    Now here in the mapper class we write WordCountMapper


    package com.hadoop.training;

    import java.io.IOException;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    import java.util.StringTokenizer;

    public class WordCountMapper  extends Mapper<LongWritable,Text,Text,IntWritable> {

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

    public void map (LongWritable key,Text value, Context context) throws IOException,InterruptedException {

    StringTokenizer itr = new  StringTokenizer(value.toString());

    while (itr.hasMoreTokens()){
    word.set(itr.nextToken());
    context.write(word,one);

    }

    }

    }

    Writing the Reducer Class 

    Now here in the reducer class we write WordCountReducer
     
    package com.hadoop.training;

    import java.io.IOException;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;

    public class  WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>{

        private IntWritable result = new IntWritable();

    public void reduce(Text key,Iterable<IntWritable> value, Context context) throws IOException,InterruptedException {
     int sum = 0;
    for (IntWritable val : value) {
    sum +=val.get();

    }
    result.set(sum);
     context.write(key,result);
    }

    }


    Writing the MapReduce driver class


    Writing the MapReduce driver class as WordCount

    package com.hadoop.training;

    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.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

    public class WordCount {

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

     if (args.length != 2) {
    System.err.println("Usage: MaxTemperature <input path> <output path>");
    System.exit(-1);
    }

    @SuppressWarnings("deprecation")
    Job job = new Job();
    job.setJarByClass(WordCount.class);
    job.setJobName("Word Count");

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

    job.setMapperClass(WordCountMapper.class);
    job.setReducerClass(WordCountReducer.class);

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

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

    Running The Map Reduce programme


    $ hadoop jar WC.jar com.hadoop.training.WordCount hdfs://localhost:8020/user/rajeev/input hdfs://localhost:8020/user/rajeev/output

42 comments:

  1. There are lots of information about latest technology and how to get trained in them, like Hadoop Training Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(Hadoop Training in Chennai). By the way you are running a great blog. Thanks for sharing this.

    Hadoop training institutes in chennai | Hadoop Training Chennai

    ReplyDelete
  2. Cloud is one of the tremendous technology that any company in this world would rely on(Salesforce developer training in chennai). Using this technology many tough tasks can be accomplished easily in no time. Your content are also explaining the same(Salesforce training institutes in chennai). Thanks for sharing this in here. You are running a great blog, keep up this good work.

    ReplyDelete
  3. Appreciating the persistence you put into your blog and detailed information you provide.
    Best Hadoop Training Institute In chennai

    amazon-web-services-training-institute-in-chennai

    ReplyDelete
  4. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    python training in chennai | python training in bangalore

    python online training | python training in pune

    python training in chennai | python training in bangalore

    python training in tambaram |

    ReplyDelete
  5. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    rpa training in Chennai

    rpa training in anna nagar | rpa training in marathahalli

    rpa training in btm | rpa training in kalyan nagar

    rpa training in electronic city | rpa training in chennai

    rpa online training | selenium training in training

    ReplyDelete
  6. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    python training in rajajinagar
    Python training in btm
    Python training in usa

    ReplyDelete
  7. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    DevOps online Training

    ReplyDelete
  8. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.


    angularjs Training in chennai
    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  9. Well done! Pleasant post! This truly helps me to discover the solutions for my inquiry. Trusting, that you will keep posting articles having heaps of valuable data. You're the best! 

    Data Science training in rajaji nagar | Data Science with Python training in chenni
    Data Science training in electronic city | Data Science training in USA
    Data science training in pune | Data science training in kalyan nagar

    ReplyDelete
  10. I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
    German Classes in Chennai
    Java Training in Chennai
    german classes chennai
    german teaching institutes in chennai
    Java Training Institute in Chennai
    Best Java Training Institute in Chennai

    ReplyDelete
  11. I have gone through your blog, it was very much useful for me and because of your blog, and also I gained much unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.
    airport ground staff training courses in chennai
    airport ground staff training in chennai
    ground staff training in chennai

    ReplyDelete
  12. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  13. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.

    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training


    ReplyDelete
  14. Very nice posts. this could not be explained better. Thanks for sharing, Keep up the good work.

    Python training in kalyan nagar
    Java Training in Kalyan Nagar

    ReplyDelete
  15. Thanks for sharing valuable information.It will help everyone.keep Posting!! Machine Learning Training In Bangalore

    ReplyDelete

  16. I have finally found a Worth able content to read. The way you have presented information here is quite impressive. I have bookmarked this page for future use.

    Java training in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Online Training

    ReplyDelete
  17. This is my first time visit here. From the tons of comments ExcelR Machine Learning Course Pune on your articles.I guess I am not only one having all the enjoyment right here!

    ReplyDelete
  18. Think This Is Owsm Post, But If You Check This BITFINEX

    ReplyDelete
  19. Is XM REVIEW Scam? Can They Be Trusted? What Are The Best Brokers? Check Out Our Detailed XM Review And Get The Answers To These Questions And Much More.

    ReplyDelete
  20. Want To Trade Forex With AVATRADE REVIEW ? Read This Blog First To Find Out About The Best Forex Trading Conditions. We Review The Most Popular Forex Brokers And Tell You What You Need To Know.

    ReplyDelete
  21. This post is so interactive and informative.keep update more information...
    SEO Training in Tambaram
    SEO Training in ChennaiS

    ReplyDelete
  22. Star act man administration at more. Decade board face yet value him finally. Ball pull decision tax.

    ReplyDelete