规划好了就开始安装Hadoop,如前言中所说使用Cloudera的Hadoop发布版安装Hadoop是十分方便的,首先当然是在每台主机上一个干净的操作系统(我用的是Ubuntu 8.04,用户设为Hadoop,其它的版本应该差不多),然后就是安装Hadoop了(这样安装的是Hadoop-0.20,也可以安装Hadoop- 0.18的版本,反正安装步骤都差不多。注意,不能同时启用Hadoop-0.20和Hadoop-0.18)。由于每台机器安装步骤都一样,这里就写出了一台主机的安装步骤,主要分为以下几个步骤:
sudo vi /etc/apt/sources.list.d/cloudera.list
#稳定版(Hadoop-0.18)
#deb http://archive.cloudera.com/debian hardy-stable contrib
#deb-src http://archive.cloudera.com/debian hardy-stable contrib
#测试版(Hadoop-0.20)
deb http://archive.cloudera.com/debian hardy-testing contrib
deb-src http://archive.cloudera.com/debian hardy-testing contrib
sudo apt-get install curl
curl -s http://archive.cloudera.com/debian/archive.key | sudo apt-key add -
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install hadoop-0.20 hadoop-0.20-conf-pseudo
安装好这几台主机的Hadoop环境之后,就要对它们进行分布式运行模式的部署了,首先是设置它们之间的互联。
Hadoop环境中的互联是指各主机之间网络畅通,机器名与IP地址之间解析正常,可以从任一主机ping通其它主机的主机名。注意,这里指的是主机名,即在Hadoop-01主机上可以通过命令ping Hadoop-02来ping通Hadoop-02主机(同理,要求这几台主机都能相互Ping通各自的主机名)。可以通过在各主机的/etc /hosts文件来实现,具体设置如下:
sudo vi /etc/hosts
127.0.0.1 localhost
10.x.253.201 hadoop-01 hadoop-01
10.x.253.202 hadoop-02 hadoop-02
10.x.253.203 hadoop-03 hadoop-03
10.x.253.204 hadoop-04 hadoop-04
10.x.3.30 firehare-303 firehare-303
将每个主机的hosts文件都改成上述设置,这样就实现了主机间使用主机名互联的要求。
注:如果深究起来,并不是所有的主机都需要知道Hadoop环境中其它主机主机名的。其实只是作为主节点的主机(如NameNode、 JobTracker),需要在该主节点hosts文件中加上Hadoop环境中所有机器的IP地址及其对应的主机名,如果该台机器作Datanode 用,则只需要在hosts文件中加上本机和主节点机器的IP地址与主机名即可(至于JobTracker主机是否也要同NameNode主机一样加上所有机器的IP和主机名,本人由于没有环境,不敢妄言,但猜想是要加的,如果哪位兄弟有兴趣,倒是不妨一试)。在这里只是由于要作测试,作为主节点的主机可能会改变,加上本人比较懒,所以就全加上了。:)
Hadoop要求所有机器上hadoop的部署目录结构要相同,并且都有一个相同用户名的帐户。由于这里采用的是Cloudera发布的Hadoop包,所以并不需要这方面的设置,大家了解一下即可。
在 Hadoop 分布式环境中,主节点(NameNode、JobTracker) 需要通过 SSH 来启动和停止从节点(DataNode、TeskTracker)上的各类进程。因此需要保证环境中的各台机器均可以通过 SSH 登录访问,并且主节点用 SSH 登录从节点时,不需要输入密码,这样主节点才能在后台自如地控制其它结点。可以将各台机器上的 SSH 配置为使用无密码公钥认证方式来实现。 Ubuntu上的SSH协议的开源实现是OpenSSH, 缺省状态下是没有安装的,如需使用需要进行安装。
安装OpenSSH很简单,只需要下列命令就可以把openssh-client和openssh-server给安装好:
sudo apt-get install ssh
首先在Hadoop-01机器上执行以下命令:
hadoop@hadoop-01:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):(在这里直接回车)
Enter same passphrase again:(在这里直接回车)
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
9d:42:04:26:00:51:c7:4e:2f:7e:38:dd:93:1c:a2:d6 hadoop@hadoop-01
上述命令将为主机hadoops-01上的当前用户hadoop生成其密钥对,该密钥对被保存在/home/hadoop/.ssh/id_rsa 文件中,同时命令所生成的证书以及公钥也保存在该文件所在的目录中(在这里是:/home/hadoop/.ssh),并形成两个文件 id_rsa,id_rsa.pub。然后将 id_rsa.pub 文件的内容复制到每台主机(其中包括本机hadoop-01)的/home/hadoop/.ssh/authorized_keys文件的尾部,如果该文件不存在,可手工创建一个。
注意:id_rsa.pub 文件的内容是长长的一行,复制时不要遗漏字符或混入了多余换行符。
从 hadoop-01 分别向 hadoop-01, hadoop-04, firehare-303 发起 SSH 连接请求,确保不需要输入密码就能 SSH 连接成功。注意第一次 SSH 连接时会出现类似如下提示的信息:
The authenticity of host [hadoop-01] can't be established. The key fingerprint is:
c8:c2:b2:d0:29:29:1a:e3:ec:d9:4a:47:98:29:b4:48 Are you sure you want to continue connecting (yes/no)?
请输入 yes, 这样 OpenSSH 会把连接过来的这台主机的信息自动加到 /home/hadoop/.ssh/know_hosts 文件中去,第二次再连接时,就不会有这样的提示信息了。
Hadoop的JAVA_HOME是在文件/etc/conf/hadoop-env.sh中设置,具体设置如下:
sudo vi /etc/conf/hadoop-env.sh
export JAVA_HOME="/usr/lib/jvm/java-6-sun"
Hadoop的核心配置文件是/etc/hadoop/conf/core-site.xml,具体配置如下:
fs.default.name
hdfs://hadoop-01:8020
hadoop.tmp.dir
/var/lib/hadoop-0.20/cache/${user.name}
Hadoop的分布式环境设置主要是通过文件/etc/hadoop/conf/hdfs-site.xml来实现的,具体配置如下:
dfs.replication
3
dfs.permissions
false
dfs.name.dir
/var/lib/hadoop-0.20/cache/hadoop/dfs/name
Hadoop的分布式计算是采用了Map/Reduce算法,该算法环境的设置主要是通过文件/etc/hadoop/conf/mapred-site.xml来实现的,具体配置如下:
mapred.job.tracker
hadoop-01:8021
首先设置主节点,编辑/etc/hadoop/conf/masters文件,如下所示:
hadoop-01
然后是设置从节点,编辑/etc/hadoop/conf/slaves文件,如下所示:
hadoop-02
hadoop-03
hadoop-04
firehare-303
从节点上的Hadoop设置很简单,只需要将主节点上的Hadoop设置,复制一份到从节点上即可。
scp -r /etc/hadoop/conf hadoop-02:/etc/hadoop
scp -r /etc/hadoop/conf hadoop-03:/etc/hadoop
scp -r /etc/hadoop/conf hadoop-04:/etc/hadoop
scp -r /etc/hadoop/conf firehare-303:/etc/hadoop
在启动Hadoop之前还要做最后一个准备工作,那就是格式化分布式文件系统,这个只需要在主节点做就行了,具体如下:
/usr/lib/hadoop-0.20/bin/hadoop namenode -format
启动Hadoop可以通过以下命令来实现:
/usr/lib/hadoop-0.20/bin/start-all.sh
注意:该命令是没有加sudo的,如果加了sudo就会提示出错信息的,因为root用户并没有做无验证ssh设置。以下是输出信息,注意hadoop-03是故意没接的,所以出现No route to host信息。
hadoop@hadoop-01:~$ /usr/lib/hadoop-0.20/bin/start-all.sh
namenode running as process 4836. Stop it first.
hadoop-02: starting datanode, logging to /usr/lib/hadoop-0.20/bin/../logs/hadoop-hadoop-datanode-hadoop-02.out
hadoop-04: starting datanode, logging to /usr/lib/hadoop-0.20/bin/../logs/hadoop-hadoop-datanode-hadoop-04.out
firehare-303: starting datanode, logging to /usr/lib/hadoop-0.20/bin/../logs/hadoop-hadoop-datanode-usvr-303b.out
hadoop-03: ssh: connect to host hadoop-03 port 22: No route to host
hadoop-01: secondarynamenode running as process 4891. Stop it first.
jobtracker running as process 4787. Stop it first.
hadoop-02: starting tasktracker, logging to /usr/lib/hadoop-0.20/bin/../logs/hadoop-hadoop-tasktracker-hadoop-02.out
hadoop-04: starting tasktracker, logging to /usr/lib/hadoop-0.20/bin/../logs/hadoop-hadoop-tasktracker-hadoop-04.out
firehare-303: starting tasktracker, logging to /usr/lib/hadoop-0.20/bin/../logs/hadoop-hadoop-tasktracker-usvr-303b.out
hadoop-03: ssh: connect to host hadoop-03 port 22: No route to host
这样Hadoop就正常启动了!
Hadoop架设好了,接下来就是要对其进行测试,看看它是否能正常工作,具体代码如下:
hadoop@hadoop-01:~$ hadoop-0.20 fs -mkdir input
hadoop@hadoop-01:~$ hadoop-0.20 fs -put /etc/hadoop-0.20/conf/*.xml input
hadoop@hadoop-01:~$ hadoop-0.20 fs -ls input
Found 6 items
-rw-r--r-- 3 hadoop supergroup 3936 2010-03-11 08:55 /user/hadoop/input/capacity-scheduler.xml
-rw-r--r-- 3 hadoop supergroup 400 2010-03-11 08:55 /user/hadoop/input/core-site.xml
-rw-r--r-- 3 hadoop supergroup 3032 2010-03-11 08:55 /user/hadoop/input/fair-scheduler.xml
-rw-r--r-- 3 hadoop supergroup 4190 2010-03-11 08:55 /user/hadoop/input/hadoop-policy.xml
-rw-r--r-- 3 hadoop supergroup 536 2010-03-11 08:55 /user/hadoop/input/hdfs-site.xml
-rw-r--r-- 3 hadoop supergroup 266 2010-03-11 08:55 /user/hadoop/input/mapred-site.xml
hadoop@hadoop-01:~$ hadoop-0.20 jar /usr/lib/hadoop-0.20/hadoop-*-examples.jar grep input output 'dfs[a-z.]+'
10/03/11 14:35:57 INFO mapred.FileInputFormat: Total input paths to process : 6
10/03/11 14:35:58 INFO mapred.JobClient: Running job: job_201003111431_0001
10/03/11 14:35:59 INFO mapred.JobClient: map 0% reduce 0%
10/03/11 14:36:14 INFO mapred.JobClient: map 33% reduce 0%
10/03/11 14:36:20 INFO mapred.JobClient: map 66% reduce 0%
10/03/11 14:36:26 INFO mapred.JobClient: map 66% reduce 22%
10/03/11 14:36:36 INFO mapred.JobClient: map 100% reduce 22%
10/03/11 14:36:44 INFO mapred.JobClient: map 100% reduce 100%
10/03/11 14:36:46 INFO mapred.JobClient: Job complete: job_201003111431_0001
10/03/11 14:36:46 INFO mapred.JobClient: Counters: 19
10/03/11 14:36:46 INFO mapred.JobClient: Job Counters
10/03/11 14:36:46 INFO mapred.JobClient: Launched reduce tasks=1
10/03/11 14:36:46 INFO mapred.JobClient: Rack-local map tasks=4
10/03/11 14:36:46 INFO mapred.JobClient: Launched map tasks=6
10/03/11 14:36:46 INFO mapred.JobClient: Data-local map tasks=2
10/03/11 14:36:46 INFO mapred.JobClient: FileSystemCounters
10/03/11 14:36:46 INFO mapred.JobClient: FILE_BYTES_READ=100
10/03/11 14:36:46 INFO mapred.JobClient: HDFS_BYTES_READ=12360
10/03/11 14:36:46 INFO mapred.JobClient: FILE_BYTES_WRITTEN=422
10/03/11 14:36:46 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=204
10/03/11 14:36:46 INFO mapred.JobClient: Map-Reduce Framework
10/03/11 14:36:46 INFO mapred.JobClient: Reduce input groups=4
10/03/11 14:36:46 INFO mapred.JobClient: Combine output records=4
10/03/11 14:36:46 INFO mapred.JobClient: Map input records=315
10/03/11 14:36:46 INFO mapred.JobClient: Reduce shuffle bytes=124
10/03/11 14:36:46 INFO mapred.JobClient: Reduce output records=4
10/03/11 14:36:46 INFO mapred.JobClient: Spilled Records=8
10/03/11 14:36:46 INFO mapred.JobClient: Map output bytes=86
10/03/11 14:36:46 INFO mapred.JobClient: Map input bytes=12360
10/03/11 14:36:46 INFO mapred.JobClient: Combine input records=4
10/03/11 14:36:46 INFO mapred.JobClient: Map output records=4
10/03/11 14:36:46 INFO mapred.JobClient: Reduce input records=4
10/03/11 14:36:46 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
10/03/11 14:36:46 INFO mapred.FileInputFormat: Total input paths to process : 1
10/03/11 14:36:46 INFO mapred.JobClient: Running job: job_201003111431_0002
10/03/11 14:36:47 INFO mapred.JobClient: map 0% reduce 0%
10/03/11 14:36:56 INFO mapred.JobClient: map 100% reduce 0%
10/03/11 14:37:08 INFO mapred.JobClient: map 100% reduce 100%
10/03/11 14:37:10 INFO mapred.JobClient: Job complete: job_201003111431_0002
10/03/11 14:37:11 INFO mapred.JobClient: Counters: 18
10/03/11 14:37:11 INFO mapred.JobClient: Job Counters
10/03/11 14:37:11 INFO mapred.JobClient: Launched reduce tasks=1
10/03/11 14:37:11 INFO mapred.JobClient: Launched map tasks=1
10/03/11 14:37:11 INFO mapred.JobClient: Data-local map tasks=1
10/03/11 14:37:11 INFO mapred.JobClient: FileSystemCounters
10/03/11 14:37:11 INFO mapred.JobClient: FILE_BYTES_READ=100
10/03/11 14:37:11 INFO mapred.JobClient: HDFS_BYTES_READ=204
10/03/11 14:37:11 INFO mapred.JobClient: FILE_BYTES_WRITTEN=232
10/03/11 14:37:11 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=62
10/03/11 14:37:11 INFO mapred.JobClient: Map-Reduce Framework
10/03/11 14:37:11 INFO mapred.JobClient: Reduce input groups=1
10/03/11 14:37:11 INFO mapred.JobClient: Combine output records=0
10/03/11 14:37:11 INFO mapred.JobClient: Map input records=4
10/03/11 14:37:11 INFO mapred.JobClient: Reduce shuffle bytes=0
10/03/11 14:37:11 INFO mapred.JobClient: Reduce output records=4
10/03/11 14:37:11 INFO mapred.JobClient: Spilled Records=8
10/03/11 14:37:11 INFO mapred.JobClient: Map output bytes=86
10/03/11 14:37:11 INFO mapred.JobClient: Map input bytes=118
10/03/11 14:37:11 INFO mapred.JobClient: Combine input records=0
10/03/11 14:37:11 INFO mapred.JobClient: Map output records=4
10/03/11 14:37:11 INFO mapred.JobClient: Reduce input records=4
不难看出,上述测试已经成功,这说明Hadoop部署成功,能够在上面进行Map/Reduce分布性计算了。