Apache HBase 集群安装文档


简介:

Apache HBase 是一个分布式的、面向列的开源 NoSQL 数据库。具有高性能、高可靠性、可伸缩、面向列、分布式存储的特性。

HBase 的数据文件最终落地在 HDFS 之上,所以在 Hadoop 集群中,DataNode 节点都需安装 HBase Worker Node。

另外,HBase 受 ZooKeeper 管理,还需安装 ZooKeeper 单机或集群。建议 HBase Master 节点不要与集群中其余 Master 节点安装在同一台物理服务器。

HBase Master 处理 HBase 集群环境下的很多核心功能,协调管理 RegionServer、故障转移、重新分配等

  HBase Master  处理 HBase 集群环境下的很多核心功能,协调管理 RegionServer、故障转移、重新分配等
  
    LoadBalancer     进程分发 Region 到多个 RegionServer
    Catalog Janitor  进程检查不可用的 Region、进行垃圾回收
    Log Cleaner      进程用于删除陈旧的 WAL 文件

  RegionServer  处理数据移动 (get、scan、存储put提交的数据、标记delete数据)、紧缩操作 (小紧缩、主紧缩)、维护 Block Cache、管理 MemStore 和 WAL、接收来自 Master 的新 Region
  
    CompactSplitThread  查找需要拆分的 Region (大于 filesize 的 Region)、处理小紧缩
    MajoyCompactionChecker  检查是否需要主紧缩
    MemStoreFlusher  检查 MemStore 是否满足被刷出到磁盘
    LogRoller  用于关闭 WAL 并创建一个新文件


官方地址:http://hbase.apache.org
官方文档:http://hbase.apache.org/book.html
下载地址:http://mirror.bit.edu.cn/apache/hbase/stable/hbase-1.2.6-bin.tar.gz

HBase Shell -> HMaster(HBase Master) -> HRegionServer(HBase RegionServer) -> Region -> HFile(HDFS File)

一、安装 HBase

shell > wget http://mirror.bit.edu.cn/apache/hbase/stable/hbase-1.2.6-bin.tar.gz
shell > tar zxf hbase-1.2.6-bin.tar.gz -C /usr/local/

shell > chown -R hadoop.hadoop /usr/local/hbase-1.2.6

二、配置 HBase

1、编辑 hbase-env.sh

# cd /usr/local/hbase-1.2.6
hadoop shell > vim conf/hbase-env.sh
# 指定 java 目录
export JAVA_HOME=/usr/java/default
# 管理自身 ZooKeeper
export HBASE_MANAGES_ZK=false

2、编辑 hbase-site.xml

hadoop shell > vim conf/hbase-site.xml

<configuration>

    <property>
        <name>hbase.cluster.distributed</name>
        <value>true</value>
    </property>

    <property>
        <name>hbase.rootdir</name>
        <value>hdfs://master.hadoop:8020/hbase</value>
    </property>

    <property>
        <name>hbase.zookeeper.quorum</name>
        <value>datanode01.hadoop,datanode02.hadoop,datanode03.hadoop</value>
    </property>

</configuration>

# hbase.cluster.distributed  是否启用集群模式
# hbase.rootdir  HBase 数据根目录
# hbase.zookeeper.quorum  ZooKeeper 节点

3、编辑 regionservers

hadoop shell > vim conf/regionservers

datanode01.hadoop
datanode02.hadoop
datanode03.hadoop

# 指定 regionserver 节点

三、同步目录、设置环境变量

shell > ansible datanode -m synchronize -a 'src=/usr/local/hbase-1.2.6 dest=/usr/local/'

shell > echo 'export PATH=$PATH:/usr/local/hbase-1.2.6/bin' >> /etc/profile && source /etc/profile

shell > ansible datanode -m shell -a 'echo "export PATH=$PATH:/usr/local/hbase-1.2.6/bin" >> /etc/profile && source /etc/profile'

四、启动服务

1、ZooKeeper

shell > ansible datanode -m shell -a '/usr/local/zookeeper-3.4.10/bin/zkServer.sh start'

2、Hadoop

hadoop shell > sh sbin/start-all.sh

3、HBase

hadoop shell > sh bin/start-hbase.sh

# 启动服务后,master.hadoop 主机会启动一个 HMaster 的进程,DataNode 主机会启动一个 HRegionServer 的进程,可用 jps 查看
# HDFS 文件系统上会创建相应的目录,可用 hdfs dfs -ls / 查看,位置即 hbase.rootdir 参数指定
# ZooKeeper 上也会创建相应的 Znode,可用 zkCli.sh -server IP:PORT,IP:PORT,IP:PORT 连接 ZooKeeper Server 通过 ls / 查看
# 并且可以通过 WEB UI 来查看 HMaster、HRegionServer 运行状态、表、Region 数量等
# HMaster:http://HMaster:16010  HRegionServer: http://HRegionServer:16030  注意,0.98.x 版本之前端口为 60010、60030

