HBase的安装和使用

独立部署模式

设置java路径

编辑Hbase安装目录下conf/hbase-env.sh文件,设置JAVA_HOME
JAVA_HOME在该文件中被注释,取消注释,设置自己系统中java的路径

编辑conf/hbase-site.xml

添加如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///home/testuser/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/testuser/zookeeper</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
<description>
Controls whether HBase will check for stream capabilities (hflush/hsync).

Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
with the 'file://' scheme, but be mindful of the NOTE below.

WARNING: Setting this to false blinds you to potential data loss and
inconsistent system state in the event of process and/or node failures. If
HBase is complaining of an inability to use hsync or hflush it's most
likely not a false positive.
</description>
</property>
</configuration>

testuser为示例用户
不需要创建hbase目录,hbase会自动创建,如果自己创建,hbase会自动进行迁移

启动HBase

1
bin/start-hbase.sh

启动后使用jps可看到一个HMaster进程

使用HBase

连接

1
2
$ ./bin/hbase shell
hbase(main):001:0>

创建一个表

1
2
3
4
hbase(main):001:0> create 'test', 'cf'
0 row(s) in 0.4170 seconds

=> Hbase::Table - test

列出表的相关信息

1
2
3
4
5
6
hbase(main):002:0> list 'test'
TABLE
test
1 row(s) in 0.0180 seconds

=> ["test"]

查看表的详细信息

1
2
3
4
5
6
7
8
9
10
hbase(main):003:0> describe 'test'
Table test is ENABLED
test
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE =>
'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'f
alse', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE
=> '65536'}
1 row(s)
Took 0.9998 seconds

向表中放入数据

1
2
3
4
5
6
7
8
hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.0850 seconds

hbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0110 seconds

hbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0100 seconds

一次插入三个值。第一个插入位于row1列cf:a,值为value1。HBase中的列由列族前缀组成,列cf后跟冒号,然后是列限定符后缀,如a

一次扫描表格中的所有数据

1
2
3
4
5
6
hbase(main):006:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1421762485768, value=value1
row2 column=cf:b, timestamp=1421762491785, value=value2
row3 column=cf:c, timestamp=1421762496210, value=value3
3 row(s) in 0.0230 seconds

得到单行数据

1
2
3
4
hbase(main):007:0> get 'test', 'row1'
COLUMN CELL
cf:a timestamp=1421762485768, value=value1
1 row(s) in 0.0350 seconds

禁用数据表

如果要删除表或更改其设置,以及在某些其他情况下,您需要先使用该disable命令禁用该表。您可以使用该enable命令重新启用它

1
2
3
4
5
hbase(main):008:0> disable 'test'
0 row(s) in 1.1820 seconds

hbase(main):009:0> enable 'test'
0 row(s) in 0.1770 seconds

删除数据表

要删除表,请使用该drop命令。

1
2
hbase(main):011:0> drop'test'
0行,0.1370秒

删除之前先禁用该数据表

退出HBase Shell

要退出HBase Shell并断开与群集的连接,请使用该quit命令。HBase仍然在后台运行。

停止HBase

1
2
$ ./bin/stop-hbase.sh
stopping hbase....................

伪分布式本地安装

配置HBase。

编辑hbase-site.xml配置。首先,添加以下属性,该属性指示HBase以分布式模式运行,每个守护程序具有一个JVM实例。

1
2
3
4
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>

接下来,hbase.rootdir使用hdfs://URI语法将本地文件系统更改为HDFS实例的地址。在此示例中,HDFS在端口8020上的localhost上运行。请确保删除条目hbase.unsafe.stream.capability.enforce或将其设置为true。

1
2
3
4
<property>
<name>hbase.rootdir</name>
<value>hdfs://localhost:8020/hbase</value>
</property>

无需在HDFS中创建目录。HBase会自动创建。如果创建目录,HBase将尝试进行迁移

完整配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://master:9000/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/spark/data/zookeeper</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>true</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
</configuration>

启动HBase。

使用bin/start-hbase.sh命令启动HBase。如果您的系统配置正确,该jps命令应显示正在运行的HMaster和HRegionServer进程。

检查HDFS中的HBase目录。

如果一切正常,HBase在HDFS中创建了它的目录。在上面的配置中,它存储在HDFS上的/hbase/中。您可以使用hadoop fs 相关的命令列出此目录。

1
hadoop fs -ls /hbase

完全分布式安装

首先各主机之间要ssh无密码登录

可参考我之前的博客配置
我的三台机子为

Node Name Master ZooKeeper RegionServer
master yes yes no
slave1 backup yes yes
slave2 no yes yes

配置master主机

master主机运行主master和ZooKeeper进程,但不运行RegionServers。

编辑conf/regionservers并删除包含的行localhost。加入与从节点的主机名slave1和slave2

1
2
slave1
slave2

配置slave1用作备份主服务器。

conf目录下创建一个backup-masters的新文件,并使用主机名为其添加一个新行slave1

配置ZooKeeper

在master,编辑conf/hbase-site.xml并添加以下属性。

1
2
3
4
5
6
7
8
<property>
<name>hbase.zookeeper.quorum</name>
<value>master,slave1,slave2</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/usr/local/zookeeper</value>
</property>

其他主机都采用相同的配置

把master主机的配置文件复制到从节点上

启动HBase

在群集的每个节点上,运行该jps命令并验证每个服务器上是否正在运行正确的进程
master jps Output

1
2
3
4
$ jps
20355 Jps
20071 HQuorumPeer
20137 HMaster

slave1 jps Output

1
2
3
4
5
$ jps
15930 HRegionServer
16194 Jps
15838 HQuorumPeer
16010 HMaster

slave2 jps Output

1
2
3
4
$ jps
13901 Jps
13639 HQuorumPeer
13737 HRegionServer

从节点可能会没有HRegionServer进程,可能的原因是集群中的节点时间未同步,使用NTP进行时间同步即可,参考我的另一篇博客

打赏

请我喝杯咖啡吧~

支付宝
微信