Hadoop 一:完美集群搭建

12年前

 【集群节点】
——————————————————————————————————————————————
unbuntu-master  192.168.77.81  namenode|jobtracker
unbuntu-slaveone  192.168.77.82  datanode|tasktracker
unbuntu-slavetwo  192.168.77.83  datanode|tasktracker
——————————————————————————————————————————————
# NameNode 和 JobTracker 可以是同一台机器,也可以分开;
# Secondary NameNode 在 Hadoop-1.0.3 中被废弃,用 Checkpoint Node 和 Backup Node 来代替;

 

一、HOSTS

 

1. 修改 HOSTS 文件:
$ sudo vi /ect/hosts

 

# 增加以下行:
192.168.77.81 unbuntu-master
192.168.77.82 unbuntu-slaveone
192.168.77.83 unbuntu-slavetwo

 

# 其中以下两行名称重复,容易引起异常,可注释第一行;
127.0.1.1  unbuntu-master
192.168.77.81 unbuntu-master

 

2. 其他节点同步修改;

 

二、JDK(Ubuntu)

 

Suse 和 Redhat 操作系统可以下载 RPM 格式安装包,使用以下命令直接安装,并且不需要配置环境变量:
$ rpm -ivh jdk-7u6-linux-x64.rpm

 

1. 下载 JDK 安装包并放入指定位置:
/home/navon/entry/jdk-7u6-linux-x64.tar.gz

 

2. 解压并移动到指定位置:
$ tar -zxvf jdk-7u6-linux-x64.tar.gz
$ mv /home/navon/entry/jdk1.7.0_06 /usr/java

 

3. 配置环境变量:
$ vim ~/.bashrc

 

# 增加以下行:
export JAVA_HOME=/usr/java/jdk1.7.0_06
export CLASSPATH=.:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib
export PATH=${JAVA_HOME}/bin:$PATH

 

# 使配置生效:
$ source ~/.bashrc

 

4. 测试安装:
$ java -version

 

5. 其他节点同步安装;

 

三、SSH(Ubuntu)

 

Suse 和 Redhat 操作系统,可以省略步骤1,SSH 默认已安装;

 

1. 安装 SSH(Ubuntu):
$ sudo apt-get install ssh

 

2. 生成密钥:
$ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

 

3. 存储口令:
$ ssh-add

 

4. 同步到其他节点(其他节点需先安装 SSH):

方法一:
$ scp ~/.ssh/authorized_keys ubuntu-slaveone:~/.ssh/
$ scp ~/.ssh/authorized_keys ubuntu-slavetwo:~/.ssh/

 

navon@ubuntu-slaveone$ chmod 644 ~/.ssh/authorized_keys
navon@ubuntu-slavetwo$ chmod 644 ~/.ssh/authorized_keys

 

方法二:
$ scp ~/.ssh/id_dsa.pub ubuntu-slaveone:~/.ssh/
$ scp ~/.ssh/id_dsa.pub ubuntu-slavetwo:~/.ssh/

 

navon@ubuntu-slaveone$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
navon@ubuntu-slavetwo$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

 

5. 验证:
navon@ubuntu-master$ ssh ubuntu-slaveone
navon@ubuntu-master$ ssh ubuntu-slavetwo

 

# 不需要密码连接成功说明配置完成;

 

四、安装 HADOOP

 

1. 下载 Hadoop 安装包并放入指定位置:
/home/navon/entry/hadoop-1.0.3.tar.gz

 

2. 解压并移动到指定位置:
$ tar -zxvf hadoop-1.0.3.tar.gz
$ mv /home/navon/entry/hadoop-1.0.3. /usr

 

3. 配置环境变量(所有节点):
$ vim ~/.bashrc

 

# 增加以下行:
export HADOOP_HOME=/usr/hadoop-1.0.3
export PATH=$PATH:$HADOOP_HOME/bin

 

# 使配置生效:
$ source ~/.bashrc

 

4. 其他节点同步安装;

 

五、Hadoop 配置(/usr/hadoop-1.0.3/conf):

 

1. 配置 hadoop-env.sh:
$ vi /usr/hadoop-1.0.3/conf/hadoop-env.sh

 

# 增加以下行:
export JAVA_HOME=/usr/java/jdk1.7.0_06
export HADOOP_HOME_WARN_SUPPRESS=1

 

# 后面一句表示取消环境变量HADOOP_HOME过时的警告;

 

