hadoop2.9.1伪分布式环境搭建以及文件系统的简单操作
1、准备
公司主营业务:做网站、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出武川免费做网站回馈大家。
1.1、在vmware上安装centos7的虚拟机
1.2、系统配置
配置网络
# vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.120.131
GATEWAY=192.168.120.2
NETMASK=255.255.255.0
DNS1=8.8.8.8
DNS2=4.4.4.4
1.3、配置主机名
# hostnamectl set-hostname master1
# hostname master1
1.4、指定时区(如果时区不是上海)
# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 6月 4 19:25 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
如果时区不对的话需要修改时区,方法:
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
1.5、上传包
hadoop-2.9.1.tar
jdk-8u171-linux-x64.tar
2、开始搭建环境
2.1、创建用户和组
[root@master1 ~]# groupadd hadoop
[root@master1 ~]# useradd -g hadoop hadoop
[root@master1 ~]# passwd hadoop
2.2、解压包
切换用户
[root@master1 ~]# su hadoop
创建存放包的目录
[hadoop@master1 root]$ cd
[hadoop@master1 ~]$ mkdir src
[hadoop@master1 ~]$ mv *.tar src
解压包
[hadoop@master1 ~]$ cd src
[hadoop@master1 src]$ tar -xf jdk-8u171-linux-x64.tar -C ../
[hadoop@master1 src]$ tar xf hadoop-2.9.1.tar -C ../
[hadoop@master1 src]$ cd
[hadoop@master1 ~]$ mv jdk1.8.0_171 jdk
[hadoop@master1 ~]$ mv hadoop-2.9.1 hadoop
2.3、配置环境变量
[hadoop@master1 ~]$ vi .bashrc
export JAVA_HOME=/home/hadoop/jdk
export JRE_HOME=/$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin
export HADOOP_HOME=/home/hadoop/hadoop
export HADOOP_INSTALL=$HADOOP_HOME
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin
使配置文件生效
[hadoop@master1 ~]$ source .bashrc
验证
[hadoop@master1 ~]$ java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[hadoop@master1 ~]$ hadoop version
Hadoop 2.9.1
Subversion https://github.com/apache/hadoop.git -r e30710aea4e6e55e69372929106cf119af06fd0e
Compiled by root on 2018-04-16T09:33Z
Compiled with protoc 2.5.0
From source with checksum 7d6d2b655115c6cc336d662cc2b919bd
This command was run using /home/hadoop/hadoop/share/hadoop/common/hadoop-common-2.9.1.jar
2.4、修改hadoop配置文件
[hadoop@master1 ~]$ cd hadoop/etc/hadoop/
[hadoop@master1 hadoop]$ vi hadoop-env.sh
export JAVA_HOME=/home/hadoop/jdk
[hadoop@master1 hadoop]$ vi core-site.xml
说明:
fs.defaultFS:这个属性用来指定namenode的hdfs协议的文件系统通信地址,可以指定一个主机+端口,也可以指定一个namenode服务(这个服务内部可以有多台namenode实现ha的namenode服务)
hadoop.tmp.dir:hadoop集群在工作的时候存储的一些临时文件的目录
[hadoop@master1 hadoop]$ vi hdfs-site.xml
说明:
dfs.replication:hdfs的副本数设置。也就是上传一个文件,其分割的block块后,每个block的冗余副本个数,默认配置是3。
下面的参数以配置就会出现datanode无法启动的问题,所以不做配置,尚未搞明白怎么出现的。
dfs.namenode.name.dir:namenode数据的存放目录。也就是namenode元数据存放的目录,记录了hdfs系统中文件的元数据。
dfs.datanode.data.dir:datanode数据的存放目录。也就是block块的存放目录。
下面贴出异常信息
[hadoop@master1 logs]$ pwd
/home/hadoop/hadoop/logs
[hadoop@master1 logs]$ tail -f hadoop-hadoop-datanode-master1.log
2018-06-12 22:30:14,749 WARN org.apache.hadoop.hdfs.server.common.Storage: Failed to add storage directory [DISK]file:/data/hadoop/hdfs/dn/
java.io.IOException: Incompatible clusterIDs in /data/hadoop/hdfs/dn: namenode clusterID = CID-5bbc555b-4622-4781-9a7f-c2e5131e4869; datanode clusterID = CID-29ec402d-95f8-4148-8d18-f7e4b965be4f
at org.apache.hadoop.hdfs.server.datanode.DataStorage.doTransition(DataStorage.java:760)
2018-06-12 22:30:14,752 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: Initialization failed for Block pool
java.io.IOException: All specified directories have failed to load.
at org.apache.hadoop.hdfs.server.datanode.DataStorage.recoverTransitionRead(DataStorage.java:557)
2018-06-12 22:30:14,753 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Ending block pool service for: Block pool
2018-06-12 22:30:14,854 INFO org.apache.hadoop.hdfs.server.datanode.DataNode: Removed Block pool
2018-06-12 22:30:16,855 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Exiting Datanode
2018-06-12 22:30:16,916 INFO org.apache.hadoop.hdfs.server.datanode.DataNode: SHUTDOWN_MSG:
/************************************************************
SHUTDOWN_MSG: Shutting down DataNode at master1/192.168.120.131
[hadoop@master1 hadoop]$ cp mapred-site.xml.template mapred-site.xml
[hadoop@master1 hadoop]$ vi mapred-site.xml
说明:
mapreduce.framework.name:指定mr框架为yarn方式,Hadoop二代MP也基于Yarn来运行。
[hadoop@master1 hadoop]$ vi yarn-site.xml
说明:
yarn.resourcemanager.hostname:yarn总管理器的IPC通讯地址,可以是IP也可以是主机名。
yarn.nodemanager.aux-service:集群为MapReduce程序提供的shuffle服务
2.5、创建目录并赋予权限
[hadoop@master1 hadoop]$ exit
[root@master1 ~]# mkdir -p /data/hadoop/hadoop_tmp_dir
[root@master1 ~]# mkdir -p /data/hadoop/hdfs/{nn,dn}
[root@master1 ~]# chown -R hadoop:hadoop /data
3、格式化文件系统并启动服务
3.1、格式化文件系统
[root@master1 ~]# su hadoop
[hadoop@master1 ~]$ cd hadoop/bin
[hadoop@master1 bin]$ ./hdfs namenode -format
注意:
如果是集群环境,HDFS初始化只能在主节点上运行
3.2、启动HDFS
[hadoop@master1 bin]$ cd sbin
[hadoop@master1 sbin]$ ./start-dfs.sh
注意:
如果是集群环境,不管在集群中的哪个节点都可以运行
如果有个别服务启动失败,配置也没有问题的话,很有可能是创建的目录权限问题
3.3、启动YARN
[hadoop@master1 sbin]$ ./start-yarn.sh
注意:
如果是集群环境,只能在主节点中运行
查看服务状态
[hadoop@master1 sbin]$ jps
6708 NameNode
6966 SecondaryNameNode
6808 DataNode
7116 Jps
5791 ResourceManager
5903 NodeManager
3.4、浏览器查看服务状态
使用web查看HSFS运行状态
在浏览器输入
http://192.168.120.131:50070
使用web查看YARN运行状态
在浏览器输入
http://192.168.120.131:8088
4、启动ssh无密码验证
上面启动服务时还需要输入用户名登录密码,如下所示:
[hadoop@master1 sbin]$ ./start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /home/hadoop/hadoop/logs/yarn-hadoop-resourcemanager-master1.out
hadoop@localhost's password:
如果想要做到无密码启动服务的话需要配置ssh
[hadoop@master1 sbin]$ cd ~/.ssh/
[hadoop@master1 .ssh]$ ll
总用量 4
-rw-r--r--. 1 hadoop hadoop 372 6月 12 18:36 known_hosts
[hadoop@master1 .ssh]$ ssh-keygen
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:
SHA256:D14LpPKZbih0K+kVoTl23zGsKK1xOVlNuSugDvrkjJA hadoop@master1
The key's randomart image is:
+---[RSA 2048]----+
| |
| . |
| . + |
| o . * . |
| = = o S . |
| o.=.@ * O . |
|E.=oOoB + o |
|oB+*oo.. |
|ooBo .. |
+----[SHA256]-----+
一路按下enter键就行
[hadoop@master1 .ssh]$ ll
总用量 12
-rw-------. 1 hadoop hadoop 1675 6月 12 18:46 id_rsa
-rw-r--r--. 1 hadoop hadoop 396 6月 12 18:46 id_rsa.pub
-rw-r--r--. 1 hadoop hadoop 372 6月 12 18:36 known_hosts
[hadoop@master1 .ssh]$ cat id_rsa.pub >> ~/.ssh/authorized_keys
[hadoop@master1 .ssh]$ ll
总用量 16
-rw-rw-r--. 1 hadoop hadoop 396 6月 12 18:47 authorized_keys
-rw-------. 1 hadoop hadoop 1675 6月 12 18:46 id_rsa
-rw-r--r--. 1 hadoop hadoop 396 6月 12 18:46 id_rsa.pub
-rw-r--r--. 1 hadoop hadoop 372 6月 12 18:36 known_hosts
如果发现还需要输入密码才能登录,这是因为文件权限的问题,改下权限就可以
[hadoop@master1 .ssh]$ chmod 600 authorized_keys
发现可以实现无密码登录了
[hadoop@master1 .ssh]$ ssh localhost
Last login: Tue Jun 12 18:48:38 2018 from fe80::e961:7d5b:6a72:a2a9%ens33
[hadoop@master1 ~]$
当然无密登录的实现还可以用另一种方法实现
在执行完ssh-keygen之后
执行下面的命令
ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop@master1
5、文件系统的简单应用及遇到的一些问题
5.1、创建目录
在文件系统中创建目录
[hadoop@master1 bin]$ hdfs dfs -mkdir -p /user/hadoop
18/06/12 21:25:31 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
列出创建的目录
[hadoop@master1 bin]$ hdfs dfs -ls /
18/06/12 21:29:55 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 1 items
drwxr-xr-x - hadoop supergroup 0 2018-06-12 21:25 /user
5.2、解决警告问题
有WARN警告,但是并不影响Hadoop正常使用。
两种方式可以解决这个报警问题,方法一是重新编译源码,方法二是在日志中取消告警信息,我采用的是第二种方式。
[hadoop@master1 ]$ cd /home/hadoop/hadoop/etc/hadoop/
[hadoop@master1 hadoop]$ vi log4j.properties
添加
#native WARN
log4j.logger.org.apache.hadoop.util.NativeCodeLoader=ERROR
可以看到效果了
[hadoop@master1 hadoop]$ hdfs dfs -ls /
Found 1 items
drwxr-xr-x - hadoop supergroup 0 2018-06-12 21:25 /user
5.3、上传文件到hdfs文件系统中
[hadoop@master1 bin]$ hdfs dfs -mkdir -p input
[hadoop@master1 hadoop]$ hdfs dfs -put /home/hadoop/hadoop/etc/hadoop input
Hadoop默认附带了丰富的例子:包括wordcoun,terasort,join,grep等,执行下面的命令查看:
[hadoop@master1 bin]$ hadoop jar /home/hadoop/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.9.1.jar
An example program must be given as the first argument.
Valid program names are:
aggregatewordcount: An Aggregate based map/reduce program that counts the words in the input files.
aggregatewordhist: An Aggregate based map/reduce program that computes the histogram of the words in the input files.
bbp: A map/reduce program that uses Bailey-Borwein-Plouffe to compute exact digits of Pi.
dbcount: An example job that count the pageview counts from a database.
distbbp: A map/reduce program that uses a BBP-type formula to compute exact bits of Pi.
grep: A map/reduce program that counts the matches of a regex in the input.
join: A job that effects a join over sorted, equally partitioned datasets
multifilewc: A job that counts words from several files.
pentomino: A map/reduce tile laying program to find solutions to pentomino problems.
pi: A map/reduce program that estimates Pi using a quasi-Monte Carlo method.
randomtextwriter: A map/reduce program that writes 10GB of random textual data per node.
randomwriter: A map/reduce program that writes 10GB of random data per node.
secondarysort: An example defining a secondary sort to the reduce.
sort: A map/reduce program that sorts the data written by the random writer.
sudoku: A sudoku solver.
teragen: Generate data for the terasort
terasort: Run the terasort
teravalidate: Checking results of terasort
wordcount: A map/reduce program that counts the words in the input files.
wordmean: A map/reduce program that counts the average length of the words in the input files.
wordmedian: A map/reduce program that counts the median length of the words in the input files.
wordstandarddeviation: A map/reduce program that counts the standard deviation of the length of the words in the input files.
伪分布式运行MapReduce作业的方式跟单机模式相同,区别在于伪分布式方式读取的是HDFS中的文件(可以将单机步骤中创建的本地input文件夹,输出结果output文件夹都删除来验证这一点)。
[hadoop@master1 sbin]$ hadoop jar /home/hadoop/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.9.1.jar grep input output 'dfs[a-z]+'
18/06/12 22:57:05 INFO client.RMProxy: Connecting to ResourceManager at /192.168.120.131:8032
18/06/12 22:57:07 INFO input.FileInputFormat: Total input files to process : 30
省略。。。
18/06/12 22:57:08 INFO mapreduce.Job: Running job: job_1528815135795_0001
18/06/12 22:57:23 INFO mapreduce.Job: Job job_1528815135795_0001 running in uber mode : false
18/06/12 22:57:23 INFO mapreduce.Job: map 0% reduce 0%
18/06/12 22:58:02 INFO mapreduce.Job: map 13% reduce 0%
省略。。。
18/06/12 23:00:17 INFO mapreduce.Job: map 97% reduce 32%
18/06/12 23:00:18 INFO mapreduce.Job: map 100% reduce 32%
18/06/12 23:00:19 INFO mapreduce.Job: map 100% reduce 100%
18/06/12 23:00:20 INFO mapreduce.Job: Job job_1528815135795_0001 completed successfully
18/06/12 23:00:20 INFO mapreduce.Job: Counters: 50
File System Counters
FILE: Number of bytes read=46
FILE: Number of bytes written=6136681
FILE: Number of read operations=0
省略。。。
File Input Format Counters
Bytes Read=138
File Output Format Counters
Bytes Written=24
查看结果
[hadoop@master1 sbin]$ hdfs dfs -cat output/*
1 dfsmetrics
1 dfsadmin
把结果取到本地
[hadoop@master1 sbin]$ hdfs dfs -get output /data
[hadoop@master1 sbin]$ ll /data
总用量 0
drwxrwxrwx. 5 hadoop hadoop 52 6月 12 19:20 hadoop
drwxrwxr-x. 2 hadoop hadoop 42 6月 12 23:03 output
[hadoop@master1 sbin]$ cat /data/output/*
1 dfsmetrics
1 dfsadmin
6、开启历史服务器
历史服务器服务用来在web中查看任务运行情况
[hadoop@master1 sbin]$ mr-jobhistory-daemon.sh start historyserver
starting historyserver, logging to /home/hadoop/hadoop/logs/mapred-hadoop-historyserver-master1.out
[hadoop@master1 sbin]$ jps
19985 Jps
15778 ResourceManager
15890 NodeManager
14516 NameNode
14827 SecondaryNameNode
19948 JobHistoryServer
14653 DataNode
在初学时尽可能的把配置简单化,有助于出错后的排查。
参考:
https://www.cnblogs.com/wangxin37/p/6501484.html
https://www.cnblogs.com/xing901022/p/5713585.html
文章题目:hadoop2.9.1伪分布式环境搭建以及文件系统的简单操作
浏览地址:http://hbruida.cn/article/jieojg.html