1.3 hbase 搭建方式以及搭建流程

搭建方式:独立模式、伪分布式、完全分布式

实际上,您需要完全分布式配置才能完全测试 HBase 并在实际场景中使用它。在分布式配置中,群集包含多个节点,每个节点运行一个或多个 HBase 守护程序。其中包括主要和备用 Master,多个 Zookeeper 节点和多个 RegionServer 节点。

规划如下:

NodeName

Master

Zookeeper

RegionServer

node1

yes

no

no

node2

backup

yes

yes

node3

no

yes

yes

node4

no

yes

yes

搭建具体操作

1、配置四台主机免密钥

2、将 hbase 解压到 node1 的/opt

3、在/etc/profile 中配置 HBASE_HOME,并将 HBase 的 bin 目录添加到 PATH 中

hbase分布式搭建 hbase分布式部署_xml

4、删除/opt/hbase-0.98.12.1-hadoop2 中的 docs 目录

5、进入 conf,编辑 regionservers

a) 配置如下:

node2
node3
node4

6、配置 node2 为 backup master

a) 在 conf 中添加一个文件 backup-masters,并配置如下

[root@node1 conf]# echo "node2" >> backup-masters

7、在 conf/hbase-env.sh 中设置 JAVA_HOME 和 pid 目录

a)

hbase分布式搭建 hbase分布式部署_hbase分布式搭建_02

b) export HBASE_PID_DIR=/opt/hbase-0.98/pids

8、配置 zookeeper

a) 设置使用外部的 zookeeper

在 conf/hbase-env.sh 中设置 HBASE_MANAGES_ZK=false

b) 要么将 zoo.cfg 拷贝到 HBASE 的 CLASSPATH,要么在 hbase-site.xml 中配置zookeeper 的信息。hbase 会优先使用 zoo.cfg 的配置。在 hbase-site.xml 中配置如下:

<property>
	<name>hbase.cluster.distributed</name>
	<value>true</value>
</property>
<property>
	<name>hbase.rootdir</name>
	<value>hdfs://mycluster/hbase_ha</value>
</property>
<property>
	<!-- 默认端口号可以不写,也可以添加:
	node2:2181,node3:2181,node4:2181 -->
	<name>hbase.zookeeper.quorum</name>
	<value>node2,node3,node4</value>
</property>
<property>
	<!-- 可以不配置 -->
	<name>hbase.zookeeper.property.dataDir</name>
	<value>/var/bjsxt/zookeeper/data</value>
</property>

9、拷贝 hdfs-site.xml 到 HBASE_HOME/conf,当然,最好是做一个符号链接。

[root@node1 conf]# ln /opt/hadoop-2.6.5/etc/hadoop/hdfs-site.xml hdfs-site.xml

10、 将/opt/hadoop-0.98 拷贝到 node2,node3 和 node4。确保没有任意一个节点运行 hbase

11、 在 node2 上配置 hbase 的环境变量,生效后并将之拷贝 node3 和 node4 上。

12、 确保 zookeeper 集群正常运行,如果没有运行,则运行 zookeeper

13、 启动集群

在 node1 上执行 start-hbase.sh

hbase分布式搭建 hbase分布式部署_hadoop_03

14、 在各个节点使用 jps 验证运行的进程

a)

hbase分布式搭建 hbase分布式部署_hbase分布式搭建_04

b)

hbase分布式搭建 hbase分布式部署_hbase分布式搭建_05

c)

hbase分布式搭建 hbase分布式部署_xml_06

d)

hbase分布式搭建 hbase分布式部署_hadoop_07

15、 浏览 HBase 的 Web UI。

a) http://node1:60010

hbase分布式搭建 hbase分布式部署_hbase分布式搭建_08

hbase分布式搭建 hbase分布式部署_hadoop_09

16、 测试一下如果节点或服务消失会发生什么。

如果只配置了四个节点,HBase 还不是太有弹性。不过依然可以测试如果主HMaster 或者 HRegionServer 消失会发生什么。比如可以 kill 掉进程,查看 log日志等。

a) 杀死 node1 上的 HMaster 观察 web ui

http://node2:60010/master-status

hbase分布式搭建 hbase分布式部署_hadoop_10

b) 在 node1 启动 HMaster:hbase-daemon.sh start master访问 node1 的 web ui 界面:

http://node1:60010/master-status

hbase分布式搭建 hbase分布式部署_xml_11

c) 在 node2 上 kill 掉 HRegionServer,查看 web ui:

hbase分布式搭建 hbase分布式部署_hbase分布式搭建_12

d) 在 node2 上启动 HRegionServer:hbase-daemon.sh start regionserver,查看 web ui:

hbase分布式搭建 hbase分布式部署_hbase分布式搭建_13

客户端常用命令基本操作

hbase分布式搭建 hbase分布式部署_hadoop_14