2. 配置 core-site.xml:
$ vi /usr/hadoop-1.0.3/conf/core-site.xml

 

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
 <property>
  <name>hadoop.local.dir</name>
  <value>/home/${user.name}/hadoop/local</value>
  <description>A base for other directories.</description>
 </property>
 <property>
  <name>hadoop.other.dir</name>
  <value>/home/${user.name}/hadoop/other</value>
  <description>A base for other directories.</description>
 </property>
 <property>
  <name>hadoop.remote.dir</name>
  <value>/home/${user.name}/hadoop/remote</value>
  <description>A base for other directories.</description>
 </property>
 <property>
  <name>fs.default.name</name>
  <value>hdfs://ubuntu-master:19880</value>
  <description>The name of the default file system. A URI whose
  scheme and authority determine the FileSystem implementation.
  The uri's scheme determines the config property (fs.SCHEME.impl)
  naming the FileSystem implementation class. The uri's authority is
  used to determine the host, port, etc. for a filesystem.</description>
 </property>
 <property>
  <name>hadoop.tmp.dir</name>
  <value>/home/${user.name}/hadoop/tmp</value>
  <description>A base for other temporary directories.</description>
 </property>
</configuration>

 

# 其中 hadoop.local.dir 、hadoop.remote.dir 和 hadoop.other.dir
# 是自定义变量,使用方式:${hadoop.local.dir};
# ${user.name} 返回当前系统用户;

 

3. 配置 hdfs-site.xml:
$ vi /usr/hadoop-1.0.3/conf/hdfs-site.xml

 

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
 <property>
  <name>dfs.name.dir</name>
  <value>${hadoop.local.dir}/dfs/name,${hadoop.remote.dir}/dfs/name</value>
  <description>Determines where on the local filesystem the DFS
  name node should store the name table(fsimage). If this is a
  comma-delimited list of directories then the name table is
  replicated in all of the directories, for redundancy. </description>
 </property>
 <property>
  <name>dfs.data.dir</name>
  <value>${hadoop.local.dir}/dfs/data,${hadoop.other.dir}/dfs/data</value>
  <description>Determines where on the local filesystem an DFS
  data node should store its blocks. If this is a comma-delimited
  list of directories, then data will be stored in all named
  directories, typically on different devices. Directories that
  do not exist are ignored. </description>
 </property>
 <property>
  <name>dfs.replication</name>
  <value>2</value>
  <description>Default block replication. The actual number of
  replications can be specified when the file is created. The default
  is used if replication is not specified in create time. </description>
 </property>
</configuration>

 

# 此处的目录不能预先创建,Hadoop 格式化时会自动创建,如果预先创建反而会有问题;

 

4. 配置 mapred-site.xml:
$ vi /usr/hadoop-1.0.3/conf/mapred-site.xml

 

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
 <property>
  <name>mapred.job.tracker</name>
  <value>ubuntu-master:19881</value>
  <description>The host and port that the MapReduce job tracker
  runs at. If "local", then jobs are run in-process as a single map
  and reduce task. </description>
 </property>
 <property>
  <name>mapred.local.dir</name>
  <value>${hadoop.local.dir}/mapred/local</value>
  <description>The local directory where MapReduce stores
  intermediate data files. May be a comma-separated list of
  directories on different devices in order to spread disk i/o.
  Directories that do not exist are ignored. </description>
 </property>
 <property>
  <name>mapred.system.dir</name>
  <value>${hadoop.local.dir}/mapred/system</value>
  <description>The directory where MapReduce stores control files.
  </description>
 </property>
 <property>
  <name>mapred.tasktracker.map.tasks.maximum</name>
  <value>2</value>
  <description>The maximum number of map tasks that will be
  run simultaneously by a task tracker. </description>
 </property>
 <property>
  <name>mapred.tasktracker.reduce.tasks.maximum</name>
  <value>2</value>
  <description>The maximum number of reduce tasks that will be
  run simultaneously by a task tracker.</description>
 </property>
 <property>
  <name>mapred.child.java.opts</name>
  <value>-Xmx800m</value>
  <description>Java opts for the task tracker child processes.
  The following symbol, if present, will be interpolated: @taskid@
  is replaced by current TaskID. Any other occurrences of '@' will
  go unchanged. For example, to enable verbose gc logging to a
  file named for the taskid in /tmp and to set the heap maximum
  to be a gigabyte, pass a 'value'
  of: -Xmx1024m -verbose:gc -Xloggc:/tmp/@taskid@.gc The
  configuration variable mapred.child.ulimit can be used to control
  the maximum virtual memory of the child processes. </description>
 </property>
