HBase
1.概念
HBase 是一个基于Hadoop的
分布式
可扩展
巨大数据
仓库 当用户需要对海量数据进行实时
(时效性)随机
(记录级别数据)读/写 用户可以使用Hbase Hbase设计目标是能够持有一张巨大的表 该表的规模能达到数十亿行 * 数百亿列
并且可以运行在商用的硬件集群之上 Hbase是一个开源的
分布式
版本化
的非关系化的数据库-NOSQL 该设计仿照了Google的BigTable设计
2.列存储
将IO特性相似的列归为一个簇 HBase底层在检索的时候以列簇为最小文件加载单位
Hbase中所有的记录都是按照一定顺序排列的
ROWKEY --> 列簇 --> 列名字 --> 时间戳
默认查询返回时间戳最新版本记录
-
列存储的优缺点
优点:
- 支持稀疏存储 null值不存
- 磁盘IO利用率100%
缺点:
- 值包含ROWKEY/列信息/时间戳
-
定义
- ROWKEY:等价关系型数据库的主键id
- 列簇:将IO操作特性相似的列归为一个簇 Hbase底层会以列簇为单位索引数据
- 列:列簇/列名/列值/时间戳构成
- 时间戳:用于记录Hbase中数据的版本 一般系统会自动指定为插入数据时间
3.Shell命令-掌握
Namespace-数据库
修改namespace:alter_namespace
创建namespace:create_namespace
查看namespace详情:describe_namespace
删除namespace:drop_namespace
查看所有的namespace:list_namespace
查看namespace的表:list_namespace_tables
表的操作
-
查看所有表
hbase(main):021:0> list TABLE baizhi:t_employee 1 row(s) in 0.0470 seconds => []
-
创建表
hbase(main):007:0> create 'user:t_employee',{NAME=>'cf1'},{NAME=>'cf2'} 0 row(s) in 2.5770 seconds => Hbase::Table - baizhi:t_employee
-
查看table详情
hbase(main):022:0> desc 'user:t_employee' Table baizhi:t_employee is ENABLED baizhi:t_employee COLUMN FAMILIES DESCRIPTION {NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} {NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 2 row(s) in 0.2030 seconds
VERSIONS
:保留数据版本 默认值1
TTL
:列簇下列存活时间 默认是FOREVER
BLOCKCACHE
:是否开启缓存 用于加快读
IN_MEMORY
:设置是否将列簇下所欲数据加载内存中 加速读写 默认值false
BLOOMFILTER
:配置布隆过滤器(一种数据文件过滤机制)默认值ROW 可选值两个ROW|ROWCOL 度过修改为ROWCOL 系统需要额外开销存储列信息作为过滤文件的索引 -
禁用表
hbase(main):027:0> disable_all 'user:t_u.*' baizhi:t_user Disable the above 1 tables (y/n)? y 1 tables successfully disabled
-
启用表
hbase(main):028:0> enable_all 'user:t_u.*' user:t_employee Enable the above 1 tables (y/n)? y 1 tables successfully enabled
-
截断表
hbase(main):034:0> truncate 'user:t_employee' Truncating 'user:t_employee' table (it may take a while): - Disabling table... - Truncating table... 0 row(s) in 3.3620 seconds
-
删除表
hbase(main):035:0> drop 'user:t_employee' ERROR: Table user:t_employee is enabled. Disable it first. hbase(main):037:0> disable 'user:t_employee' 0 row(s) in 2.2450 seconds hbase(main):038:0> drop 'user:t_employee' 0 row(s) in 1.2590 seconds
DML操作
-
put
hbase(main):012:0> put 'user:t_employee','001','cf1:name','xiaohei' 0 row(s) in 0.0300 seconds hbase(main):012:0> put 'user:t_employee','001','cf1:age','20' 0 row(s) in 0.0300 seconds hbase(main):012:0> put 'user:t_employee','002','cf1:name','xiaohua' 0 row(s) in 0.0300 seconds hbase(main):012:0> put 'user:t_employee','001','cf1:age','21' 0 row(s) in 0.0300 seconds
-
get
hbase(main):023:0> get 'baizhi:t_employee','001' COLUMN CELL cf1:name timestamp=1578914502620, value=xiaohei cf1:name timestamp=1578914471071, value=20 cf1:age timestamp=1578914019293, value=xiaohua cf1:age timestamp=1578914440417, value=21 4 row(s) in 0.2820 seconds
-
delete/deleteall
# 删除指定版本以前的所有cell hbase(main):024:0> delete 'user:t_employee','001','cf1:name',1578914502620 0 row(s) in 0.1140 seconds # 删除cf1:age的所有单元格 hbase(main):025:0> delete 'user:t_employee','001','cf1:age' 0 row(s) in 0.0130 seconds # 删除所有数据 hbase(main):027:0> deleteall 'user:t_employee','001' 0 row(s) in 0.0140 seconds hbase(main):030:0> get 'user:t_employee','001' COLUMN CELL 0 row(s) in 0.0220 seconds
-
scan
hbase(main):031:0> scan 'user:t_employee' ROW COLUMN+CELL 002 column=cf1:deptno, timestamp=1578914749636, value=10 002 column=cf1:name, timestamp=1578914718280, value=wangwu 002 column=cf1:sal, timestamp=1578914731110, value=6000.00 002 column=cf2:name, timestamp=1578914049182, value=lisi 003 column=cf1:deptno, timestamp=1578914801299, value=10 003 column=cf1:name, timestamp=1578914774544, value=lisi 003 column=cf1:sal, timestamp=1578914787810, value=4000.00 2 row(s) in 0.1720 seconds