HBase环境搭建有三种方式:1.本地模式:只需要一个节点(只有HMaster,没有HRegionServer),不需要集成ZooKeeper,数据存储在本地文件系统上;2.伪分布模式:只需要一个节点(HMaster和HRegionServer在同一个节点上),需要集成ZooKeeper,数据存储在HDFS上;3.全分布模式:至少需要3个节点(一个HMaster节点和至少2个HRegionServer节点),需要集成ZooKeeper,数据存储在HDFS上。本节先来介绍HBase伪分布模式的环境搭建过程。
本节用到的安装介质:
hbase-2.0.1-bin.tar.gz 提取码:h04f
zookeeper-3.4.10.tar.gz 提取码:31j4
1.搭建Hadoop伪分布环境
Hadoop伪分布模式的搭建过程请参看文章《Hadoop从入门到精通2:Hadoop2.x环境搭建之伪分布模式》。
注意:主机名一定不要使用localhost,否则会出现各种意想不到的错误!!!
2.搭建ZooKeeper伪分布环境
Zookeeper有两种常见的搭建方式:1.standalone方式;2.集群方式。
这两者的区别是:
- standalone方式只需要一个Zookeeper节点;集群方式至少需要3个节点(一个leader,2个follower);
- standalone方式一旦Zookeeper节点宕机,Spark HA就不能使用了;
- 集群方式如果leader宕机,Zookeeper内部会从follower中选举出一个新的leader,可以对HA提供更有效的保障。
这里来介绍Zookeeper的standalone方式的搭建过程:
2.1上传Zookeeper安装包
[root@bigdata ~]# cd /root/tools/
[root@bigdata tools]# ls
zookeeper-3.4.10.tar.gz
2.2解压Zookeeper安装包
[root@bigdata tools]# tar -zxvf zookeeper-3.4.10.tar.gz -C /root/trainings/
2.3配置Zookeeper环境变量
[root@bigdata tools]# vim /root/.bash_profile
ZOOKEEPER_HOME=/root/trainings/zookeeper-3.4.10
export ZOOKEEPER_HOME
PATH=PATH
export PATH
[root@bigdata tools]# source /root/.bash_profile
2.4配置Zookeeper参数
创建Zookeeper保存数据的目录:
[root@bigdata ~]# mkdir /root/trainings/zookeeper-3.4.10/tmp
创建Zookeeper Server ID的配置文件myid,内容写入1:
[root@bigdata ~]# vim /root/trainings/zookeeper-3.4.10/tmp/myid
1
编辑Zookeeper的配置文件zoo.cfg:
[root@bigdata ~]# cd /root/trainings/zookeeper-3.4.10/conf
[root@bigdata conf]# cp zoo_sample.cfg zoo.cfg
[root@bigdata conf]# vim zoo.cfg
#dataDir=/tmp/zookeeper
dataDir=/root/trainings/zookeeper-3.4.10/tmp
server.1=bigdata:2888:3888
2.5启动Zookeeper
[root@bigdata ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdata ~]# jps
1996 QuorumPeerMain
2015 Jps
3.搭建HBase伪分布环境
3.1下载HBase安装包
可以从上面的安装介质链接中下载HBase安装包,或者去HBase官网下载最新HBase安装包,然后使用WInSCP工具将下载好的安装包上传至/root/tools目录下。
[root@bigdata ~]# cd /root/tools/
[root@bigdata tools]# ls
hbase-2.0.1-bin.tar.gz
3.2解压HBase到安装目录
将HBase安装包解压至安装目录/root/trainings/
[root@bigdata tools]# tar -zxvf hbase-2.0.1-bin.tar.gz -C /root/trainings/
3.3配置HBase环境变量
将HBase加入到环境变量PATH中
[root@bigdata tools]# cd /root/trainings/hbase-2.0.1/
[root@bigdata hbase-2.0.1]# pwd
/root/trainings/hbase-2.0.1
[root@bigdata hbase-2.0.1]# vim /root/.bash_profile
HBASE_HOME=/root/trainings/hbase-2.0.1
export HBASE_HOME
PATH=$HBASE_HOME/bin:$PATH
export PATH
[root@bigdata hbase-2.0.1]# source /root/.bash_profile
3.4配置HBase配置文件
启动Hadoop伪分布集群:
[root@bigdata ~]# start-all.sh
在HDFS上新建一个目录用来存储HBase的数据:
[root@bigdata ~]# hdfs dfs -mkdir /hbase
进入$HBASE_HOME/conf目录,配置下面的配置文件:
[root@bigdata conf]# pwd
/root/trainings/hbase-2.0.1/conf[root@bigdata conf]# vim hbase-env.sh
# The java implementation to use. Java 1.8+ required.
# export JAVA_HOME=/usr/java/jdk1.8.0/
export JAVA_HOME=/root/trainings/jdk1.8.0_144
#注意:参数HBASE_MANAGES_ZK=true时表示使用HBase自带的ZooKeeper,
#因此,如果第2步没有安装别的ZooKeeper请将该参数置为true,否则置为false。
export HBASE_MANAGES_ZK=false
[root@bigdata conf]# vim hbase-site.xml
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://bigdata:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>bigdata</value>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>[root@bigdata conf]# vim regionservers
bigdata
注意:这里需要Java的版本在1.8以上。
4.使用HBase
4.1启动HBase
[root@bigdata ~ ]# start-hbase.sh
bigdata: running zookeeper, logging to /root/trainings/hbase-2.0.1/bin/../logs/hbase-root-zookeeper->bigdata.out
running master, logging to /root/trainings/hbase-2.0.1/logs/hbase-root-master-bigdata.out
: running regionserver, logging to /root/trainings/hbase-2.0.1/logs/hbase-root-regionserver-bigdata.out[root@bigdata ~ ]# jps
3745 Jps
3506 HRegionServer
2339 DataNode
2789 NodeManager
3429 HMaster
2678 ResourceManager
2519 SecondaryNameNode
2201 NameNode
1996 QuorumPeerMain
可以看到,HBase伪分布模式启动之后HMaster进程和HRegionServer进程在同一个节点上。
可以在网页上监控HBase的状态信息:端口号16010
4.2使用HBase shell
使用hbase shell命令可以进入HBase命令行模式:
[root@bigdata ~]# hbase shell
HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
Version 2.0.1, r987f7b6d37c2fcacc942cc66e5c5122aba8fdfbe, Wed Jun 13 12:03:55 PDT 2018
Took 0.0016 seconds
hbase(main):001:0> create 'tblStudent','Info','Grade'
Created table tblStudent
Took 1.1067 seconds
=> Hbase::Table - tblStudent
hbase(main):002:0> put 'tblStudent','stu001','Info:name','Tom'
Took 0.1482 seconds
hbase(main):003:0> put 'tblStudent','stu001','Info:age','25'
Took 0.0094 seconds
hbase(main):004:0> put 'tblStudent','stu001','Grade:chinese','88'
Took 0.0064 seconds
hbase(main):005:0> put 'tblStudent','stu001','Grade:math','90'
Took 0.0109 seconds
hbase(main):006:0> put 'tblStudent','stu002','Info:name','Jack'
Took 0.0040 seconds
hbase(main):007:0> put 'tblStudent','stu002','Info:age','23'
Took 0.0045 seconds
hbase(main):008:0> put 'tblStudent','stu002','Grade:english','78'
Took 0.0040 seconds
hbase(main):009:0> put 'tblStudent','stu002','Grade:math','60'
Took 0.0036 seconds
hbase(main):010:0> scan 'tblStudent'
ROW COLUMN+CELL
stu001 column=Grade:chinese, timestamp=1531751218073, value=88
stu001 column=Grade:math, timestamp=1531751223451, value=90
stu001 column=Info:age, timestamp=1531751211186, value=25
stu001 column=Info:name, timestamp=1531751200256, value=Tom
stu002 column=Grade:english, timestamp=1531751241605, value=78
stu002 column=Grade:math, timestamp=1531751253012, value=60
stu002 column=Info:age, timestamp=1531751236220, value=23
stu002 column=Info:name, timestamp=1531751229668, value=Jack
2 row(s)
Took 0.0266 seconds
hbase(main):011:0> quit
使用quit命令退出HBase命令行环境。
查看HDFS上/hbase目录下生成的数据:
[root@bigdata ~]# hdfs dfs -ls /hbase
Found 12 items
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/.hbck
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/.tmp
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/MasterProcWALs
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/WALs
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/archive
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/corrupt
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/data
-rw-r--r-- 1 root supergroup 42 2018-07-16 22:18 /hbase/hbase.id
-rw-r--r-- 1 root supergroup 7 2018-07-16 22:18 /hbase/hbase.version
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/mobdir
drwxr-xr-x - root supergroup 0 2018-07-16 22:18 /hbase/oldWALs
drwx--x--x - root supergroup 0 2018-07-16 22:18 /hbase/staging[root@bigdata ~]# hdfs dfs -ls /hbase/data/default/tblStudent
Found 3 items
drwxr-xr-x - root supergroup 0 2018-07-16 22:26 /hbase/data/default/tblStudent/.tabledesc
drwxr-xr-x - root supergroup 0 2018-07-16 22:26 /hbase/data/default/tblStudent/.tmp
drwxr-xr-x - root supergroup 0 2018-07-16 22:26 >/hbase/data/default/tblStudent/9a25032683426dbe07e9f17782e6c1e5
[root@bigdata ~]# /root/trainings/hadoop-2.7.3/bin/hdfs dfs -ls >/hbase/data/default/tblStudent/9a25032683426dbe07e9f17782e6c1e5
Found 4 items
-rw-r--r-- 1 root supergroup 45 2018-07-16 22:26 >/hbase/data/default/tblStudent/9a25032683426dbe07e9f17782e6c1e5/.regioninfo
drwxr-xr-x - root supergroup 0 2018-07-16 22:26 >/hbase/data/default/tblStudent/9a25032683426dbe07e9f17782e6c1e5/Grade
drwxr-xr-x - root supergroup 0 2018-07-16 22:26 >/hbase/data/default/tblStudent/9a25032683426dbe07e9f17782e6c1e5/Info
drwxr-xr-x - root supergroup 0 2018-07-16 22:26 >/hbase/data/default/tblStudent/9a25032683426dbe07e9f17782e6c1e5/recovered.edits
4.3停止HBase
[root@bigdata ~]# stop-hbase.sh
stopping hbase..........
bigdata: running zookeeper, logging to /root/trainings/hbase-2.0.1/bin/../logs/hbase-root-zookeeper->bigdata.out
bigdata: no zookeeper to stop because no pid file /tmp/hbase-root-zookeeper.pid
[root@bigdata ~]# jps
4366 Jps
本节介绍了HBase伪分布模式的环境搭建过程!祝你玩得愉快!