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
- How to Execute WordCount Program in MapReduce using Cloudera Distribution Hadoop(CDH)
- How to find top-N records using MapReduce
- MapReduce Program - Weather Data Analysis For Analyzing Hot And Cold Days
- MapReduce Program - Finding The Average Age of Male and Female Died in Titanic Disaster
- MapReduce - Combiners
- Distributed Cache in Hadoop MapReduce
- How MapReduce handles data query ?
- MapReduce Job Execution
- Job Initialisation in MapReduce
- How Job runs on MapReduce
- How MapReduce completes a task?
- MapReduce - Understanding With Real-Life Example
- Hadoop - Mapper In MapReduce
- Hadoop MapReduce - Data Flow
- MapReduce Architecture
- How to Execute Character Count Program in MapReduce Hadoop?
- Matrix Multiplication With 1 MapReduce Step
- Hadoop - mrjob Python Library For MapReduce With Example
- Import and Export Data using SQOOP
- Deleting Files in HDFS using Python Snakebite
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.