五、HBase shell

1、获取帮助

hadoop shell > hbase shell

hbase(main):001:0> help
HBase Shell, version 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.

COMMAND GROUPS:
  Group name: general
  Commands: status, table_help, version, whoami

  Group name: ddl
  Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, locate_region, show_filters

  Group name: namespace
  Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables

  Group name: dml
  Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve
...

# 截取了部分输出
# 输入 help 指令即可打印出帮助信息,有各类的指令 general、ddl、namespace、dml 等

# XShell 连接服务器,HBase Shell 不能回退的问题,1、Ctrl+Backspace 组合键解决 2、XShell 会话 -> 属性 -> 终端 -> 键盘 -> BACKSPACE 键序列改为 ASCII 127(Ctrl+?)(I) 即可

hbase(main):002:0> help 'general'
Command: status
Show cluster status. Can be 'summary', 'simple', 'detailed', or 'replication'. The
default is 'summary'. Examples:

  hbase> status
  hbase> status 'simple'
  hbase> status 'summary'
  hbase> status 'detailed'
  hbase> status 'replication'
  hbase> status 'replication', 'source'
  hbase> status 'replication', 'sink'

Command: table_help
Help for table-reference commands.

# 进一步指定 help 'command',可以获取指定命令的帮助信息,如 help 'alter',注意:help 后面的指令要用 '' 包裹

2、namespace 命名空间

hbase(main):003:0> help 'namespace'  # 获取 namespace 帮助信息( 输出就不截出了,以防文章太长 )

hbase(main):004:0> list_namespace    # 打印默认的 namespace
NAMESPACE                                                                                                                                           
default                                                                                                                                             
hbase                                                                                                                                               
2 row(s) in 0.0220 seconds

# namespace 类似传统关系型数据库中 Database 的概念,用于表的划分,可以为 namespace 单独修改、回收、授权等操作
# hbase 为系统表的命名空间,用户创建表如果不指定命名空间,将默认创建到 default namespace 中

hbase(main):005:0> describe_namespace 'hbase'  # 查看命令空间结构
DESCRIPTION                                                                                                                                         
{NAME => 'hbase'}                                                                                                                                   
1 row(s) in 0.0180 seconds

# create_namespace 'namespace'、alter_namespace 'namespace'、drop_namespace 'namespace' 创建、修改、删除命令空间

3、table

hbase(main):006:0> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
0 row(s) in 4.7550 seconds

=> Hbase::Table - t1

# default 命名空间下创建一张名为 t1,列族为 f1、f2、f3 的表,简化写法:create 't2', 'f1', 'f2', 'f3'
# HBase 创建表时至少要指定表名跟列族名,单张表至少有一个列族,建议最多不要超过 3 个,列族可以包含无数个列,且创建表时无需事先定义,列族可以单独设置属性,列族名尽量要短

