Hadoop - Quick Guide Hadoop - Big Data Overview
Hadoop - Quick Guide Hadoop - Big Data Overview
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Copyright tutorialspoint.com
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
2 of 41
Analytical
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Latency
1 ms - 100 ms
Concurrency
1000 - 100,000
1 - 10
Access Pattern
Reads
Queries
Selective
Unselective
Data Scope
Operational
Retrospective
End User
Customer
Data Scientist
Technology
NoSQL
Limitation
This approach works fine with those applications that process less voluminous data that can be accommodated by
standard database servers, or up to the limit of the processor that is processing the data. But when it comes to
3 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
dealing with huge amounts of scalable data, it is a hectic task to process such data through a single database
bottleneck.
Googles Solution
Google solved this problem using an algorithm called MapReduce. This algorithm divides the task into small parts
and assigns them to many computers, and collects the results from them which when integrated, form the result
dataset.
Big Data
Hadoop
Using the solution provided by Google, Doug Cutting and his team developed an Open Source Project called
HADOOP.
Hadoop runs applications using the MapReduce algorithm, where the data is processed in parallel with others. In
short, Hadoop is used to develop applications that could perform complete statistical analysis on huge amounts of
data.
Big Data
Hadoop Architecture
At its core, Hadoop has two major layers namely:
Processing/Computation layer MapReduce , and
Storage layer HadoopDistributedF ileSystem.
Big Data
MapReduce
MapReduce is a parallel programming model for writing distributed applications devised at Google for efficient
processing of large amounts of data multiterabytedata sets , on large clusters thousandsofnodes of
commodity hardware in a reliable, fault-tolerant manner. The MapReduce program runs on Hadoop which is an
Apache open-source framework.
4 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
distributed file systems. However, the differences from other distributed file systems are significant. It is highly
fault-tolerant and is designed to be deployed on low-cost hardware. It provides high throughput access to
application data and is suitable for applications having large datasets.
Apart from the above-mentioned two core components, Hadoop framework also includes the following two
modules:
Hadoop Common : These are Java libraries and utilities required by other Hadoop modules.
Hadoop YARN : This is a framework for job scheduling and cluster resource management.
Advantages of Hadoop
Hadoop framework allows the user to quickly write and test distributed systems. It is efficient, and it
automatic distributes the data and work across the machines and in turn, utilizes the underlying
parallelism of the CPU cores.
Hadoop does not rely on hardware to provide fault-tolerance and high availability F T HA, rather Hadoop
library itself has been designed to detect and handle failures at the application layer.
Servers can be added or removed from the cluster dynamically and Hadoop continues to operate without
interruption.
Another big advantage of Hadoop is that apart from being open source, it is compatible on all the platforms
since it is Java based.
5 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Pre-installation Setup
Before installing Hadoop into the Linux environment, we need to set up Linux using ssh SecureShell. Follow
the steps given below for setting up the Linux environment.
Creating a User
At the beginning, it is recommended to create a separate user for Hadoop to isolate Hadoop file system from Unix
file system. Follow the steps given below to create a user:
Open the root using the command su.
Create a user from the root account using the command useradd username.
Now you can open an existing user account using the command su username.
Open the Linux terminal and type the following commands to create a user.
$ su
password:
# useradd hadoop
# passwd hadoop
New passwd:
Retype new passwd
Installing Java
Java is the main prerequisite for Hadoop. First of all, you should verify the existence of java in your system using
the command java -version. The syntax of java version command is given below.
6 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
$ java -version
If java is not installed in your system, then follow the steps given below for installing java.
Step 1
Download java JDK < latestversion > X64.tar. gz by visiting the following link
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads1880260.html.
Then jdk-7u71-linux-x64.tar.gz will be downloaded into your system.
Step 2
Generally you will find the downloaded java file in Downloads folder. Verify it and extract the jdk-7u71-linuxx64.gz file using the following commands.
$ cd Downloads/
$ ls
jdk-7u71-linux-x64.gz
$ tar zxf jdk-7u71-linux-x64.gz
$ ls
jdk1.7.0_71
jdk-7u71-linux-x64.gz
Step 3
To make java available to all the users, you have to move it to the location /usr/local/. Open root, and type the
following commands.
$ su
password:
# mv jdk1.7.0_71 /usr/local/
# exit
Step 4
For setting up PATH and JAVA_HOME variables, add the following commands to ~/.bashrc file.
export JAVA_HOME=/usr/local/jdk1.7.0_71
export PATH=PATH:$JAVA_HOME/bin
Now verify the java -version command from the terminal as explained above.
7 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Downloading Hadoop
Download and extract Hadoop 2.4.1 from Apache software foundation using the following commands.
$ su
password:
# cd /usr/local
# wget http://apache.claz.org/hadoop/common/hadoop-2.4.1/
hadoop-2.4.1.tar.gz
# tar xzf hadoop-2.4.1.tar.gz
# mv hadoop-2.4.1/* to hadoop/
# exit
Setting Up Hadoop
You can set Hadoop environment variables by appending the following commands to ~/.bashrc file.
export HADOOP_HOME=/usr/local/hadoop
Before proceeding further, you need to make sure that Hadoop is working fine. Just issue the following command:
$ hadoop version
If everything is fine with your setup, then you should see the following result:
Hadoop 2.4.1
Subversion https://svn.apache.org/repos/asf/hadoop/common -r 1529768
Compiled by hortonmu on 2013-10-07T06:28Z
Compiled with protoc 2.5.0
8 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
It means your Hadoop's standalone mode setup is working fine. By default, Hadoop is configured to run in a
non-distributed mode on a single machine.
Example
Let's check a simple example of Hadoop. Hadoop installation delivers the following example MapReduce jar file,
which provides basic functionality of MapReduce and can be used for calculating, like Pi value, word counts in a
given list of files, etc.
$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.2.0.jar
Let's have an input directory where we will push a few files and our requirement is to count the total number of
words in those files. To calculate the total number of words, we do not need to write our MapReduce, provided the
.jar file contains the implementation for word count. You can try other examples using the same .jar file; just issue
the following commands to check supported MapReduce functional programs by hadoop-mapreduce-examples2.2.0.jar file.
$ hadoop jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduceexamples-2.2.0.jar
Step 1
Create temporary content files in the input directory. You can create this input directory anywhere you would like
to work.
$ mkdir input
$ cp $HADOOP_HOME/*.txt input
$ ls -l input
These files have been copied from the Hadoop installation home directory. For your experiment, you can have
different and large sets of files.
Step 2
Let's start the Hadoop process to count the total number of words in all the files available in the input directory,
as follows:
$ hadoop jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduceexamples-2.2.0.jar
wordcount input ouput
9 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Step 3
Step-2 will do the required processing and save the output in output/part-r00000 file, which you can check by
using:
$cat output/*
It will list down all the words along with their total counts available in all the files available in the input directory.
"AS
4
"Contribution" 1
"Contributor" 1
"Derivative 1
"Legal 1
"License"
1
"License");
1
"Licensor"
1
"NOTICE
1
"Not
1
"Object"
1
"Source
1
"Work
1
"You"
1
"Your")
1
"[]"
1
"control"
1
"printed
1
"submitted"
1
(50%)
1
(BIS),
1
(C)
1
(Don't)
1
(ECCN)
1
(INCLUDING
2
(INCLUDING,
2
.............
10 of 41
06/08/2015 12:36 PM
export
export
export
export
export
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
HADOOP_HDFS_HOME=$HADOOP_HOME
YARN_HOME=$HADOOP_HOME
HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin
HADOOP_INSTALL=$HADOOP_HOME
Now apply all the changes into the current running system.
$ source ~/.bashrc
In order to develop Hadoop programs in java, you have to reset the java environment variables in hadoopenv.sh file by replacing JAVA_HOME value with the location of java in your system.
export JAVA_HOME=/usr/local/jdk1.7.0_71
The following are the list of files that you have to edit to configure Hadoop.
core-site.xml
The core-site.xml file contains information such as the port number used for Hadoop instance, memory
allocated for the file system, memory limit for storing the data, and size of Read/Write buffers.
Open the core-site.xml and add the following properties in between <configuration>, </configuration> tags.
<configuration>
<property>
<name>fs.default.name </name>
<value> hdfs://localhost:9000 </value>
</property>
</configuration>
hdfs-site.xml
The hdfs-site.xml file contains information such as the value of replication data, namenode path, and datanode
paths of your local file systems. It means the place where you want to store the Hadoop infrastructure.
Let us assume the following data.
dfs.replication (data replication value) = 1
11 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Open this file and add the following properties in between the <configuration> </configuration> tags in this file.
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.name.dir</name>
<value>file:///home/hadoop/hadoopinfra/hdfs/namenode </value>
</property>
<property>
<name>dfs.data.dir</name>
<value>file:///home/hadoop/hadoopinfra/hdfs/datanode </value>
</property>
</configuration>
Note: In the above file, all the property values are user-defined and you can make changes according to your
Hadoop infrastructure.
yarn-site.xml
This file is used to configure yarn into Hadoop. Open the yarn-site.xml file and add the following properties in
between the <configuration>, </configuration> tags in this file.
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>
mapred-site.xml
This file is used to specify which MapReduce framework we are using. By default, Hadoop contains a template of
yarn-site.xml. First of all, it is required to copy the file from mapred-site,xml.template to mapred-site.xml
file using the following command.
12 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
$ cp mapred-site.xml.template mapred-site.xml
Open mapred-site.xml file and add the following properties in between the <configuration>,
</configuration>tags in this file.
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
13 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
The following command is used to start dfs. Executing this command will start your Hadoop file system.
$ start-dfs.sh
Big Data
Big Data
14 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Hadoop File System was developed using distributed file system design. It is run on commodity hardware. Unlike
other distributed systems, HDFS is highly faulttolerant and designed using low-cost hardware.
HDFS holds very large amount of data and provides easier access. To store such huge data, the files are stored
across multiple machines. These files are stored in redundant fashion to rescue the system from possible data
losses in case of failure. HDFS also makes applications available to parallel processing.
Features of HDFS
It is suitable for the distributed storage and processing.
Hadoop provides a command interface to interact with HDFS.
The built-in servers of namenode and datanode help users to easily check the status of cluster.
Streaming access to file system data.
HDFS provides file permissions and authentication.
HDFS Architecture
Given below is the architecture of a Hadoop File System.
Big Data
HDFS follows the master-slave architecture and it has the following elements.
Namenode
The namenode is the commodity hardware that contains the GNU/Linux operating system and the namenode
software. It is a software that can be run on commodity hardware. The system having the namenode acts as the
master server and it does the following tasks:
Manages the file system namespace.
Regulates clients access to files.
It also executes file system operations such as renaming, closing, and opening files and directories.
Datanode
The datanode is a commodity hardware having the GNU/Linux operating system and datanode software. For
every node Commodityhardware/System in a cluster, there will be a datanode. These nodes manage the
data storage of their system.
Datanodes perform read-write operations on the file systems, as per client request.
They also perform operations such as block creation, deletion, and replication according to the instructions
of the namenode.
Block
Generally the user data is stored in the files of HDFS. The file in a file system will be divided into one or more
15 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
segments and/or stored in individual data nodes. These file segments are called as blocks. In other words, the
minimum amount of data that HDFS can read or write is called a Block. The default block size is 64MB, but it can
be increased as per the need to change in HDFS configuration.
Goals of HDFS
Fault detection and recovery : Since HDFS includes a large number of commodity hardware, failure of
components is frequent. Therefore HDFS should have mechanisms for quick and automatic fault detection
and recovery.
Huge datasets : HDFS should have hundreds of nodes per cluster to manage the applications having huge
datasets.
Hardware at data : A requested task can be done efficiently, when the computation takes place near the
data. Especially where huge datasets are involved, it reduces the network traffic and increases the
throughput.
After formatting the HDFS, start the distributed file system. The following command will start the namenode as
well as the data nodes as cluster.
$ start-dfs.sh
Step 1
You have to create an input directory.
$ $HADOOP_HOME/bin/hadoop fs -mkdir /user/input
16 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Step 2
Transfer and store a data file from local systems to the Hadoop file system using the put command.
$ $HADOOP_HOME/bin/hadoop fs -put /home/file.txt /user/input
Step 3
You can verify the file using ls command.
$ $HADOOP_HOME/bin/hadoop fs -ls /user/input
Step 1
Initially, view the data from HDFS using cat command.
$ $HADOOP_HOME/bin/hadoop fs -cat /user/output/outfile
Step 2
Get the file from HDFS to the local file system using get command.
$ $HADOOP_HOME/bin/hadoop fs -get /user/output/ /home/hadoop_tp/
17 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
All other files and path names refer to the objects inside HDFS.
Command
Description
-ls <path>
Lists the contents of the directory specified by path, showing the names, permissions,
owner, size and modification date for each entry.
-lsr <path>
Behaves like -ls, but recursively displays entries in all subdirectories of path.
-du <path>
Shows disk usage, in bytes, for all the files which match path; filenames are reported with
the full HDFS protocol prefix.
-dus <path>
Like -du, but prints a summary of disk usage of all files/directories in the path.
-mv <src><dest>
-rm <path>
-rmr <path>
Removes the file or directory identified by path. Recursively deletes any child entries
i. e. , filesorsubdirectoriesofpath .
-put <localSrc>
<dest>
Copies the file or directory from the local file system identified by localSrc to dest within
the DFS.
-copyFromLocal
<localSrc> <dest>
Identical to -put
-moveFromLocal
<localSrc> <dest>
Copies the file or directory from the local file system identified by localSrc to dest within
HDFS, and then deletes the local copy on success.
Copies the file or directory in HDFS identified by src to the local file system path
identified by localDest.
-getmerge <src>
<localDest>
Retrieves all files that match the path src in HDFS, and copies them to a single, merged
file in the local file system identified by localDest.
18 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
-cat <filen-ame>
-copyToLocal
<src> <localDest>
Identical to -get
-moveToLocal
<src> <localDest>
-mkdir <path>
Sets the target replication factor for files identified by path to rep.
-touchz <path>
Creates a file at path containing the current time as a timestamp. Fails if a file already
exists at path, unless the file is already size 0.
-test -[ezd]
<path>
-stat [format]
<path>
Prints information about path. Format is a string which accepts file size in blocks ,
filename , block size , replication , and modification date .
-tail [-f]
<file2name>
-chmod [-R]
mode,mode,...
<path>...
Changes the file permissions associated with one or more objects identified by path....
Performs changes recursively with R. mode is a 3-digit octal mode, or {augo}+/-{rwxX}.
Assumes if no scope is specified and does not apply an umask.
-chown [-R]
[owner][:[group]]
<path>...
Sets the owning user and/or group for files or directories identified by path.... Sets owner
recursively if -R is specified.
Sets the owning group for files or directories identified by path.... Sets group recursively
if -R is specified.
-help
<cmd-name>
Returns usage information for one of the commands listed above. You must omit the
leading '-' character in cmd.
T heactualreplicationfactorwillmovetowardthetargetovertime
HADOOP - MAPREDUCE
MapReduce is a framework using which we can write applications to process huge amounts of data, in parallel, on
large clusters of commodity hardware in a reliable manner.
19 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
What is MapReduce?
MapReduce is a processing technique and a program model for distributed computing based on java. The
MapReduce algorithm contains two important tasks, namely Map and Reduce. Map takes a set of data and
converts it into another set of data, where individual elements are broken down into tuples key/valuepairs.
Secondly, reduce task, which takes the output from a map as an input and combines those data tuples into a
smaller set of tuples. As the sequence of the name MapReduce implies, the reduce task is always performed after
the map job.
The major advantage of MapReduce is that it is easy to scale data processing over multiple computing nodes.
Under the MapReduce model, the data processing primitives are called mappers and reducers. Decomposing a
data processing application into mappers and reducers is sometimes nontrivial. But, once we write an application
in the MapReduce form, scaling the application to run over hundreds, thousands, or even tens of thousands of
machines in a cluster is merely a configuration change. This simple scalability is what has attracted many
programmers to use the MapReduce model.
The Algorithm
Generally MapReduce paradigm is based on sending the computer to where the data resides!
MapReduce program executes in three stages, namely map stage, shuffle stage, and reduce stage.
Map stage : The map or mappers job is to process the input data. Generally the input data is in the
form of file or directory and is stored in the Hadoop file system HDF S. The input file is passed to
the mapper function line by line. The mapper processes the data and creates several small chunks of
data.
Reduce stage : This stage is the combination of the Shuffle stage and the Reduce stage. The
Reducers job is to process the data that comes from the mapper. After processing, it produces a new
set of output, which will be stored in the HDFS.
During a MapReduce job, Hadoop sends the Map and Reduce tasks to the appropriate servers in the
cluster.
The framework manages all the details of data-passing such as issuing tasks, verifying task completion, and
copying data around the cluster between the nodes.
Most of the computing takes place on nodes with data on local disks that reduces the network traffic.
After completion of the given tasks, the cluster collects and reduces the data to form an appropriate result,
and sends it back to the Hadoop server.
Big Data
20 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Writable interface. Additionally, the key classes have to implement the Writable-Comparable interface to facilitate
sorting by the framework. Input and Output types of a MapReduce job: Input <k1, v1> -> map -> <k2, v2>->
reduce -> <k3, v3>Output .
Input
Output
Map
<k1, v1>
list <
k2, v2 >
Reduce
<k2, listv2>
list <
k3, v3 >
Terminology
PayLoad - Applications implement the Map and the Reduce functions, and form the core of the job.
Mapper - Mapper maps the input key/value pairs to a set of intermediate key/value pair.
NamedNode - Node that manages the Hadoop Distributed File System HDF S.
DataNode - Node where data is presented in advance before any processing takes place.
MasterNode - Node where JobTracker runs and which accepts job requests from clients.
SlaveNode - Node where Map and Reduce program runs.
JobTracker - Schedules jobs and tracks the assign jobs to Task tracker.
Task Tracker - Tracks the task and reports status to JobTracker.
Job - A program is an execution of a Mapper and Reducer across a dataset.
Task - An execution of a Mapper or a Reducer on a slice of data.
Task Attempt - A particular instance of an attempt to execute a task on a SlaveNode.
Example Scenario
Given below is the data regarding the electrical consumption of an organization. It contains the monthly electrical
consumption and the annual average for various years.
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
Avg
1979
23
23
43
24
25
26
26
26
26
25
26
25
1980
26
27
28
28
28
30
31
31
31
30
30
30
29
1981
31
32
32
32
33
34
35
36
36
34
34
34
34
21 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
1984
39
38
39
39
39
41
42
43
40
39
38
38
40
1985
38
39
39
39
39
41
41
41
00
40
39
39
45
If the above data is given as input, we have to write applications to process it and produce results such as finding
the year of maximum usage, year of minimum usage, and so on. This is a walkover for the programmers with
finite number of records. They will simply write the logic to produce the required output, and pass the data to the
application written.
But, think of the data representing the electrical consumption of all the largescale industries of a particular state,
since its formation.
When we write applications to process such bulk data,
They will take a lot of time to execute.
There will be a heavy network traffic when we move data from source to network server and so on.
To solve these problems, we have the MapReduce framework.
Input Data
The above data is saved as sample.txtand given as input. The input file looks as shown below.
1979
1980
1981
1984
1985
23
26
31
39
38
23
27
32
38
39
2
28
32
39
39
43
28
32
39
39
24
28
33
39
39
25
30
34
41
41
26
31
35
42
41
26
31
36
43
41
26
31
36
40
00
26
30
34
39
40
25
30
34
38
39
26
30
34
38
39
25
29
34
40
45
Example Program
Given below is the program to the sample data using MapReduce framework.
package hadoop;
import java.util.*;
import java.io.IOException;
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class ProcessUnits
{
//Mapper class
public static class E_EMapper extends MapReduceBase implements
22 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
throws
}
}
//Main function
public static void main(String args[])throws Exception
{
JobConf conf = new JobConf(Eleunits.class);
conf.setJobName("max_eletricityunits");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(E_EMapper.class);
conf.setCombinerClass(E_EReduce.class);
conf.setReducerClass(E_EReduce.class);
23 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
Save the above program as ProcessUnits.java. The compilation and execution of the program is explained
below.
Step 1
The following command is to create a directory to store the compiled java classes.
$ mkdir units
Step 2
Download Hadoop-core-1.2.1.jar, which is used to compile and execute the MapReduce program. Visit the
following link http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/1.2.1 to download the jar. Let
us assume the downloaded folder is /home/hadoop/.
Step 3
The following commands are used for compiling the ProcessUnits.java program and creating a jar for the
program.
$ javac -classpath hadoop-core-1.2.1.jar -d units ProcessUnits.java
$ jar -cvf units.jar -C units/ .
Step 4
The following command is used to create an input directory in HDFS.
$HADOOP_HOME/bin/hadoop fs -mkdir input_dir
Step 5
The following command is used to copy the input file named sample.txtin the input directory of HDFS.
24 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Step 6
The following command is used to verify the files in the input directory.
$HADOOP_HOME/bin/hadoop fs -ls input_dir/
Step 7
The following command is used to run the Eleunit_max application by taking the input files from the input
directory.
$HADOOP_HOME/bin/hadoop jar units.jar hadoop.ProcessUnits input_dir output_dir
Wait for a while until the file is executed. After execution, as shown below, the output will contain the number of
input splits, the number of Map tasks, the number of reducer tasks, etc.
INFO mapreduce.Job: Job job_1414748220717_0002
completed successfully
14/10/31 06:02:52
INFO mapreduce.Job: Counters: 49
File System Counters
FILE:
FILE:
FILE:
FILE:
FILE:
HDFS:
HDFS:
HDFS:
HDFS:
HDFS:
Number
Number
Number
Number
Number
Number
Number
Number
Number
Number
of
of
of
of
of
of
of
of
of
of
bytes read=61
bytes written=279400
read operations=0
large read operations=0
write operations=0
bytes read=546
bytes written=40
read operations=9
large read operations=0
write operations=2 Job Counters
25 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Step 8
The following command is used to verify the resultant files in the output folder.
$HADOOP_HOME/bin/hadoop fs -ls output_dir/
Step 9
The following command is used to see the output in Part-00000 file. This file is generated by HDFS.
$HADOOP_HOME/bin/hadoop fs -cat output_dir/part-00000
26 of 41
34
40
45
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Step 10
The following command is used to copy the output folder from HDFS to the local file system for analyzing.
$HADOOP_HOME/bin/hadoop fs -cat output_dir/part-00000/bin/hadoop dfs get output_dir
/home/hadoop
Important Commands
All Hadoop commands are invoked by the $HADOOP_HOME/bin/hadoop command. Running the Hadoop
script without any arguments prints the description for all commands.
Usage : hadoop [--config confdir] COMMAND
The following table lists the options available and their description.
Options
Description
namenode -format
secondarynamenode
namenode
datanode
dfsadmin
mradmin
fsck
fs
balancer
oiv
fetchdt
jobtracker
pipes
tasktracker
historyserver
27 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
job
queue
version
jar <jar>
DistCp version 2.
Prints the class path needed to get the Hadoop jar and the required
libraries.
daemonlog
GENERIC_OPTIONS
Description
-submit <job-file>
status <job-id>
Prints the map and reduce completion percentage and all job counters.
-kill <job-id>
Prints the events' details received by jobtracker for the given range.
Prints job details, failed and killed tip details. More details about the job
such as successful tasks and task attempts made for each task can be
viewed by specifying the [all] option.
28 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
-list[all]
Displays all jobs. -list displays only jobs which are yet to complete.
-kill-task <task-id>
Kills the task. Killed tasks are NOT counted against failed attempts.
-fail-task <task-id>
HADOOP - STREAMING
Hadoop streaming is a utility that comes with the Hadoop distribution. This utility allows you to create and run
Map/Reduce jobs with any executable or script as the mapper and/or the reducer.
29 of 41
06/08/2015 12:36 PM
#
#
#
#
#
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Make sure this file has execution permission chmod + x/home/expert/hadoop 1.2.1/mapper. py.
Save the mapper and reducer codes in mapper.py and reducer.py in Hadoop home directory. Make sure these files
have execution permission chmod + xmapper. pyandchmod + xreducer. py. As python is indentation
sensitive so the same code can be download from the below link.
30 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
For Example,
./bin/hadoop jar contrib/streaming/hadoop-streaming-1.2.1.jar -input myinput -output
myoutput -mapper /home/expert/hadoop-1.2.1/mapper.py -reducer /home/expert/hadoop1.2.1/reducer.py
Important Commands
Parameters
Options
Description
-input directory/file-name
Required
-output directory-name
Required
-mapper executable or
script or JavaClassName
Required
Mapper executable.
-reducer executable or
script or JavaClassName
Required
Reducer executable.
-file file-name
Optional
31 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
-inputformat
JavaClassName
Optional
Class you supply should return key/value pairs of Text class. If not
specified, TextInputFormat is used as the default.
-outputformat
JavaClassName
Optional
Class you supply should take key/value pairs of Text class. If not
specified, TextOutputformat is used as the default.
-partitioner
JavaClassName
Optional
-combiner
streamingCommand or
JavaClassName
Optional
-cmdenv name=value
Optional
-inputreader
Optional
-verbose
Optional
Verbose output.
-lazyOutput
Optional
-numReduceTasks
Optional
-mapdebug
Optional
-reducedebug
Optional
Follow the steps given below to have Hadoop Multi-Node cluster setup.
Installing Java
32 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Java is the main prerequisite for Hadoop. First of all, you should verify the existence of java in your system using
java -version. The syntax of java version command is given below.
$ java -version
If java is not installed in your system, then follow the given steps for installing java.
Step 1
Download java (JDK - X64.tar.gz) by visiting the following link http://www.oracle.com/technetwork/java/javase
/downloads/jdk7-downloads-1880260.html
Then jdk-7u71-linux-x64.tar.gz will be downloaded into your system.
Step 2
Generally you will find the downloaded java file in Downloads folder. Verify it and extract the jdk-7u71-linuxx64.gz file using the following commands.
$ cd Downloads/
$ ls
jdk-7u71-Linux-x64.gz
$ tar zxf jdk-7u71-Linux-x64.gz
$ ls
jdk1.7.0_71 jdk-7u71-Linux-x64.gz
Step 3
To make java available to all the users, you have to move it to the location /usr/local/. Open the root, and type
the following commands.
$ su
password:
# mv jdk1.7.0_71 /usr/local/
# exit
Step 4
For setting up PATH and JAVA_HOME variables, add the following commands to ~/.bashrc file.
export JAVA_HOME=/usr/local/jdk1.7.0_71
33 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
export PATH=PATH:$JAVA_HOME/bin
Now verify the java -version command from the terminal as explained above. Follow the above process and
install java in all your cluster nodes.
su hadoop
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub tutorialspoint@hadoop-master
ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop_tp1@hadoop-slave-1
ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop_tp2@hadoop-slave-2
chmod 0600 ~/.ssh/authorized_keys
exit
Installing Hadoop
In the Master server, download and install Hadoop using the following commands.
#
#
#
#
#
#
#
mkdir /opt/hadoop
cd /opt/hadoop/
wget http://apache.mesi.com.ar/hadoop/common/hadoop-1.2.1/hadoop-1.2.0.tar.gz
tar -xzf hadoop-1.2.0.tar.gz
mv hadoop-1.2.0 hadoop
chown -R hadoop /opt/hadoop
cd /opt/hadoop/hadoop/
Configuring Hadoop
34 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
You have to configure Hadoop server by making the following changes as given below.
core-site.xml
Open the core-site.xml file and edit it as shown below.
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://hadoop-master:9000/</value>
</property>
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
</configuration>
hdfs-site.xml
Open the hdfs-site.xml file and edit it as shown below.
<configuration>
<property>
<name>dfs.data.dir</name>
<value>/opt/hadoop/hadoop/dfs/name/data</value>
<final>true</final>
</property>
<property>
<name>dfs.name.dir</name>
<value>/opt/hadoop/hadoop/dfs/name</value>
<final>true</final>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
mapred-site.xml
Open the mapred-site.xml file and edit it as shown below.
<configuration>
<property>
<name>mapred.job.tracker</name>
35 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
<value>hadoop-master:9001</value>
</property>
</configuration>
hadoop-env.sh
Open the hadoop-env.sh file and edit JAVA_HOME, HADOOP_CONF_DIR, and HADOOP_OPTS as shown
below.
Note: Set the JAVA_HOME as per your system configuration.
export JAVA_HOME=/opt/jdk1.7.0_17 export HADOOP_OPTS=-Djava.net.preferIPv4Stack=true
export HADOOP_CONF_DIR=/opt/hadoop/hadoop/conf
su hadoop
cd /opt/hadoop
scp -r hadoop hadoop-slave-1:/opt/hadoop
scp -r hadoop hadoop-slave-2:/opt/hadoop
36 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Networking
Add new nodes to an existing Hadoop cluster with some appropriate network configuration. Assume the following
network configuration.
For New node Configuration:
IP address : 192.168.1.103
netmask : 255.255.255.0
hostname : slave3.in
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
On a new node, add "hadoop" user and set password of Hadoop user to "hadoop123" or anything you want by
using the following commands.
useradd hadoop
passwd hadoop
Copy the content of public key into file "$HOME/.ssh/authorized_keys" and then change the permission for
the same by executing the following commands.
cd $HOME
mkdir -p $HOME/.ssh
chmod 700 $HOME/.ssh
cat id_rsa.pub >>$HOME/.ssh/authorized_keys
chmod 644 $HOME/.ssh/authorized_keys
Check ssh login from the master machine. Now check if you can ssh to the new node without a password from the
master.
ssh hadoop@192.168.1.103 or hadoop@slave3
To make the changes effective, either restart the machine or run hostname command to a new machine with the
38 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
Now try to ping the machine with hostnames to check whether it is resolving to IP or not.
On new node machine:
ping master.in
Start HDFS on a newly added slave node by using the following command
./bin/hadoop-daemon.sh start datanode
Step 1
Login to master.
Login to master machine user where Hadoop is installed.
39 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
$ su hadoop
Step 2
Change cluster configuration.
An exclude file must be configured before starting the cluster. Add a key named dfs.hosts.exclude to our
$HADOOP_HOME/etc/hadoop/hdfs-site.xml file. The value associated with this key provides the full path
to a file on the NameNode's local file system which contains a list of machines which are not permitted to connect
to HDFS.
For example, add these lines to etc/hadoop/hdfs-site.xml file.
<property>
<name>dfs.hosts.exclude</name>
<value>/home/hadoop/hadoop-1.2.1/hdfs_exclude.txt</value>
<description>DFS exclude</description>
</property>
Step 3
Determine hosts to decommission.
Each machine to be decommissioned should be added to the file identified by the hdfs_exclude.txt, one domain
name per line. This will prevent them from connecting to the NameNode. Content of the "/home/hadoop
/hadoop-1.2.1/hdfs_exclude.txt" file is shown below, if you want to remove DataNode2.
slave2.in
Step 4
Force configuration reload.
Run the command "$HADOOP_HOME/bin/hadoop dfsadmin -refreshNodes" without the quotes.
$ $HADOOP_HOME/bin/hadoop dfsadmin -refreshNodes
This will force the NameNode to re-read its configuration, including the newly updated excludes file. It will
decommission the nodes over a period of time, allowing time for each node's blocks to be replicated onto
machines which are scheduled to remain active.
On slave2.in, check the jps command output. After some time, you will see the DataNode process is shutdown
automatically.
Step 5
Shutdown nodes.
40 of 41
06/08/2015 12:36 PM
http://www.tutorialspoint.com/cgi-bin/printpage.cgi
After the decommission process has been completed, the decommissioned hardware can be safely shut down for
maintenance. Run the report command to dfsadmin to check the status of decommission. The following
command will describe the status of the decommission node and the connected nodes to the cluster.
$ $HADOOP_HOME/bin/hadoop dfsadmin -report
Step 6
Edit excludes file again.
Once the machines have been decommissioned, they can be removed from the excludes file. Running
"$HADOOP_HOME/bin/hadoop dfsadmin -refreshNodes" again will read the excludes file back into the
NameNode; allowing the DataNodes to rejoin the cluster after the maintenance has been completed, or additional
capacity is needed in the cluster again, etc.
Special Note: If the above process is followed and the tasktracker process is still running on the node, it needs to
be shut down. One way is to disconnect the machine as we did in the above steps. The Master will recognize the
process automatically and will declare as dead. There is no need to follow the same process for removing the
tasktracker because it is NOT much crucial as compared to the DataNode. DataNode contains the data that you
want to remove safely without any loss of data.
The tasktracker can be run/shutdown on the fly by the following command at any point of time.
$ $HADOOP_HOME/bin/hadoop-daemon.sh stop tasktracker $HADOOP_HOME/bin/hadoop-daemon.sh
start tasktracker
41 of 41
06/08/2015 12:36 PM