1. 程式人生 > >21 大資料--Hbase命令

21 大資料--Hbase命令

hbase提供了一個shell的終端給使用者互動(使用hbase)

[[email protected] bin]# ./hbase shell

退出使用quit和Ctril+C即可;

注:需要關閉hadoop的安全模式不然進行一些操作,比如scan會卡住
進入到hadoop的bin目錄下

[[email protected] bin]# hadoop dfsadmin -safemode leave

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、向表中新增記錄
create ‘表名’,’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}

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