hbase提供了一个shell的终端给用户交互
[root@mini1 bin]# ./hbase shell
退出使用quit或者ctrl+c即可。
注:需要关闭hadoop的安全模式不然进行一些操作,比如scan会卡住
进入到hadoop的bin目录下
[root@mini1 bin]# hadoop dfsadmin -safemode leave
接下来可以使用hbase命令来进行操作了
1、创建表
create ‘表名’,’列族1’,’列族2’,…’列族n’
create 'user1','info1','info2'
2、查看所有表
list
hbase(main):002:0> list
1 row(s) in 1.4540 seconds
=> ["user1"]
3、描述表
describe ‘表名’
hbase(main):014:0> describe 'user1'
Table user1 is ENABLED
COLUMN FAMILIES DESCRIPTION
{NAME => 'info1', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KE
EP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info2', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KE
EP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0790 seconds
4、删除表
注:删除表之前需要先使表disable不然直接删除会报错,会提示先disable 表
disable ‘表名’
drop ‘表名’
hbase(main):004:0> disable 'user1'
0 row(s) in 1.4110 seconds
hbase(main):005:0> drop 'user1'
0 row(s) in 0.2330 seconds
hbase(main):006:0> list
TABLE
0 row(s) in 0.0450 seconds
=> []
5、判断表是否存在
hbase(main):013:0> exists 'user1'
Table user1 does exist
0 row(s) in 0.0980 seconds
6、向表中添加记录
put ‘表名’,’rowkey’(键),’列族:列名’,’值’
hbase(main):011:0> put 'user1','1234','info1:name','zhangsan'
0 row(s) in 0.1310 seconds
7、扫描表
扫描整张表
hbase(main):015:0> scan 'user1'
ROW COLUMN+CELL
1234 column=info1:name, timestamp=1509303796190, value=zhangsan
1 row(s) in 0.0630 seconds
注:hbase没有直接修改操作,但是可以覆盖,只要rowkey跟列族列名一致就会覆盖
比如这里要修改上面插入数据的info:name为’lisi’
hbase(main):002:0> put 'user1','1234','info1:name','list'
0 row(s) in 0.1470 seconds
hbase(main):003:0> scan 'user1'
ROW COLUMN+CELL
1234 column=info1:name, timestamp=1509304915052, value=list
但是如果rowkey相同,列族相同只要列名不同就只会添加而不会覆盖
比如插入年龄为18岁
hbase(main):004:0> put 'user1','1234','info1:age','18'
0 row(s) in 0.0500 seconds
hbase(main):005:0> scan 'user1'
ROW COLUMN+CELL
1234 column=info1:age, timestamp=1509305017108, value=18
1234 column=info1:name, timestamp=1509304915052, value=list
8、查询记录数
注:rowkey相同的话只算一条
count ‘表名’
hbase(main):008:0> scan 'user1'
ROW COLUMN+CELL
1234 column=info1:age, timestamp=1509305121972, value=18
1234 column=info1:name, timestamp=1509304915052, value=list
12345 column=info1:age, timestamp=1509305156151, value=24
12345 column=info2:name, timestamp=1509305221477, value=lucy
2 row(s) in 0.0460 seconds
hbase(main):009:0> count 'user1'
2 row(s) in 0.0540 seconds
=> 2
9、查询
获取某个列族
获取某个列族的列
hbase(main):010:0> get 'user1','1234','info1'
COLUMN CELL
info1:age timestamp=1509305121972, value=18
info1:name timestamp=1509304915052, value=list
2 row(s) in 0.0970 seconds
hbase(main):011:0> get 'user1','1234','info1:name'
COLUMN CELL
info1:name timestamp=1509304915052, value=list
1 row(s) in 0.0700 seconds
查询某个列,某个时间戳版本的值
get 'user1', '1234bbb', {COLUMN => 'info1:username',TIMESTAMP => 1538014481194}
Hbase中多版本(version)数据获取办法
参考https://blog.csdn.net/liuchuanhong1/article/details/53895234/
···
alter 'user',{NAME=>'info1',VERSIONS=>3}
get 'user','1234',{COLUMN=>'info1:name',VERSIONS=>3}
···
9、删除记录
可以删除一个列族的一个列记录
delete ‘表名’ ,‘rowkey’ , ‘列族:列’
也可以删除一整行
deleteall ‘表名’,’rowkey;
hbase(main):012:0> scan 'user1'
ROW COLUMN+CELL
1234 column=info1:age, timestamp=1509305121972, value=18
1234 column=info1:name, timestamp=1509304915052, value=list
12345 column=info1:age, timestamp=1509305156151, value=24
12345 column=info2:name, timestamp=1509305221477, value=lucy
2 row(s) in 0.0930 seconds
hbase(main):013:0> delete 'user1','1234','info1:name'
0 row(s) in 0.1160 seconds
hbase(main):014:0> scan 'user1'
ROW COLUMN+CELL
1234 column=info1:age, timestamp=1509305121972, value=18
12345 column=info1:age, timestamp=1509305156151, value=24
12345 column=info2:name, timestamp=1509305221477, value=lucy
2 row(s) in 0.0430 seconds
hbase(main):018:0> deleteall 'user1','1234'
0 row(s) in 0.0130 seconds
hbase(main):019:0> scan 'user1'
ROW COLUMN+CELL
12345 column=info1:age, timestamp=1509305156151, value=24
12345 column=info2:name, timestamp=1509305221477, value=lucy
1 row(s) in 0.0570 seconds
10、清空表
truncate ‘表名’
hbase(main):025:0> truncate 'user1'
Truncating 'user1' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 1.5680 seconds
hbase(main):026:0> list
TABLE
user1
1 row(s) in 0.1010 seconds
=> ["user1"]
hbase(main):027:0> scan 'user1'
ROW COLUMN+CELL
0 row(s) in 0.0400 seconds
谢谢