1. 程式人生 > >HBase常用shell命令

HBase常用shell命令

1. Group name: general

        1) status: 檢視hbase中master狀態

hbase(main):004:0> status
1 active master, 0 backup masters, 1 servers, 0 dead, 27.0000 average load

        2) version: 檢視hbase版本

hbase(main):005:0> version
1.3.0, re359c76e8d9fd0d67396456f92bcbad9ecd7a710, Tue Jan  3 05:31:38 MSK 2017

        3) whoami: 檢視當前使用者

hbase(main):006:0> whoami
llh (auth:SIMPLE)
    groups: llh, adm, cdrom, sudo, dip, plugdev, lpadmin, sambashare

2. Group name: namespace

        1)  create_namespace: 建立名稱空間

hbase(main):010:0> create_namespace 'test'
0 row(s) in 1.0160 seconds

        2) drop_namespace: 刪除名稱空間

hbase(main):011:0> drop_namespace 'test'
0 row(s) in 0.8820 seconds

        3) list_namespace: 顯示所有名稱空間

hbase(main):012:0> list_namespace
NAMESPACE                                                                       
default                                                                         
hbase                                                                           
2 row(s) in 0.0080 seconds

        4) describe_namespace: 顯示單個名稱空間的詳細資訊

hbase(main):013:0> describe_namespace 'hbase'
DESCRIPTION                                                                     
{NAME => 'hbase'}                                                               
1 row(s) in 0.0060 seconds

3. Group name: ddl

        1) create: 建立單個表

hbase(main):002:0> create 'tb','cf1'
0 row(s) in 1.3180 seconds

=> Hbase::Table - tb
hbase(main):005:0> create 'test:tb1','cf1'
0 row(s) in 1.2320 seconds

=> Hbase::Table - test:tb1

        2) list: 顯示所有表

hbase(main):009:0> list
TABLE                                                                                                                                                                                                                     
test:tb1                                                                        
tb                                                                    
2 row(s) in 0.0420 seconds

=> ["tb", "test:tb1"]

        3) describe: 顯示單個表的詳細資訊

hbase(main):010:0> describe 'test:tb1'
Table test:tb1 is ENABLED                                                       
test:tb1                                                                        
COLUMN FAMILIES DESCRIPTION                                                     
{NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEE
P_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COM
PRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '655
36', REPLICATION_SCOPE => '0'}                                                  
1 row(s) in 0.1440 seconds

        4) disable: 關閉單個表

hbase(main):011:0> disable 'test:tb1'
0 row(s) in 2.2720 seconds

        5) enable: 開啟單個表 

hbase(main):012:0> enable 'test:tb1'
0 row(s) in 1.2620 seconds

        6) drop: 刪除單個表

hbase(main):014:0> disable 'tb'
0 row(s) in 2.2450 seconds

hbase(main):015:0> drop 'tb'  
0 row(s) in 1.2630 seconds

        7) alter: 修改表結構

hbase(main):016:0> alter 'test:tb1', NAME=>'cf1', TTL=>'3600'       //修改列簇為cf1資料保留兩個小時
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9330 seconds

hbase(main):017:0> alter 'test:tb1', NAME=>'cf2', VERSIONS=>5       //改變或新增一個列簇
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.8890 seconds

hbase(main):019:0> alter 'test:tb1', NAME=>'cf2', METHOD=>'delete'  //刪除一個列簇
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.1470 seconds

        8) alter_async: 非同步執行alter

        9) alter_status: 檢視alter執行的狀態

hbase(main):026:0> alter_status 'test:tb1'
1/1 regions updated.
Done.

        10) is_enabled: 驗證指定表是否有效

hbase(main):020:0> is_enabled 'test:tb1'
true                                                                            
0 row(s) in 0.0150 seconds

        11) exists: 檢視指定表是否存在

hbase(main):021:0> exists 'test:tb1'
Table test:tb1 does exist                                                       
0 row(s) in 0.0080 seconds

        12) locate_region: 檢視region分佈情況

hbase(main):022:0> locate_region 'test:tb1','cf1'
HOST                  REGION                                                    
 ebi-llh:16020        {ENCODED => 5f4d2d31db8eaaeb39cfd978b0234054, NAME => 'tes
                      t:tb1,,1521473386612.5f4d2d31db8eaaeb39cfd978b0234054.', S
                      TARTKEY => '', ENDKEY => ''}                              
1 row(s) in 0.0250 seconds

        13) show_filters: 顯示所有過濾器(*)

hbase(main):023:0> show_filters
DependentColumnFilter                                                           
KeyOnlyFilter                                                                   
ColumnCountGetFilter

4. Group name: dml

        1) put: 插入或更新資料

hbase(main):001:0> put 'test:tb1','10001','cf1:name','jack'
0 row(s) in 0.3390 seconds

        2) delete: 刪除單條資料

hbase(main):002:0> delete 'test:tb1','10001','cf1:name'
0 row(s) in 0.0240 seconds

        3) deleteall: 刪除多條資料