1、连接 HBase:

shell> ./bin/hbase shell

2、在 hbase 的 shell 中输入 help 并回车,获取帮助:

hbase(main):001:0> help
在 hbase shell 中输入'help "COMMAND"'查看指定命令的帮助信息。(如,'help "get"'-- get 的引号是必须的)

命令是分组的,输入'help "COMMAND_GROUP"'查看命令组帮助信息。(如,'help"general"')

命令组:
    组名:general
    命令:status, table_help, version, whoami
    组名:ddl
    命令:alter, alter_async, alter_status, create, describe, disable, disable_all, drop,drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list,show_filters
    组名:namespace
    命 令 : alter_namespace, create_namespace, describe_namespace,drop_namespace, list_namespace, list_namespace_tables
    组名:dml
    命令:append, count, delete, deleteall, get, get_counter, get_splits, incr, put,
scan, truncate, truncate_preserve
    组名:tools
    命 令 : assign, balance_switch, balancer, catalogjanitor_enabled,
catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_rs,
flush, hlog_roll, major_compact, merge_region, move, split, trace, unassign,
zk_dump
    组名:replication
    命 令 : add_peer, disable_peer, disable_table_replication, enable_peer,
enable_table_replication, list_peers, list_replicated_tables, remove_peer,
set_peer_tableCFs, show_peer_tableCFs
组名:snapshots
    命 令 : clone_snapshot, delete_all_snapshot, delete_snapshot, list_snapshots,
restore_snapshot, snapshot
    组名:security
    命令:grant, revoke, user_permission
    组名:visibility labels
    命令:add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility

SHELL USAGE:
shell 用法:

将所有的名称如表名列名,用引号引起来。逗号分隔命令参数。回车执行命令。

在创建表或修改表的时候使用配置字典。配置字典是 Ruby 的 Hashes。看起来像这样:

{'key1' => 'value1', 'key2' => 'value2', ...}

用大括号引起来,键值分隔符是=>。通常键是预定义的常量,比如:NAME, VERSIONS,COMPRESSION 等。常量不需要括起来。输入'Object.constants'查看环境中所有常量。

如果用到了二进制的 key,使用双引号引起来的十六进制表示。例如:

hbase> get 't1', "key\x03\x3f\xcd"
hbase> get 't1', "key\003\023\011"
hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"

3、创建表

使用 create 命令创建表,必须指定表名和列族的名称

hbase(main):001:0> create 'test', 'cf'
0 row(s) in 0.4170 seconds
=> Hbase::Table - test

4、列出表的信息

使用 list 命令列出信息。

hbase(main):002:0> list 'test'
TABLE
test
1 row(s) in 0.0180 seconds
=> ["test"]

5、向 table 插入数据

使用 put 命令。

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。

6、一次性扫描表中的所有数据

要查询表中的数据,可以使用 scan 命令。可以在该命令中使用限定条件,下面获取了表中的所有数据:

hbase(main):016:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1568514191889, value=value1
row2 column=cf:b, timestamp=1568514198429, value=value2
row3 column=cf:c, timestamp=1568514205341, value=value3
3 row(s) in 0.0350 seconds

7、查询一行记录

使用 get 命令一次获取一行记录

hbase(main):017:0> get 'test', 'row1'
COLUMN CELL
cf:a timestamp=1568514191889, value=value1
1 row(s) in 0.0140 seconds

8、禁用一张表

如果要删除表或更改表的设置,需要先使用 disable 命令禁用该表,之后也可以使用enable 命令重新使用该表。

hbase(main):018:0> disable 'test'
0 row(s) in 1.3350 seconds
hbase(main):019:0> enable 'test'
0 row(s) in 0.2520 seconds
#再次使用 disable 命令禁用该表:
hbase(main):020:0> disable 'test'
0 row(s) in 1.2580 seconds

9、删除表

在表已经禁用的情况下,使用 drop 命令删除表

hbase(main):021:0> drop 'test'
0 row(s) in 0.1550 seconds

10、退出 HBase 的 shell

退出 shell 断开到集群的连接,可以使用 quit 命令。但是 hbase 服务进程仍然在后台运行。

hbase(main):022:0> quit
[root@node1 hbase-0.98.12.1-hadoop2]#

11、停止 hbase 服务进程

bin/start-hbase.sh 开启 hbase 的所有进程,bin/stop-hbase.sh 用于停止所有 hbase进程。

[root@node1 hbase-0.98.12.1-hadoop2]# bin/stop-hbase.sh
stopping hbase..................
[root@node1 hbase-0.98.12.1-hadoop2]#

12、使用 jps 查看进程信息,确保 hbase 的 HMaster 和 HRegionServer 进程成功关闭:

[root@node1 hbase-0.98.12.1-hadoop2]# jps
2831 Jps
[root@node1 hbase-0.98.12.1-hadoop2]#