</configuration>

 

5. 配置 masters:
$ vi /usr/hadoop-1.0.3/conf/masters

 

# 注意,此处不要设置为:
unbuntu-master

 

# 运行 Secondary NameNode 的机器列表(每行一个);

 

6. 配置 slaves
$ vi /usr/hadoop-1.0.3/conf/slaves

 

# 增加以下行:
unbuntu-slaveone
unbuntu-slavetwo

 

# 运行 DataNode 和 TaskTracker 的机器列表(每行一个);

 

7. 同步Hadoop配置:

navon@ubuntu-master$ scp -r /usr/hadoop-1.0.3/conf ubuntu-slaveone:/usr/hadoop-1.0.3
navon@ubuntu-master$ scp -r /usr/hadoop-1.0.3/conf ubuntu-slavetwo:/usr/hadoop-1.0.3

 

8. 测试:
navon@ubuntu-master$ hadoop

 

六、启动集群

 

1. 格式化一个新的分布式文件系统:
navon@ubuntu-master$ hadoop namenode -format

 

2. 启动 Hadoop:
navon@ubuntu-master$ start-dfs.sh
navon@ubuntu-master$ start-mapred.sh
 
# 在主节点上启动 Hadoop,主节点会启动所有从节点的 Hadoop;

# start-all.sh 和 stop-all.sh 这两个脚本在1.03中被废弃,使用
# start-dfs.sh & start-mapred.sh 和 stop-dfs.sh & stop-mapred.sh 来代替;

 

3. 关闭 Hadoop:
navon@ubuntu-master$ stop-dfs.sh
navon@ubuntu-master$ stop-mapred.sh

 

七、排错

 

1. 日志目录:
navon@ubuntu-master$ cd /usr/hadoop1.0.3/logs/

 

2. 查看端口占用情况:
navon@ubuntu-master$ netstat -antp | grep 19880

 

3. Incompatible namespaceIDs 错误:
namespaceIDs 不一致,每次进行文件系统格式化(hadoop namenode -format)会
重新创建一个 namenodeId,存储在 …/dfs/data 目录中,格式化时清空了 NameNode
下的数据,但没有清空 DataNode 下的数据,导致启动时 DataNode 失败!
每次格式化之前清空所有节点数据目录(…/dfs/data)即可避免该问题的出现;

 

八、实例

 

1. HDFS 浏览:
# NameNode 和 JobTracker 的网络接口,默认地址分别为:
http://ubuntu-master:50070/
http://ubuntu-master:50030/

 

# DataNode 和 TaskTracker 的网络接口,默认地址分别为:
http://ubuntu-slaveone:50075/
http://ubuntu-slaveone:50060/
http://ubuntu-slavetwo:50075/
http://ubuntu-slavetwo:50060/

 

2. 使用 jps(JVM 进程工具)查看进程:
navon@ubuntu-master$ jps
***** NameNode
***** JobTracker
***** jps

 

navon@ubuntu-slaveone$ jps
***** DataNode
***** TaskTracker
***** jps

 

3. 将输入文件拷贝到分布式文件系统:
$ hadoop fs -mkdir sample-input
$ hadoop fs -put /usr/hadoop_1.0.3/conf/slaves sample-input

 

4. 运行发行版提供的示例程序:
$ hadoop jar hadoop-examples-1.0.3.jar grep sample-input sample-output 'sla[a-z.]+'

 

# 运行示例程序中的 grep(类),HDFS 上的输入目录为 sample-input
,输出目录为 sample-output,查找以 sla 开头的文件;

 

5. HDFS 常用操作:
#  将 HDFS 的文件拷贝到本地文件系统;
hadoop fs -get output ~/output

 

# 查看 HDFS 的基本统计信息;
hadoop dfsadmin -report
 
# 查看当前 HDFS 的副本数;
$ hadoop fsck -locations

 

# 列出 HDFS 下的文件;
$ hadoop fs -ls

 

# 列出 HDFS 下 output 目录中文件;
$ hadoop fs -ls output

 

#  删除 HDFS 上 output 目录;
hadoop fs -rmr output

 

#  查看 HDFS 上 output 目录中文件的内容;
hadoop fs -cat output/*

转自:http://www.cnblogs.com/cloudhopping/archive/2012/09/14/perfact_hadoop_1.html