测试:修订间差异
无编辑摘要 |
无编辑摘要 |
||
(未显示2个用户的23个中间版本) | |||
第94行: | 第94行: | ||
127.0.0.1 localhost | 127.0.0.1 localhost | ||
10. | 10.x.253.201 hadoop-01 hadoop-01 | ||
10. | 10.x.253.202 hadoop-02 hadoop-02 | ||
10. | 10.x.253.203 hadoop-03 hadoop-03 | ||
10. | 10.x.253.204 hadoop-04 hadoop-04 | ||
10. | 10.x.3.30 firehare-303 firehare-303</pre> | ||
将每个主机的hosts文件都改成上述设置,这样就实现了主机间使用主机名互联的要求。<br> | 将每个主机的hosts文件都改成上述设置,这样就实现了主机间使用主机名互联的要求。<br> | ||
<br> | <br> | ||
注:如果深究起来,并不是所有的主机都需要知道Hadoop环境中其它主机主机名的。其实只是作为主节点的主机(如NameNode、JobTracker),需要在该主节点hosts文件中加上Hadoop环境中所有机器的IP地址及其对应的主机名,如果该台机器作Datanode用,则只需要在hosts文件中加上本机和主节点机器的IP地址与主机名即可(至于JobTracker主机是否也要同NameNode主机一样加上所有机器的IP和主机名,本人由于没有环境,不敢妄言,但猜想是要加的,如果哪位兄弟有兴趣,倒是不妨一试)。在这里只是由于要作测试,作为主节点的主机可能会改变,加上本人比较懒,所以就全加上了。:) | |||
==== 计算帐号设置 ==== | ==== 计算帐号设置 ==== | ||
第109行: | 第109行: | ||
Hadoop要求所有机器上hadoop的部署目录结构要相同,并且都有一个相同用户名的帐户。由于这里采用的是Cloudera发布的Hadoop包,所以并不需要这方面的设置,大家了解一下即可。 | Hadoop要求所有机器上hadoop的部署目录结构要相同,并且都有一个相同用户名的帐户。由于这里采用的是Cloudera发布的Hadoop包,所以并不需要这方面的设置,大家了解一下即可。 | ||
==== SSH设置 ==== | ==== SSH设置 ==== | ||
在 Hadoop 分布式环境中,主节点(NameNode、JobTracker) 需要通过 SSH 来启动和停止从节点(DataNode、TeskTracker)上的各类进程。因此需要保证环境中的各台机器均可以通过 SSH 登录访问,并且主节点用 SSH 登录从节点时,不需要输入密码,这样主节点才能在后台自如地控制其它结点。可以将各台机器上的 SSH 配置为使用无密码公钥认证方式来实现。 Ubuntu上的SSH协议的开源实现是OpenSSH, 缺省状态下是没有安装的,如需使用需要进行安装。<br> | |||
===== 安装OpenSSH ===== | |||
安装OpenSSH很简单,只需要下列命令就可以把openssh-client和openssh-server给安装好: | |||
<pre>sudo apt-get install ssh | |||
</pre> | |||
===== 设置OpenSSH的无密码公钥认证<br> ===== | |||
首先在Hadoop-01机器上执行以下命令:<br> | |||
<pre>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</pre> | |||
上述命令将为主机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 文件的内容是长长的一行,复制时不要遗漏字符或混入了多余换行符。'''<br> | |||
===== 无密码公钥SSH的连接测试<br> ===== | |||
从 hadoop-01 分别向 hadoop-01, hadoop-04, firehare-303 发起 SSH 连接请求,确保不需要输入密码就能 SSH 连接成功。注意第一次 SSH 连接时会出现类似如下提示的信息: | |||
<pre>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)? | |||
</pre> | |||
请输入 yes, 这样 OpenSSH 会把连接过来的这台主机的信息自动加到 /home/hadoop/.ssh/know_hosts 文件中去,第二次再连接时,就不会有这样的提示信息了。 <br> | |||
==== 设置主节点的Hadoop<br> ==== | |||
===== 设置JAVA_HOME<br> ===== | |||
Hadoop的JAVA_HOME是在文件/etc/conf/hadoop-env.sh中设置,具体设置如下:<br> | |||
<pre>sudo vi /etc/conf/hadoop-env.sh | |||
export JAVA_HOME="/usr/lib/jvm/java-6-sun" | |||
</pre> | |||
===== Hadoop的核心配置<br> ===== | |||
Hadoop的核心配置文件是/etc/hadoop/conf/core-site.xml,具体配置如下:<br> | |||
<pre><?xml version="1.0"?> | |||
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> | |||
<configuration> | |||
<property> | |||
<name>fs.default.name</name> | |||
<!-- | |||
<value>hdfs://localhost:8020</value> | |||
--> | |||
<value>hdfs://hadoop-01:8020</value> | |||
</property> | |||
<property> | |||
<name>hadoop.tmp.dir</name> | |||
<value>/var/lib/hadoop-0.20/cache/${user.name}</value> | |||
</property> | |||
</configuration> | |||
</pre> | |||
===== 设置Hadoop的分布式存储环境<br> ===== | |||
Hadoop的分布式环境设置主要是通过文件/etc/hadoop/conf/hdfs-site.xml来实现的,具体配置如下:<br> | |||
<pre><?xml version="1.0"?> | |||
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> | |||
<configuration> | |||
<property> | |||
<name>dfs.replication</name> | |||
<!-- | |||
<value>1</value> | |||
--> | |||
<value>3</value> | |||
</property> | |||
<property> | |||
<name>dfs.permissions</name> | |||
<value>false</value> | |||
</property> | |||
<property> | |||
<!-- specify this so that running 'hadoop namenode -format' formats the right dir --> | |||
<name>dfs.name.dir</name> | |||
<value>/var/lib/hadoop-0.20/cache/hadoop/dfs/name</value> | |||
</property> | |||
</configuration> | |||
</pre> | |||
===== 设置Hapoop的分布式计算环境<br> ===== | |||
Hadoop的分布式计算是采用了Map/Reduce算法,该算法环境的设置主要是通过文件/etc/hadoop/conf/mapred-site.xml来实现的,具体配置如下: | |||
<pre><?xml version="1.0"?> | |||
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> | |||
<configuration> | |||
<property> | |||
<name>mapred.job.tracker</name> | |||
<!-- | |||
<value>localhost:8021</value> | |||
--> | |||
<value>hadoop-01:8021</value> | |||
</property> | |||
</configuration> | |||
</pre> | |||
===== 设置Hadoop的主从节点<br> ===== | |||
首先设置主节点,编辑/etc/hadoop/conf/masters文件,如下所示:<br> | |||
<pre>hadoop-01 | |||
</pre> | |||
然后是设置从节点,编辑/etc/hadoop/conf/slaves文件,如下所示:<br> | |||
<pre>hadoop-02 | |||
hadoop-03 | |||
hadoop-04 | |||
firehare-303 | |||
</pre> | |||
==== 设置从节点上的Hadoop<br> ==== | |||
从节点上的Hadoop设置很简单,只需要将主节点上的Hadoop设置,复制一份到从节点上即可。<br> | |||
<pre>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 | |||
</pre> | |||
=== 启动Hadoop<br> === | |||
==== 格式化分布式文件系统 ==== | |||
在启动Hadoop之前还要做最后一个准备工作,那就是格式化分布式文件系统,这个只需要在主节点做就行了,具体如下: <br> | |||
<pre>/usr/lib/hadoop-0.20/bin/hadoop namenode -format | |||
</pre> | |||
==== 启动Hadoop服务<br> ==== | |||
启动Hadoop可以通过以下命令来实现: | |||
<pre>/usr/lib/hadoop-0.20/bin/start-all.sh</pre> | |||
注意:该命令是没有加sudo的,如果加了sudo就会提示出错信息的,因为root用户并没有做无验证ssh设置。以下是输出信息,注意hadoop-03是故意没接的,所以出现No route to host信息。<br> | |||
<pre>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 | |||
</pre> | |||
这样Hadoop就正常启动了!<br> | |||
=== 测试Hadoop<br> === | |||
Hadoop架设好了,接下来就是要对其进行测试,看看它是否能正常工作,具体代码如下: | |||
<pre>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 08:55:43 INFO mapred.FileInputFormat: Total input paths to process : 6 | |||
10/03/11 08:55:44 INFO mapred.JobClient: Running job: job_201003110836_0001 | |||
10/03/11 08:55:45 INFO mapred.JobClient: map 0% reduce 0% | |||
10/03/11 08:55:57 INFO mapred.JobClient: map 33% reduce 0% | |||
10/03/11 08:56:06 INFO mapred.JobClient: map 33% reduce 11% | |||
10/03/11 08:56:07 INFO mapred.JobClient: map 66% reduce 11% | |||
10/03/11 08:56:12 INFO mapred.JobClient: map 100% reduce 11% | |||
10/03/11 08:56:21 INFO mapred.JobClient: map 100% reduce 22% | |||
10/03/11 09:04:06 INFO mapred.JobClient: Task Id : attempt_201003110836_0001_m_000002_0, Status : FAILED | |||
Too many fetch-failures | |||
10/03/11 09:04:06 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn | |||
10/03/11 09:04:06 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn | |||
10/03/11 09:04:22 INFO mapred.JobClient: map 100% reduce 27% | |||
10/03/11 09:06:50 INFO mapred.JobClient: Task Id : attempt_201003110836_0001_m_000003_0, Status : FAILED | |||
Too many fetch-failures | |||
10/03/11 09:06:50 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn | |||
10/03/11 09:06:50 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn | |||
10/03/11 09:07:03 INFO mapred.JobClient: map 100% reduce 100% | |||
10/03/11 09:07:05 INFO mapred.JobClient: Job complete: job_201003110836_0001 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Counters: 18 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Job Counters | |||
10/03/11 09:07:05 INFO mapred.JobClient: Launched reduce tasks=1 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Launched map tasks=8 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Data-local map tasks=8 | |||
10/03/11 09:07:05 INFO mapred.JobClient: FileSystemCounters | |||
10/03/11 09:07:05 INFO mapred.JobClient: FILE_BYTES_READ=100 | |||
10/03/11 09:07:05 INFO mapred.JobClient: HDFS_BYTES_READ=12360 | |||
10/03/11 09:07:05 INFO mapred.JobClient: FILE_BYTES_WRITTEN=422 | |||
10/03/11 09:07:05 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=204 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Map-Reduce Framework | |||
10/03/11 09:07:05 INFO mapred.JobClient: Reduce input groups=4 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Combine output records=4 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Map input records=315 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Reduce shuffle bytes=49 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Reduce output records=4 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Spilled Records=8 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Map output bytes=86 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Map input bytes=12360 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Combine input records=4 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Map output records=4 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Reduce input records=4 | |||
10/03/11 09:07:05 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same. | |||
10/03/11 09:07:05 INFO mapred.FileInputFormat: Total input paths to process : 1 | |||
10/03/11 09:07:05 INFO mapred.JobClient: Running job: job_201003110836_0002 | |||
10/03/11 09:07:06 INFO mapred.JobClient: map 0% reduce 0% | |||
10/03/11 09:07:13 INFO mapred.JobClient: map 100% reduce 0% | |||
10/03/11 09:07:19 INFO mapred.JobClient: map 100% reduce 100% | |||
10/03/11 09:07:21 INFO mapred.JobClient: Job complete: job_201003110836_0002 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Counters: 18 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Job Counters | |||
10/03/11 09:07:21 INFO mapred.JobClient: Launched reduce tasks=1 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Launched map tasks=1 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Data-local map tasks=1 | |||
10/03/11 09:07:21 INFO mapred.JobClient: FileSystemCounters | |||
10/03/11 09:07:21 INFO mapred.JobClient: FILE_BYTES_READ=100 | |||
10/03/11 09:07:21 INFO mapred.JobClient: HDFS_BYTES_READ=204 | |||
10/03/11 09:07:21 INFO mapred.JobClient: FILE_BYTES_WRITTEN=232 | |||
10/03/11 09:07:21 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=62 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Map-Reduce Framework | |||
10/03/11 09:07:21 INFO mapred.JobClient: Reduce input groups=1 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Combine output records=0 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Map input records=4 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Reduce shuffle bytes=0 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Reduce output records=4 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Spilled Records=8 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Map output bytes=86 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Map input bytes=118 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Combine input records=0 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Map output records=4 | |||
10/03/11 09:07:21 INFO mapred.JobClient: Reduce input records=4 | |||
</pre> | |||
不难看出,上述测试已经成功,这说明Hadoop部署成功,能够在上面进行Map/Reduce分布性计算了。 |
2010年3月11日 (四) 09:55的最新版本
利用 Cloudera 部署 Hadoop
前言
Hadoop 是一个实现了 MapReduce 计算模型的开源分布式并行编程框架。MapReduce的概念来源于Google实验室,它是一个简化并行计算的编程模型,适用于大规模集群上的海量数据处理,目前最成功的应用是分布式搜索引擎。随着2007年底该模式Java开源实现项目Apache Hadoop的出现,使得程序员可以轻松地编写分布式并行程序,并将其运行于计算机集群上,完成海量数据的计算。近两年尤其是今年国内外采用MapReduce模型的应用也逐渐丰富起来,如像NTT KDDI和中国移动这类的电信公司采用该模型分析用户信息,优化网络配置;美国供电局采用该模型来分析电网现状;包括VISA和JP摩根在内的金融公司采用该模型来分析股票数据;包括Amazon和ebay在内的零售商和电子商务公司也开始采用该模型;甚至部分生物公司也采用该模型来进行DNA测序和分析。然而Hadoop安装、部署、管理的难度非常大,这使用很多用户对Hadoop望而却步,好在这种情况不久就得到了改善,Cloudera提供了非常简单的Hadoop的发布版本,能够十分方便地对Hadoop进行安装、部署和管理,这导致目前大约有75%的Hadoop新用户使用Cloudera。
规划
运行模式
Hadoop有三种运行模式:单机(非分布)运行模式、伪分布运行模式和分布式运行模式。其中前两种运行模式体现不了 Hadoop 分布式计算的优势,并没有什么实际意义(当然它们对程序的测试及调试还是很有帮助的),因此在这里还是采用实际环境中使用的分布式运行模式来部署。
主机规划
在这里拟采用三台主机搭建Hadoop环境,由于后期还需要测试增删主机及跨网段主机对Hadoop环境的影响,特将Hadoop主机规划如下: Hadoop-01 10.137.253.201 Hadoop-02 10.137.253.202 Hadoop-03 10.137.253.203 准备后期加入的测试主机 Hadoop-04 10.137.253.204 Firehare-303 10.10.3.30 准备后期加入的跨网段测试主机
Hadoop环境规划
对于Hadoop来说,最主要的是两个内容,一是分布式文件系统HDFS,一是MapReduce计算模型。在分布式文件系统HDFS看来,节点分为NameNode 和DataNode,其中NameNode只有一个,DataNode可以是很多;在MapReduce计算模型看来,节点又可分为JobTracker和 TaskTracker,其中JobTracker只有一个,TaskTracker可以是很多。因此在实际的Hadoop环境中通常有两台主节点,一台作为NameNode(I/O节点??),一台作为JobTracker(管理节点??),剩下的都是从节点,同时当做DataNode和TaskTracker使用。当然也可以将NameNode和JobTracker安装在一台主节点上。由于测试机数量有限,所以在这里是让Hadoop-01做为Namenode和Jobtracker,其它主机则作为DataNode和TaskTracker(如果Hadoop环境中主机数量很多的话,还是建议将Namenode和JobTracker部署到不同的主机,以提高计算的性能)。具体规划如下:
HDFS: Hadoop-01 NameNode Hadoop-02 DataNode Hadoop-03 DataNode Hadoop-04 DataNode Firehare-303 DataNode MapReduce: Hadoop-01 JobTracker Hadoop-02 TaskTracker Hadoop-03 TaskTracker Hadoop-04 TaskTracker Firehare-303 TaskTracker
安装
规划好了就开始安装Hadoop,如前言中所说使用Cloudera的Hadoop发布版安装Hadoop是十分方便的,首先当然是在每台主机上一个干净的操作系统(我用的是Ubuntu 8.04,用户设为Hadoop,其它的版本应该差不多),然后就是安装Hadoop了(这样安装的是Hadoop-0.20,也可以安装Hadoop-0.18的版本,反正安装步骤都差不多。注意,不能同时启用Hadoop-0.20和Hadoop-0.18)。由于每台机器安装步骤都一样,这里就写出了一台主机的安装步骤,主要分为以下几个步骤:
设置Cloudera的源
- 生成Cloudera源文件(这里采用的是Hadoop-0.20版本):
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 -
安装Hadoop
- 更新源包索引:
sudo apt-get update sudo apt-get dist-upgrade
- 安装Hadoop:
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包,所以并不需要这方面的设置,大家了解一下即可。
SSH设置
在 Hadoop 分布式环境中,主节点(NameNode、JobTracker) 需要通过 SSH 来启动和停止从节点(DataNode、TeskTracker)上的各类进程。因此需要保证环境中的各台机器均可以通过 SSH 登录访问,并且主节点用 SSH 登录从节点时,不需要输入密码,这样主节点才能在后台自如地控制其它结点。可以将各台机器上的 SSH 配置为使用无密码公钥认证方式来实现。 Ubuntu上的SSH协议的开源实现是OpenSSH, 缺省状态下是没有安装的,如需使用需要进行安装。
安装OpenSSH
安装OpenSSH很简单,只需要下列命令就可以把openssh-client和openssh-server给安装好:
sudo apt-get install ssh
设置OpenSSH的无密码公钥认证
首先在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 文件的内容是长长的一行,复制时不要遗漏字符或混入了多余换行符。
无密码公钥SSH的连接测试
从 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
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的核心配置
Hadoop的核心配置文件是/etc/hadoop/conf/core-site.xml,具体配置如下:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>fs.default.name</name> <!-- <value>hdfs://localhost:8020</value> --> <value>hdfs://hadoop-01:8020</value> </property> <property> <name>hadoop.tmp.dir</name> <value>/var/lib/hadoop-0.20/cache/${user.name}</value> </property> </configuration>
设置Hadoop的分布式存储环境
Hadoop的分布式环境设置主要是通过文件/etc/hadoop/conf/hdfs-site.xml来实现的,具体配置如下:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>dfs.replication</name> <!-- <value>1</value> --> <value>3</value> </property> <property> <name>dfs.permissions</name> <value>false</value> </property> <property> <!-- specify this so that running 'hadoop namenode -format' formats the right dir --> <name>dfs.name.dir</name> <value>/var/lib/hadoop-0.20/cache/hadoop/dfs/name</value> </property> </configuration>
设置Hapoop的分布式计算环境
Hadoop的分布式计算是采用了Map/Reduce算法,该算法环境的设置主要是通过文件/etc/hadoop/conf/mapred-site.xml来实现的,具体配置如下:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>mapred.job.tracker</name> <!-- <value>localhost:8021</value> --> <value>hadoop-01:8021</value> </property> </configuration>
设置Hadoop的主从节点
首先设置主节点,编辑/etc/hadoop/conf/masters文件,如下所示:
hadoop-01
然后是设置从节点,编辑/etc/hadoop/conf/slaves文件,如下所示:
hadoop-02 hadoop-03 hadoop-04 firehare-303
设置从节点上的Hadoop
从节点上的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
格式化分布式文件系统
在启动Hadoop之前还要做最后一个准备工作,那就是格式化分布式文件系统,这个只需要在主节点做就行了,具体如下:
/usr/lib/hadoop-0.20/bin/hadoop namenode -format
启动Hadoop服务
启动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@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 08:55:43 INFO mapred.FileInputFormat: Total input paths to process : 6 10/03/11 08:55:44 INFO mapred.JobClient: Running job: job_201003110836_0001 10/03/11 08:55:45 INFO mapred.JobClient: map 0% reduce 0% 10/03/11 08:55:57 INFO mapred.JobClient: map 33% reduce 0% 10/03/11 08:56:06 INFO mapred.JobClient: map 33% reduce 11% 10/03/11 08:56:07 INFO mapred.JobClient: map 66% reduce 11% 10/03/11 08:56:12 INFO mapred.JobClient: map 100% reduce 11% 10/03/11 08:56:21 INFO mapred.JobClient: map 100% reduce 22% 10/03/11 09:04:06 INFO mapred.JobClient: Task Id : attempt_201003110836_0001_m_000002_0, Status : FAILED Too many fetch-failures 10/03/11 09:04:06 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn 10/03/11 09:04:06 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn 10/03/11 09:04:22 INFO mapred.JobClient: map 100% reduce 27% 10/03/11 09:06:50 INFO mapred.JobClient: Task Id : attempt_201003110836_0001_m_000003_0, Status : FAILED Too many fetch-failures 10/03/11 09:06:50 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn 10/03/11 09:06:50 WARN mapred.JobClient: Error reading task outputusvr-303b.cmet.wzu.edu.cn 10/03/11 09:07:03 INFO mapred.JobClient: map 100% reduce 100% 10/03/11 09:07:05 INFO mapred.JobClient: Job complete: job_201003110836_0001 10/03/11 09:07:05 INFO mapred.JobClient: Counters: 18 10/03/11 09:07:05 INFO mapred.JobClient: Job Counters 10/03/11 09:07:05 INFO mapred.JobClient: Launched reduce tasks=1 10/03/11 09:07:05 INFO mapred.JobClient: Launched map tasks=8 10/03/11 09:07:05 INFO mapred.JobClient: Data-local map tasks=8 10/03/11 09:07:05 INFO mapred.JobClient: FileSystemCounters 10/03/11 09:07:05 INFO mapred.JobClient: FILE_BYTES_READ=100 10/03/11 09:07:05 INFO mapred.JobClient: HDFS_BYTES_READ=12360 10/03/11 09:07:05 INFO mapred.JobClient: FILE_BYTES_WRITTEN=422 10/03/11 09:07:05 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=204 10/03/11 09:07:05 INFO mapred.JobClient: Map-Reduce Framework 10/03/11 09:07:05 INFO mapred.JobClient: Reduce input groups=4 10/03/11 09:07:05 INFO mapred.JobClient: Combine output records=4 10/03/11 09:07:05 INFO mapred.JobClient: Map input records=315 10/03/11 09:07:05 INFO mapred.JobClient: Reduce shuffle bytes=49 10/03/11 09:07:05 INFO mapred.JobClient: Reduce output records=4 10/03/11 09:07:05 INFO mapred.JobClient: Spilled Records=8 10/03/11 09:07:05 INFO mapred.JobClient: Map output bytes=86 10/03/11 09:07:05 INFO mapred.JobClient: Map input bytes=12360 10/03/11 09:07:05 INFO mapred.JobClient: Combine input records=4 10/03/11 09:07:05 INFO mapred.JobClient: Map output records=4 10/03/11 09:07:05 INFO mapred.JobClient: Reduce input records=4 10/03/11 09:07:05 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same. 10/03/11 09:07:05 INFO mapred.FileInputFormat: Total input paths to process : 1 10/03/11 09:07:05 INFO mapred.JobClient: Running job: job_201003110836_0002 10/03/11 09:07:06 INFO mapred.JobClient: map 0% reduce 0% 10/03/11 09:07:13 INFO mapred.JobClient: map 100% reduce 0% 10/03/11 09:07:19 INFO mapred.JobClient: map 100% reduce 100% 10/03/11 09:07:21 INFO mapred.JobClient: Job complete: job_201003110836_0002 10/03/11 09:07:21 INFO mapred.JobClient: Counters: 18 10/03/11 09:07:21 INFO mapred.JobClient: Job Counters 10/03/11 09:07:21 INFO mapred.JobClient: Launched reduce tasks=1 10/03/11 09:07:21 INFO mapred.JobClient: Launched map tasks=1 10/03/11 09:07:21 INFO mapred.JobClient: Data-local map tasks=1 10/03/11 09:07:21 INFO mapred.JobClient: FileSystemCounters 10/03/11 09:07:21 INFO mapred.JobClient: FILE_BYTES_READ=100 10/03/11 09:07:21 INFO mapred.JobClient: HDFS_BYTES_READ=204 10/03/11 09:07:21 INFO mapred.JobClient: FILE_BYTES_WRITTEN=232 10/03/11 09:07:21 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=62 10/03/11 09:07:21 INFO mapred.JobClient: Map-Reduce Framework 10/03/11 09:07:21 INFO mapred.JobClient: Reduce input groups=1 10/03/11 09:07:21 INFO mapred.JobClient: Combine output records=0 10/03/11 09:07:21 INFO mapred.JobClient: Map input records=4 10/03/11 09:07:21 INFO mapred.JobClient: Reduce shuffle bytes=0 10/03/11 09:07:21 INFO mapred.JobClient: Reduce output records=4 10/03/11 09:07:21 INFO mapred.JobClient: Spilled Records=8 10/03/11 09:07:21 INFO mapred.JobClient: Map output bytes=86 10/03/11 09:07:21 INFO mapred.JobClient: Map input bytes=118 10/03/11 09:07:21 INFO mapred.JobClient: Combine input records=0 10/03/11 09:07:21 INFO mapred.JobClient: Map output records=4 10/03/11 09:07:21 INFO mapred.JobClient: Reduce input records=4
不难看出,上述测试已经成功,这说明Hadoop部署成功,能够在上面进行Map/Reduce分布性计算了。