1. 程式人生 > >15分鐘熟悉HBase Shell命令

15分鐘熟悉HBase Shell命令

轉自:https://blog.csdn.net/tanggao1314/article/details/51387560

下面我們看看HBase Shell的一些基本操作命令,我列出了幾個常用的HBase Shell命令,如下:

名稱

命令表示式

建立表

create '表名稱', '列名稱1','列名稱2','列名稱N'

新增記錄      

put '表名稱', '行名稱', '列名稱:', '值'

檢視記錄

get '表名稱', '行名稱'

查看錶中的記錄總數

count  '表名稱'

刪除記錄

delete  '表名' ,'行名稱' , '列名稱'

刪除一張表

先要遮蔽該表,才能對該表進行刪除,第一步 disable '表名稱' 第二步  drop '表名稱'

檢視所有記錄

scan "表名稱"  

檢視某個表某個列中所有資料

scan "表名稱" , ['列名稱:']

更新記錄 

就是重寫一遍進行覆蓋

 

一、一般操作

1.查詢伺服器狀態

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

 

2.查詢版本

hbase(main):012:0> version
1.2.1, r8d8a7107dc4ccbf36a92f64675dc60392f85c015, Wed Mar 30 11:19:21 CDT 2016


 

二、DDL操作

1.建立一個表

hbase(main):013:0> create 'table','column_famaly','column_famaly1','column_famaly2'
0 row(s) in 94.9160 seconds

 

=> Hbase::Table - table

 

 

2.列出所有表

hbase(main):014:0> list
TABLE                                                                           
stu                                                                             
table                                                                           
test                                                                            
3 row(s) in 0.0570 seconds


=> ["stu", "table", "test"]
 

 

 

3.獲得表的描述

hbase(main):015:0> describe 'table'
Table table is ENABLED                                                          
table                                                                           
COLUMN FAMILIES DESCRIPTION                                                     
{NAME => 'column_famaly', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', R
EPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =>
 '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_
MEMORY => 'false', BLOCKCACHE => 'true'}                                        
{NAME => 'column_famaly1', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', 
REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN
_MEMORY => 'false', BLOCKCACHE => 'true'}                                       
{NAME => 'column_famaly2', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', 
REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN
_MEMORY => 'false', BLOCKCACHE => 'true'}                                       
3 row(s) in 0.0430 seconds



 

4.刪除一個列族     alter,disable,enable

hbase(main):016:0> alter 'table',{NAME=>'column_famaly',METHOD=>'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 3.0220 seconds


hbase(main):018:0> describe 'table'
Table table is ENABLED                                                          
table                                                                           
COLUMN FAMILIES DESCRIPTION                                                     
{NAME => 'column_famaly1', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', 
REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN
_MEMORY => 'false', BLOCKCACHE => 'true'}                                       
{NAME => 'column_famaly2', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', 
REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN
_MEMORY => 'false', BLOCKCACHE => 'true'}                                       
2 row(s) in 0.0520 seconds

 

 

5.drop一個表

 

hbase(main):019:0> drop 'stu'


ERROR: Table stu is enabled. Disable it first.


Here is some help for this command:
Drop the named table. Table must first be disabled:
  hbase> drop 't1'
  hbase> drop 'ns1:t1'


報錯了,因為要把表格設定為disable 

 

6.把表設定為disable 

hbase(main):020:0> disable 'stu'
0 row(s) in 2.3150 seconds


再刪除一個表
hbase(main):021:0> drop 'stu'
0 row(s) in 1.2820 seconds


列出所有表
hbase(main):022:0> list
TABLE                                                                           
table                                                                           
test                                                                            
2 row(s) in 0.0240 seconds


=> ["table", "test"]

 

7.查詢表是否存在

hbase(main):023:0> exists 'stu'
Table stu does not exist                                                        
0 row(s) in 0.0380 seconds


hbase(main):024:0> exists 'table'
Table table does exist                                                          
0 row(s) in 0.0280 seconds



 

8.判斷表是否enable

hbase(main):025:0> is_enabled 'table'
true                                                                            
0 row(s) in 0.0150 seconds

 

9.判斷表是否disable

hbase(main):026:0> is_disabled 'table'
false                                                                           
0 row(s) in 0.0140 seconds


把表設為disable 
hbase(main):027:0> disable 'table'
0 row(s) in 33.2980 seconds


hbase(main):028:0> is_disabled 'table'
true                                                                            
0 row(s) in 0.0140 seconds
 

 

三、DML操作

 

 

 

1.插入幾條記錄

hbase(main):029:0> put 'table','id','column_famaly1:name','tanggao'


ERROR: Failed 1 action: NotServingRegionException: 1 time, 


Here is some help for this command:
Put a cell 'value' at specified table/row/column and optionally
timestamp coordinates.  To put a cell value into table 'ns1:t1' or 't1'
at row 'r1' under column 'c1' marked with the time 'ts1', do:


  hbase> put 'ns1:t1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value', ts1
  hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'}


The same commands also can be run on a table reference. Suppose you had a reference
t to table 't1', the corresponding command would be:


  hbase> t.put 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}


報錯了,因為要把表設為enable 
hbase(main):030:0> enable 'table'
0 row(s) in 1.3620 seconds




