hadoop安装
在Apache Hadoop主页的下载页面https://hadoop.apache.org/releases.html选择版本进行下载:

下载下来的是压缩包:

将压缩包使用Xftp上传hadoop-1的/usr目录下:

执行命令:
# tar -zxvf hadoop-2.7.7.tar.gz
解压完成后会在/usr目录下生成hadoop-2.7.7目录:

然后设置环境变量:
# vim /etc/profile
在profile文件末尾添加:
export HADOOP_HOME=/usr/hadoop-2.7.7
export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH

保存文件,然后执行:
# source /etc/profile
此时即可直接使用hadoop命令:

启动hadoop有三种安装模式:本地模式、伪分布式模式、分布式模式。
本地模式
本地模式只需要修改/usr/hadoop-2.7.7/etc/hadoop/hadoop-env.sh文件,设置JAVA_HOME即可。
在hadoop-1上面进行本地模式配置,在终端使用vim打开该文件:
# vim hadoop-2.7.7/etc/hadoop/hadoop-env.sh
找到export JAVA_HOME=${JAVA_HOME}一行,将该行注释掉,然后添加一行:
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64

本地模式即配置完成。
在/home目录下创建temp目录,在temp目录中创建一个test.txt文件:
# mkdir /home/temp
# vim /home/temp/test.txt
在test.txt文件中写入如下测试内容:
this is a example
hello world hello bob hello everyone
执行命令进入MapReduce示例程序目录:
# cd /usr/hadoop-2.7.7/share/hadoop/mapreduce
查看目录下的内容,执行命令:
# ls -l

其中hadoop-mapreduce-examples-2.7.7.jar就是要来用测试的jar包,测试执行如下:
# hadoop jar hadoop-mapreduce-examples-2.7.7.jar wordcount /home/temp/test.txt /home/temp/mcl
测试结果会生成在/home/temp/mcl目录下,执行命令查看结果:
# cat /home/temp/mcl/part-r-00000
可以看到每个单词的统计结果数据。
伪分布式模式
伪分布式模式是在单机上,模拟一个分布式的环境,具备Hadoop的所有功能。
配置文件路径:/usr/hadoop-2.7.7/etc/hadoop/。
首先配置的文件是hadoop-env.sh,与本地模式一样,配置好JAVA_HOME参数即可。
然后配置的两个文件是hdfs-site.xml和core-site.xml,这两个文件作用是配置HDFS的一些属性。
在hdfs-site.xml文件中配置(数据冗余级别设置为1):

在core-site.xml文件中配置(namenode的地址和HDFS数据保存的目录,默认是Linux的tmp目录):

其中tmp目录要手动创建:
# mkdir /usr/hadoop-2.7.7/tmp
接下来配置的两个文件是mapred-site.xml和yarn-site.xml,这两个文件的作用是配置mapreduce使用yarn容器和yarn的一些属性。
现在目录中没有mapred-site.xml文件,但是有一个mapred-site.xml.template文件,所以在/usr/hadoop-2.7.7/etc/hadoop路径下执行命令:
# cp mapred-site.xml.template mapred-site.xml
然后在拷贝的mapred-site.xml文件中添加配置(ResourceManager的地址):

在yarn-site.xml文件中配置(NodeManager运行MR任务的方式):

最后对namenode进行格式化,执行命令:
# hdfs namenode -format
只要看到信息中有一句关键:
common.Storage: Storage directory /usr/hadoop-2.7.7/tmp/dfs/name has been successfully formatted.
则格式化成功:

启动hadoop,执行/usr/hadoop-2.7.7/sbin目录下的启动脚本:
# start-all.sh
完成后信息无报错:

执行jps命令查看进程如下:

则启动完成。
打开浏览器,输入http://192.168.44.128:50070,即可打开hdfs的web页面:

点击Datanodes可以看到数据节点为hadoop-1:

测试wordcount验证是否成功:
创建hadoop的wordCountInput用来上传test.txt文件,执行命令:
# hadoop fs -mkdir /wordCountInput
目录创建完成后,将/home/temp/test.txt文件上传该目录,执行命令:
# hadoop fs -put /home/temp/test.txt /wordCountInput
查看文件是否上传,执行命令:
# hadoop fs -ls /wordCountInput

然后使用hadoop-mapreduce-examples-2.7.7.jar测试执行如下:
# hadoop jar /usr/hadoop-2.7.7/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.7.jar wordcount /wordCountInput/test.txt /wordCountOutput

等待计算完成,查看wordCountOutput目录下的文件,执行命令:
# hadoop fs -ls /wordCountOutput
可以看到计算结果文件已经生成:

查看结果数据,执行命令:
# hadoop fs -cat /wordCountOutput/part-r-00000
test.txt文件中各个单词的统计数据如下:

停止进程执行命令:
# stop-all.sh
等下stop完成,执行jps命令可以看到进程已经全部关闭:

完全分布式模式
是真正的分布式环境,具备生产条件。
完全分布式模式与伪分布式模式的区别就在于配置文件配置内容不同,namenode与datanode分别部署在不同的服务器上。
以下使用hadoop-1、hadoop-2和hadoop-3三台服务器搭建一个完全分布式集群,其中hadoop-1为namenode节点,其余两台为datanode节点。
在hadoop-1服务器上:
首先配置hadoop-env.sh文件,配置好JAVA_HOME参数。
然后配置hdfs-site.xml文件,将冗余级别改为3:

接下来core-site.xml文件,内容不需要修改:

接下来mapred-site.xml文件,内容不需要修改:

接下来yarn-site.xml 文件,内容不需要修改:

最后slaves文件中的localhost,改成hadoop-2和hadoop-3:

修改完成之后,将以上文件全部拷贝到hadoop-2和hadoop-3的相应目录下无需做其他修改。
hadoop重置
因为之前搭建伪分布式模式时,格式化过hadoop-1的namenode,所以此处需要进行重置。
首先要删除/usr/hadoop-2.7.7/logs目录中的所有文件
# rm -r *

然后重新格式化namenode:
# hdfs namenode -format

然后执行启动:

然后使用命令jps,查看三台服务器的进程如下:
hadoop-1:

hadoop-2:

hadoop-3:

打开浏览器,输入http://192.168.44.128:50070,即可打开hdfs的web页面:

点击Datanodes可以看到数据节点为hadoop-2和hadoop-3:

本地模式一般情况下不用,开发可以使用伪分布式模式或者资源充足情况下使用完全分布式模式。
文中的配置文件参数都是最简化的配置,实际情况应该按照需求进行配置。
网友评论