hbase提供了一个shell的终端
[root@hadoop ~]# hbase shell
退出使用quit或者ctrl+c即可。
注:需要关闭hadoop的安全模式不然进行一些操作,比如scan会卡住
[root@hadoop ~]# 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
=> ["user"]
3、描述表
describe ‘表名’
hbase(main):014:0> describe 'user'
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 'user'
0 row(s) in 1.4110 seconds
hbase(main):005:0> drop 'user'
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 'user'
Table user does exist
0 row(s) in 0.0980 seconds
6、向表中添加记录
put ‘表名’,’rowkey’(键),’列族:列名’,’值’
hbase(main):011:0> put 'user','1234','info1:name','zhangsan'
0 row(s) in 0.1310 seconds
7、扫描表
扫描整张表
hbase(main):015:0> scan 'user'
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 'user','1234','info1:name','list'
0 row(s) in 0.1470 seconds
hbase(main):003:0> scan 'user'
ROW COLUMN+CELL
1234 column=info1:name, timestamp=1509304915052, value=list
但是如果rowkey相同,列族相同只要列名不同就只会添加而不会覆盖
比如插入年龄为18岁
hbase(main):004:0> put 'user','1234','info1:age','18'
0 row(s) in 0.0500 seconds
hbase(main):005:0> scan 'user'
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 'user'
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 'user'
2 row(s) in 0.0540 seconds
=> 2
9、查询
获取某个列族
获取某个列族的列
hbase(main):010:0> get 'user','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 'user','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 'user'
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 'user','1234','info1:name'
0 row(s) in 0.1160 seconds
hbase(main):014:0> scan 'user'
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 'user','1234'
0 row(s) in 0.0130 seconds
hbase(main):019:0> scan 'user'
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 'user'
Truncating 'user' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 1.5680 seconds
hbase(main):026:0> list
TABLE
user
1 row(s) in 0.1010 seconds
=> ["user"]
hbase(main):027:0> scan 'user'
ROW COLUMN+CELL
0 row(s) in 0.0400 seconds
网友评论