插入幾條記錄
hbase(main):031:0> put 'table','id','column_famaly1:name','tanggao'
0 row(s) in 0.0460 seconds


hbase(main):032:0> put 'table','id','column_famaly1:age','20'
0 row(s) in 0.0150 seconds


hbase(main):033:0> put 'table','id','column_famaly1:sex','boy'
0 row(s) in 0.0190 seconds

 

2.獲取一條資料

獲取一個id的所有資料

hbase(main):034:0> get 'table','id'
COLUMN                CELL                                                      
 column_famaly1:age   timestamp=1463055735107, value=20                         
 column_famaly1:name  timestamp=1463055709542, value=tanggao                    
 column_famaly1:sex   timestamp=1463055753395, value=boy                        
3 row(s) in 0.3200 seconds



 

獲取一個id,一個列族的所有資料

hbase(main):035:0> get 'table','id','column_famaly1'
COLUMN                CELL                                                      
 column_famaly1:age   timestamp=1463055735107, value=20                         
 column_famaly1:name  timestamp=1463055709542, value=tanggao                    
 column_famaly1:sex   timestamp=1463055753395, value=boy                        
3 row(s) in 0.0270 seconds

 

獲取一個id,一個列族中一個列的所有資料

hbase(main):036:0> get 'table','id','column_famaly1:name'
COLUMN                CELL                                                      
 column_famaly1:name  timestamp=1463055709542, value=tanggao                    
1 row(s) in 0.0140 seconds



 

3.更新一條記錄

把id的age修改為22
hbase(main):037:0> put 'table','id','column_famaly1:age','22'
0 row(s) in 0.0160 seconds


hbase(main):038:0> get 'table','id','column_famaly1:age'
COLUMN                CELL                                                      
 column_famaly1:age   timestamp=1463055893492, value=22                         
1 row(s) in 0.0190 seconds


 


4.通過timestamp來獲取兩個版本的資料

hbase(main):039:0> get 'table','id',{COLUMN=>'column_famaly1:age',TIMESTAMP=>1463055735107}
COLUMN                CELL                                                      
 column_famaly1:age   timestamp=1463055735107, value=20                         
1 row(s) in 0.0340 seconds


hbase(main):040:0> get 'table','id',{COLUMN=>'column_famaly1:age',TIMESTAMP=>1463055893492}
COLUMN                CELL                                                      
 column_famaly1:age   timestamp=1463055893492, value=22                         
1 row(s) in 0.0140 seconds



 

5.全表掃描

hbase(main):041:0> scan 'table'
ROW                   COLUMN+CELL                                               
 id                   column=column_famaly1:age, timestamp=1463055893492, value=
                      22                                                        
 id                   column=column_famaly1:name, timestamp=1463055709542, value
                      =tanggao                                                  
 id                   column=column_famaly1:sex, timestamp=1463055753395, value=
                      boy                                                       
1 row(s) in 0.1520 seconds



 

6.刪除行健為id的值的‘column_famaly1:age’欄位

hbase(main):042:0> delete 'table','id','column_famaly1:age'
0 row(s) in 0.0200 seconds


hbase(main):043:0> get 'table','id'
COLUMN                CELL                                                      
 column_famaly1:name  timestamp=1463055709542, value=tanggao                    
 column_famaly1:sex   timestamp=1463055753395, value=boy                        
2 row(s) in 0.2430 seconds



 

7.刪除整行

hbase(main):044:0> deleteall 'table','id'
0 row(s) in 0.0550 seconds



 

8.查詢表中有多少行

hbase(main):045:0> count 'table'
0 row(s) in 0.0450 seconds

 

=> 0

 

再插入幾條記錄

hbase(main):046:0> put 'table','id','column_famaly1:age','20'
0 row(s) in 0.0160 seconds


hbase(main):047:0> put 'table','id','column_famaly1:name','tanggao'
0 row(s) in 0.0120 seconds


hbase(main):048:0> put 'table','id','column_famaly2:name','tanggao2'
0 row(s) in 0.0120 seconds



hbase(main):001:0> put 'table','id','column_famaly2:age','22'
0 row(s) in 0.4690 seconds



 

9.給‘id’這個行健增加'column_famaly1:addr'欄位,並使用counter實現遞增

hbase(main):002:0> incr 'table','id','column_famaly1:addr'
COUNTER VALUE = 1
0 row(s) in 0.0340 seconds




hbase(main):003:0> incr 'table','id','column_famaly1:addr'
COUNTER VALUE = 2
0 row(s) in 9.6250 seconds


hbase(main):004:0> get 'table','id','column_famaly1:addr'
COLUMN                CELL                                                      
 column_famaly1:addr  timestamp=1463056705124, value=\x00\x00\x00\x00\x00\x00\x0
                      0\x02                                                     
1 row(s) in 0.3930 seconds


hbase(main):005:0> incr 'table','id','column_famaly1:addr'
COUNTER VALUE = 3
0 row(s) in 7.3880 seconds



 

10.獲取當前count的值

hbase(main):006:0> get_counter 'table','id','column_famaly1:addr'
COUNTER VALUE = 3



 

11.將整張表清空

hbase(main):007:0> truncate 'table'
Truncating 'table' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 4.1510 seconds

hbase(main):008:0>