1. 基本操作
- 进入HBase客户端命令行
[mayi@mayi101 hbase]$ bin/hbase shell
- 查看帮助命令
hbase(main):001:0> help
- 查看当前数据库中那些表
hbase(main):002:0> list
- 类似于show database
list_namespace
2. 表操作
- 创建表
hbase(main):002:0> create 'student','info'
- 插入数据到表
hbase(main):003:0> put 'student','1001','info:sex','male'
hbase(main):004:0> put 'student','1001','info:age','18'
hbase(main):005:0> put 'student','1002','info:name','Janna'
hbase(main):006:0> put 'student','1002','info:sex','female'
hbase(main):007:0> put 'student','1002','info:age','20'
- 扫描查看表数据
hbase(main):008:0> scan 'student'
hbase(main):009:0> scan 'student',{STARTROW => '1001', STOPROW => '1001'}
hbase(main):010:0> scan 'student',{STARTROW => '1001'}
- 查看表结构
hbase(main):011:0> describe 'student'
- 更新指定字段的数据
hbase(main):012:0> put 'student','1001','info:name','Nick'
hbase(main):013:0> put 'student','1001','info:age','100'
- 查看“指定行”或“指定列族:列”的数据
hbase(main):014:0> get 'student','1001'
hbase(main):015:0> get 'student','1001','info:name'
- 统计表数据行数
hbase(main):021:0> count 'student'
- 删除数据 删除某rowkey的全部数据:
hbase(main):016:0> deleteall 'student','1001'
删除某rowkey的某一列数据:
hbase(main):017:0> delete 'student','1002','info:sex'
- 清空表数据
hbase(main):018:0> truncate 'student'
提示:清空表的操作顺序为先disable,然后再truncate。
- 删除表 首先需要先让该表为disable状态:
hbase(main):019:0> disable 'student'
然后才能drop这个表:
hbase(main):020:0> drop 'student'
提示:如果直接drop表,会报错:ERROR: Table student is enabled. Disable it first.
- 变更表信息 将info列族中的数据存放3个版本:
hbase(main):022:0> alter 'student',{NAME=>'info',VERSIONS=>3}
hbase(main):022:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}
修改表的版本version数量:
alter 'student',{NAME=>'info',VERSIONS=>3}
关于VERSIONS的例子
- scan 'table1',{VERSIONS=>2}
- get 'table1','rowkey007',{COLUMN=>'f1:age',VERSIONS=>2}
- get 'table1', 'rowkey007',{COLUMN=>'f1',VERSIONS=>2}
修改表的版本version数量:
alter 'table1',{NAME=>'f1',VERSIONS=>5}
put 'table1','rowkey007','f1:age','51'
put 'table1','rowkey007','f1:age','52'
put 'table1','rowkey007','f1:age','53' 第六条数据插入,失效的数据是最早的age为35的数据
- put 'table1','rowkey007','f1:age','54'
修改列族为6个版本,还是能把失效的age为35的数据取到,
数据还没有被删除。
- alter 'table1',{NAME=>'f1',VERSIONS=>6}
关于TTL的例子
按照规定时间对数据进行超时间过期:
- TTL => 'FOREVER'永久不过期
- TTL => '60 SECONDS (1 MINUTE)' 一分钟前的数据过期
如果put后面的timestamp,是当前时间前1分钟的,满足表的失效条件,数据不会展示,因为数据是失效的。
- put 't1','rowkey001','f1:age','54'
- put 't1','rowkey007','f1:age','54'
- put 't1','rowkey007','f1:age','54',1564285634004 因为网络原因,如果我们拿着event时间,设定失效时间较短会导致数据一直进不了hbase表。 (当前的前一分钟之前的所有数据都应该失效)
- 1564285634004 => 2019-07-28 11:47:14 < 2019-07-28 11:49:19
- 1564285819854 => 2019-07-28 11:50:19
- 查看默认配置
hbase(main):042:0> describe 'student'
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '2', 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'}
1 row(s) in 0.2900 seconds