hbase(main):007:0> desc 't1'
Table t1 is ENABLED                                                                                                                                 
t1                                                                                                                                                  
COLUMN FAMILIES DESCRIPTION                                                                                                                         
{NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FO
REVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                           
{NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FO
REVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                           
{NAME => 'f3', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FO
REVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                           
3 row(s) in 0.1230 seconds

# 查看 t1 表结构
# BLOOMFILTER 基于行布隆过滤,加快检索速度
# VERSIONS 列族数据保存的版本数
# IN_MEMORY 是否常驻内存
# KEEP_DELETED_CELLS 是否保留删除数据
# TTL 数据超时时间,FOREVER 为永久保存
# MIN_VERSIONS 保留最小版本数
# BLOCKCACHE 启用 BLOCKCACHE
# BLOCKSIZE HBase 列族的 BLOCKSIZE,默认 65536 字节,即 64k 细粒度的块尺寸,能够带来更好的随机读写性能
# REPLICATION_SCOPE 是否开启复制

hbase(main):008:0> put 't1', 'r001', 'f1', 'hbase data'
0 row(s) in 0.0060 seconds

# put 指令用于添加、更新数据,put '表名', '行键', '列族', '值', '时间戳 可选'
# RowKey 行键一行一个,设计不适当会引起热点问题,行键不可以修改

hbase(main):009:0> get 't1', 'r001', 'f1'
COLUMN                                 CELL                                                                                                         
 f1:                                   timestamp=1497333134783, value=hbase data                                                                    
1 row(s) in 0.0110 seconds

# get 指定用于获取数据,get '表名', '行键', '列族'

hbase(main):010:0> alter 't1', NAME => 'f1', VERSIONS => 3
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 3.7170 seconds

# 修改列族 f1 的版本数为 3,保留三个版本

hbase(main):011:0> scan 't1'
ROW                                    COLUMN+CELL                                                                                                  
 r001                                  column=f1:, timestamp=1497333134783, value=hbase data                                                        
1 row(s) in 0.0120 seconds

# scan 扫描表 t1,目前仅列族 f1 中有一行数据,值为 hbase data

hbase(main):012:0> put 't1', 'r001', 'f1:name', 'wang'
0 row(s) in 0.0070 seconds

hbase(main):013:0> put 't1', 'r001', 'f1:name', 'xiao'
0 row(s) in 0.0080 seconds

hbase(main):014:0> put 't1', 'r001', 'f1:name', 'qiang'
0 row(s) in 0.0120 seconds

hbase(main):015:0> scan 't1', VERSIONS => 3
ROW                                    COLUMN+CELL                                                                                                  
 r001                                  column=f1:, timestamp=1497333134783, value=hbase data                                                        
 r001                                  column=f1:name, timestamp=1497335852617, value=qiang                                                         
 r001                                  column=f1:name, timestamp=1497335848825, value=xiao                                                          
 r001                                  column=f1:name, timestamp=1497335845596, value=wang                                                          
1 row(s) in 0.0180 seconds

# 向表 't1' 中添加一列 name,更新了三个值
# 由于列族 f1 的 VERSIONS 为 3,所以可以保留三个版本的值,多版本是按时间倒叙排列,不指定版本时,默认返回最新的版本
# 可以向列族 f2 中添加列,多次更新某个字段,看保留的版本数

hbase(main):016:0> alter 't1', NAME => 'f1', TTL => 10
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 3.2350 seconds

# 将表 't1' 中的列族 'f1' TTL 设为 10 秒,查看数据有什么变化

hbase(main):017:0> put 't1', 'r001', 'f1:name', 'wangxiaoqiang'

hbase(main):018:0> get 't1', 'r001'
COLUMN                                 CELL                                                                                                         
 f1:name                               timestamp=1497339843881, value=wangxiaoqiang                                                                 
 f2:img                                timestamp=1497338098754, value=http://img.com/qiang.png                                                      
2 row(s) in 0.0090 seconds

hbase(main):019:0> get 't1', 'r001'
COLUMN                                 CELL                                                                                                         
 f2:img                                timestamp=1497338098754, value=http://img.com/qiang.png                                                      
1 row(s) in 0.0070 seconds

# 10秒过后,数据消失,因为该列族的 TTL 为 10秒,且 MIN_VERSIONS 为默认的 0,TTL 过后默认最小保留 0 个版本

hbase(main):020:0> alter 't1', NAME => 'f1', TTL => 2147483647
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 3.0490 seconds

# 当列族 TTL 的值设为 2147483647 时,表示数据不过期,等于默认的 FOREVER

hbase(main):021:0> get 't1', 'r001'
COLUMN                                 CELL                                                                                                         
 f1:age                                timestamp=1497349829445, value=man                                                                           
 f1:name                               timestamp=1497349810888, value=wangxiaoqiang                                                                                                              
 f2:img                                timestamp=1497338098754, value=http://img.com/qiang.png                                                      
3 row(s) in 0.0060 seconds

hbase(main):022:0> delete 't1', 'r001', 'f1:age'
0 row(s) in 0.0100 seconds

# 删除一列数据

hbase(main):023:0> alter 't1', 'delete' => 'f1'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 4.3090 seconds

# 删除列族

hbase(main):124:0> deleteall 't1', 'r002'
0 row(s) in 0.0050 seconds

# 删除一行数据

hbase(main):126:0> disable 't1'
0 row(s) in 4.4470 seconds

hbase(main):127:0> drop 't1'
0 row(s) in 2.5450 seconds

# 禁用表,删除表
# 启用表:enable 't1'

hbase(main):001:0> list
TABLE                                                                                                                                               
0 row(s) in 0.2200 seconds

=> []

# 查看当前 namespace 中的表

六、停止 HBase

hadoop shell > cd /usr/local/hbase-1.2.6
hadoop shell > sh bin/stop-hbase.sh

# END

原文地址:https://www.cnblogs.com/wangxiaoqiangs/p/7003066.html