In this article I would like to introduce the usage of Hadoop in your local machine. I will also give a hint about how to use it in a cluster provided you have access to the same in my future post in a similar topic. First, you have to Login to a Unix or Linux machine. If you have one, good and great. Else you can use Amazon’s Linux Server for free if you choose its free tier machine. I will write another tutorial on the usage and access to AWS Linux Server in a future post. The following are the Steps to follow to SETUP your system.
- Install Java Developer Kit latest Suite – Java 8 by first downloading the tar file
- tar zxvf jdk-8.xx-linux-x64.tar.gz or YOUR Tar File
- tar zxvf hadoop-2.6.0.tar.gz
- Paste the following to the .bashrc file
export HADOOP_USER_NAME= any name
- Go to any terminal window and type hadoop
- If you’re not getting any error you are good wit the installation!!! Congrats!
When you are using Hadoop using something other then Java a good way would be to use the Streaming Mode. It would generally take inputs in the form of standard user input or output which can be provided indirectly. What that exactly means will be explained soon. Just keep in mind that the Hadoop Streaming process uses two distinct programs having your Map and Reduce functions. Unlike mincemeat’s Map function it has actually a dedicated program which performs the Map task. You can check mincemeat’s Map Reduce implementation here. Similarly, it has a dedicated file to perform the Reduce task.
Please note that in real world with multiple machines in clusters to perform your task, you can also use one Map and more than one Reduce implementing files.
So, now you’re ready with your Hadoop. What next? Yup, you gotta write your Map implementer and Reduce implementer as well. In this case we will assume that we need only one Reduce implementer and the problem to solve will be to print the sum of a given set of numbers from a list with every number in a newline and also, print the count of the numbers. Let’s break this solution into easy verbal steps as follows :
- Write a Map function(program) that will print “1 <number>” for every number that it encounters in every line and not just distinct numbers but all occurrences.
- Write a Reduce function(program) that will read every line of output from the Map function’s(program’s) in the form “Key Value” where Key will be 1 and the Value will be the number.
- The Reduce function(program), as the next step, will aggregate all the unique Values and add up their count of 1s which are their Keys.
- Once the Keys are counted for every distinct Value all you need to do is display the SUM of the Keys and the SUM of the (Values*Keys).
While, the former will give you the count of all the numbers, the latter will give you the total of all the values. You can also count all the occurrences of all numbers before making them distinct pairs to print the sum before even counting the number of numbers. This is so simple right? So, let’s get our hands dirty on the code.
Programming part is over. What next ? Running the programs!!! How ? Not directly with console input and console output but THROUGH HADOOP. This can be done by writing a small command in bash or writing a Script instead for the same. Let’s see what it is!
hadoop jar $HADOOP_INSTALL/share/hadoop/tools/lib/hadoop-streaming-2.6.0.jar– Ctrl+C
-input YOUR_INPUT_FILE/DIRECTORY -output YOUR_OUTPUT_DIRECTORY -mapper map.py -reducer reduce.py
Copy and paste this in a file with the extension sh and change privileges of the file with the following commands :
Paste your code in the file by :
vi myFirstHadoopScript.sh– Ctrl+V :wq Return
chmod 755 myFirstHadoopScript.sh
Your Program should run properly with the output as :
count – your count
sum – your sum
Wow!!!! You are a Hadoop Rookie now 😀
To keep testing your program during the development phase you can check for the correctness by
cat YOUR_TEXT_FILE | YOUR MAP PROGRAM | YOUR REDUCE PROGRAM
You ALSO need to keep removing the OUTPUT_DIRECTORY for every execution or you can use a new one instead. Otherwise you will get bad errors!