Hadoop是一个由Apache基金会所开发的分布式系统基础架构,实现分布式文件系统HDFS,用于存储大数据集,以及可以以流的形式访问(streaming access)文件系统中的数据。Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。本文描述了在CentOS 7下,基于三个节点安装hadoop 2.8,供大家参考。
一、基础环境描述
OS版本
[root@namenode ~]# more /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
JAVA环境
[root@namenode ~]# java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)
三个节点主机名及IP
192.168.81.142 namenode.example.com namenode
192.168.81.146 datanode1.example.com datanode1
192.168.81.147 datanode2.example.com datanode2
hadoop版本
[hadoop@namenode ~]$ hadoop version
Hadoop 2.8.1
二、主要步骤
配置Java运行环境
配置hosts文件
配置hadoop运行账户及数据存放目录
配置ssh等效连接
配置用户环境变量
下载解压hadoop安装包
配置hadoop相关配置文件
格式化namenode
启动hadoop
验证hadoop
三、配置及安装hadoop 2.8
1、配置java运行环境(所有节点)
[root@namenode ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/etc/alternatives/java_sdk_1.8.0_openjdk
export PATH=$PATH:$JAVA_HOME
[root@namenode ~]# source /etc/profile.d/java.sh
[root@namenode ~]# env |grep JAVA_HOME
JAVA_HOME=/etc/alternatives/java_sdk_1.8.0_openjdk
2、配置Hosts文件,添加用户及创建目录(所有节点)
[root@namenode ~]# vim /etc/hosts
192.168.81.142 namenode.example.com namenode
192.168.81.146 datanode1.example.com datanode1
192.168.81.147 datanode2.example.com datanode2
[root@namenode ~]# useradd hadoop
[root@namenode ~]# passwd hadoop
[root@namenode ~]# mkdir -pv /usr/local/hadoop/datanode
[root@namenode ~]# chmod 755 /usr/local/hadoop/datanode
[root@namenode ~]# chown hadoop:hadoop /usr/local/hadoop
3、配置等效性(所有节点)
[root@namenode ~]# su - hadoop
[hadoop@namenode ~]$
[hadoop@namenode ~]$ ssh-keygen
[hadoop@namenode ~]$ ssh-copy-id localhost
[hadoop@namenode ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop@192.168.81.146
[hadoop@namenode ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop@192.168.81.147
[hadoop@namenode ~]$ ssh namenode.example.com date;\
> ssh datanode1.example.com date;
> ssh datanode2.example.com date
Wed Nov 15 16:06:16 CST 2017
Wed Nov 15 16:06:16 CST 2017
Wed Nov 15 16:06:16 CST 2017
4、配置hadoop运行环境(所有节点)
[hadoop@namenode ~]$ vi ~/.bash_profile
export HADOOP_HOME=/usr/local/hadoop
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_YARN_HOME=$HADOOP_HOME
export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib/native"
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin
[hadoop@namenode ~]$ source ~/.bash_profile
5、安装hadoop(所有节点)
[hadoop@namenode ~]$ wget http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.8.1/hadoop-2.8.1.tar.gz -P /tmp
[hadoop@namenode ~]$ tar -xf /tmp/hadoop-2.8.1.tar.gz -C /usr/local/hadoop --strip-components 1
6、配置hadoop相关配置文件
[hadoop@namenode ~]$ vim /usr/local/hadoop/etc/hadoop/hdfs-site.xml
<configuration>
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>file:///usr/local/hadoop/datanode</value>
</property>
</configuration>
[hadoop@namenode ~]$ scp /usr/local/hadoop/etc/hadoop/hdfs-site.xml \
> datanode1:/usr/local/hadoop/etc/hadoop
[hadoop@namenode ~]$ scp /usr/local/hadoop/etc/hadoop/hdfs-site.xml \
> datanode2:/usr/local/hadoop/etc/hadoop
[hadoop@namenode ~]$ vim /usr/local/hadoop/etc/hadoop/core-site.xml
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://namenode.example.com:9000/</value>
</property>
</configuration>
[hadoop@namenode ~]$ scp /usr/local/hadoop/etc/hadoop/core-site.xml \
> datanode1:/usr/local/hadoop/etc/hadoop
[hadoop@namenode ~]$ scp /usr/local/hadoop/etc/hadoop/core-site.xml \
> datanode2:/usr/local/hadoop/etc/hadoop
再次编辑hdfs-site.xml,仅仅针对namenode节点
[hadoop@namenode ~]$ mkdir -pv /usr/local/hadoop/namenode
[hadoop@namenode ~]$ vim /usr/local/hadoop/etc/hadoop/hdfs-site.xml
将以下内容添加到<configuration> - </configuration>
<property>
<name>dfs.namenode.name.dir</name>
<value>file:///usr/local/hadoop/namenode</value>
</property>
[hadoop@namenode ~]$ vi /usr/local/hadoop/etc/hadoop/mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
[hadoop@namenode ~]$ vi /usr/local/hadoop/etc/hadoop/yarn-site.xml
<configuration>
<property>
<name>yarn.resourcemanager.hostname</name>
<value>namenode.example.com</value>
</property>
<property>
<name>yarn.nodemanager.hostname</name>
<value>namenode.example.com</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>
[hadoop@namenode ~]$ vi /usr/local/hadoop/etc/hadoop/slaves
# add all nodes (remove localhost)
namenode.example.com
datanode1.example.com
datanode2.example.com
7、格式化
[hadoop@namenode ~]$ hdfs namenode -format
17/11/16 16:32:20 INFO namenode.NameNode: STARTUP_MSG:
/************************************************************
STARTUP_MSG: Starting NameNode
STARTUP_MSG: user = hadoop
STARTUP_MSG: host = namenode.example.com/192.168.81.142
STARTUP_MSG: args = [-format]
STARTUP_MSG: version = 2.8.1
........
17/11/16 16:32:21 INFO namenode.NameNode: SHUTDOWN_MSG:
/************************************************************
SHUTDOWN_MSG: Shutting down NameNode at namenode.example.com/192.168.81.142
************************************************************/
8、启动hadoop
[hadoop@namenode ~]$ start-dfs.sh
Starting namenodes on [namenode.example.com]
namenode.example.com: starting namenode, logging to /usr/...-namenode-namenode.example.com.out
datanode2.example.com: starting datanode, logging to /usr/...-datanode-datanode2.example.com.out
namenode.example.com: starting datanode, logging to /usr/...-datanode-namenode.example.com.out
datanode1.example.com: starting datanode, logging to /usr/...-datanode-datanode1.example.com.out
Starting secondary namenodes [blogs.jrealm.net]
blogs.jrealm.net: starting secondarynamenode, logging to /usr/...-secondarynamenode-namenode.example.com.out
[hadoop@namenode ~]$ start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /usr/...-resourcemanager-namenode.example.com.out
datanode2.example.com: starting nodemanager, logging to /usr/...-datanode2.example.com.out
datanode1.example.com: starting nodemanager, logging to /usr/...-datanode1.example.com.out
namenode.example.com: starting nodemanager, logging to /usr/...-namenode.example.com.out
[root@namenode ~]# jps
12995 Jps
10985 ResourceManager
11179 NodeManager ## Author : Leshami
10061 NameNode ## QQ/Weixin : 645746311
10301 DataNode
10655 SecondaryNameNode
9、测试hadoop
[hadoop@namenode ~]$ hdfs dfs -mkdir /test ##创建测试目录
上传文件到hadoop集群
[hadoop@namenode ~]$ hdfs dfs -copyFromLocal /usr/local/hadoop/NOTICE.txt /test
查看已上传的文件
[hadoop@namenode ~]$ hdfs dfs -cat /test/NOTICE.txt
This product includes software developed by The Apache Software
Foundation (http://www.apache.org/).
The binary distribution of this product bundles binaries of
org.iq80.leveldb:leveldb-api (https://github.com/dain/leveldb), which has the
following notices:
* Copyright 2011 Dain Sundstrom <dain@iq80.com>
* Copyright 2011 FuseSource Corp. http://fusesource.com
使用自带的jar包map-reduce 测试字数统计
[hadoop@namenode ~]$ hadoop jar \
> /usr/local/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar wordcount /test/NOTICE.txt /output01
17/11/17 14:14:39 INFO client.RMProxy: Connecting to ResourceManager at namenode.example.com/192.168.81.142:8032
17/11/17 14:14:49 INFO input.FileInputFormat: Total input files to process : 1
17/11/17 14:14:49 INFO mapreduce.JobSubmitter: number of splits:1
17/11/17 14:14:50 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1510837568617_0001
17/11/17 14:14:56 INFO impl.YarnClientImpl: Submitted application application_1510837568617_0001
17/11/17 14:14:56 INFO mapreduce.Job: The url to track the job: http://namenode.example.com:8088
/proxy/application_1510837568617_0001/
17/11/17 14:14:56 INFO mapreduce.Job: Running job: job_1510837568617_0001
17/11/17 14:16:05 INFO mapreduce.Job: Job job_1510837568617_0001 running in uber mode : false
17/11/17 14:16:05 INFO mapreduce.Job: map 0% reduce 0%
17/11/17 14:16:56 INFO mapreduce.Job: map 100% reduce 0%
17/11/17 14:17:10 INFO mapreduce.Job: map 100% reduce 100%
17/11/17 14:17:14 INFO mapreduce.Job: Job job_1510837568617_0001 completed successfully
17/11/17 14:17:16 INFO mapreduce.Job: Counters: 49
File System Counters
FILE: Number of bytes read=12094
查看输出日志文件及结果
[hadoop@namenode ~]$ hdfs dfs -ls /output01
Found 2 items
-rw-r--r-- 2 hadoop supergroup 0 2017-11-17 14:17 /output01/_SUCCESS
-rw-r--r-- 2 hadoop supergroup 9485 2017-11-17 14:17 /output01/part-r-00000
[hadoop@namenode ~]$ hdfs dfs -cat /output01/part-r-00000
"AS 1
"GCC 1
"License"); 1
& 1
'Aalto 1
'Apache 4
'ArrayDeque', 1
10、Web界面控制台
查看集群摘要信息
查看集群信息