hbase(main):007:0> deleteall 'test:tb1','10001'
0 row(s) in 0.0070 seconds

        4) get: 獲取資料

hbase(main):005:0> get 'test:tb1','10001'
COLUMN                CELL                                                      
 cf1:age              timestamp=1521942283487, value=22                         
 cf1:name             timestamp=1521942276491, value=Jack                       
1 row(s) in 0.0160 seconds

hbase(main):006:0> get 'test:tb1','10001','cf1:age'
COLUMN                CELL                                                      
 cf1:age              timestamp=1521942283487, value=22                         
1 row(s) in 0.0130 seconds

hbase(main):007:0> get 'test:tb1','10001',{TIMERANGE=>[1521942283487,1521942283488]}
COLUMN                CELL                                                      
 cf1:age              timestamp=1521942283487, value=22                         
1 row(s) in 0.0070 seconds

hbase(main):008:0> get 'test:tb1','10001',{COLUMN=>['cf1:name']}                     //等效於cf1:name
COLUMN                CELL                                                      
 cf1:name             timestamp=1521942276491, value=Jack                       
1 row(s) in 0.0070 seconds

hbase(main):009:0> get 'test:tb1','10001',{FILTER=>"ValueFilter(=,'substring:22')"}  //獲取值包含22的資料
COLUMN                CELL                                                      
 cf1:age              timestamp=1521942283487, value=22                         
1 row(s) in 0.0430 seconds

hbase(main):010:0> get 'test:tb1','10001',{FILTER=>"ValueFilter(=,'binary:22')"}    //獲取值等於22的資料

COLUMN                CELL                                                      
 cf1:age              timestamp=1521942283487, value=22                         
1 row(s) in 0.0160 seconds

        5) scan 掃描獲取資料

hbase(main):017:0> scan 'test:tb1'
ROW                   COLUMN+CELL                                               
 10001                column=cf1:age, timestamp=1521942283487, value=22         
 10001                column=cf1:name, timestamp=1521942276491, value=Jack      
1 row(s) in 0.0160 seconds

hbase(main):018:0> scan 'test:tb1',{COLUMNS=>'cf1:age'}
ROW                   COLUMN+CELL                                               
 10001                column=cf1:age, timestamp=1521942283487, value=22         
1 row(s) in 0.0130 seconds

hbase(main):019:0> scan 'test:tb1',{COLUMNS=>'cf1:age', STARTROW=>'2', LIMIT=>1}   //獲取列為age,rowkey以100開始的1條資料
ROW                   COLUMN+CELL                                               
0 row(s) in 0.0090 seconds

hbase(main):021:0> scan 'test:tb1',{TIMERANGE=>[1521942276491,1521942276492]}     //獲取插入時間戳範圍為[ts1,ts2]的資料
ROW                   COLUMN+CELL                                               
 10001                column=cf1:name, timestamp=1521942276491, value=Jack      
1 row(s) in 0.0110 seconds

hbase(main):023:0> scan 'test:tb1',{FILTER=>"PrefixFilter('100')"}                //獲取rowkey包含100的資料
ROW                   COLUMN+CELL                                               
 10001                column=cf1:age, timestamp=1521942283487, value=22         
 10001                column=cf1:name, timestamp=1521942276491, value=Jack      
1 row(s) in 0.0100 seconds

hbase(main):024:0> scan 'test:tb1',{FILTER=>"ValueFilter(=,'substring:22')"}     //獲取值包含22的資料
ROW                   COLUMN+CELL                                               
 10001                column=cf1:age, timestamp=1521942283487, value=22         
1 row(s) in 0.0090 seconds

        6) count: 統計表總數(慎重使用)

hbase(main):027:0> count 'test:tb1', INTERVAL=>10, CACHE=>1000   
1 row(s) in 0.0270 seconds

=> 1

        7) append: 追加,假如該列不存在新增新列,存在將值追加到最後

hbase(main):028:0> append 'test:tb1','10001','cf1:name','_back'  //追加,假如該列不存在新增新列,存在將值追加到最後
0 row(s) in 0.0250 seconds

hbase(main):029:0> get 'test:tb1','10001','cf1:name'
COLUMN                CELL                                                      
 cf1:name             timestamp=1521943322296, value=Jack_back                  
1 row(s) in 0.0080 seconds

        8) truncate: 清空表(disable-->drop-->create)

hbase(main):030:0> truncate 'test:tb1'                         
Truncating 'test:tb1' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 4.1460 seconds

        9) truncate_preserve: 清空表但保留分割槽

hbase(main):031:0> truncate_preserve 'test:tb1'
Truncating 'test:tb1' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.3780 seconds

        10) *incr: 增加指定錶行或列的值(不會用)

        11) *get_counter: 統計指定的行數(不會用)

5. Group name: tools

        1) flush: 重新整理資料

hbase(main):028:0> flush 'test:tb1'
0 row(s) in 0.2550 seconds