Sum of even and odd numbers in MapReduce using Cloudera Distribution Hadoop(CDH)
Counting the number of even and odd and finding their sum in any language is a piece of cake like in C, C++, Python, Java, etc. MapReduce also uses Java for the writing the program but it is very easy if you know the syntax how to write it. It is the basic of MapReduce. You will first learn how to execute this code similar to “Hello World” program in other programming languages. So here are the steps which show how to write a MapReduce code for Count and Sum of Even and Odd Numbers.
1 2 3 4 5 6 7 8 9
Even 20 // sum of even numbers Even 4 // count of even numbers Odd 25 // sum of odd numbers Odd 5 // count of odd numbers
- First Open Eclipse -> then select File -> New -> Java Project ->Name it EvenOdd -> then Finish.
- Create Three Java Classes into the project. Name them EODriver(having the main function), EOMapper, EOReducer.
- You have to include two Reference Libraries for that:
Right Click on Project -> then select Build Path-> Click on Configue Build Path
In the above figure, you can see the Add External JARs option on the Right Hand Side. Click on it and add the below mention files. You can find these files in /usr/lib/
Mapper Code: You have to copy paste this program into the EOMapper Java Class file.
Reducer Code: You have to copy paste this program into the EOReducer Java Class file.
Driver Code: You have to copy paste this program into the EODriver Java Class file.
- Now you have to make a jar file. Right Click on Project-> Click on Export-> Select export destination as Jar File-> Name the jar File(EvenOdd.jar) -> Click on next -> at last Click on Finish. Now copy this file into the Workspace directory of Cloudera
- Open the terminal on CDH and change the directory to the workspace. You can do this by using “cd workspace/” command. Now, Create a text file(EOFile.txt) and move it to HDFS. For that open terminal and write this code(remember you should be in the same directory as jar file you have created just now).
Now, run this command to copy the file input file into the HDFS.
hadoop fs -put EOFile.txt EOFile.txt
- Now to run the jar file by using following syntax: “hadoop jar JarFilename DriverClassName TextFileName OutPutFolderName”
- After Executing the code, you can see the result in EOOutput file or by writing following command on terminal.
hadoop fs -cat EOOutput/part-00000