Hbase基本命令

启动hbase:start-hbase.sh

停止hbase:stop-hbase.sh

 

 

进入hbase命令行端口:hbase shell

查看帮助信息:help

查看服务器状态:status

查看版本信息:version

查看当前用户:whoami

查看表相关命令的帮助信息:table_help

 

创建表

常用语法:create  '[namespacename:]tablename' [ ,'columnfamilyname1' [  ,'columnfamilyname2, ....]]

详细语法使用help -create 查看create语句

hbase(main):006:0> create 'test','fam1','fam2' 
0 row(s) in 8.3530 seconds 
  
=> Hbase::Table – test

 

使用list命令列出表名:

常用语法:list   '[namespacename:]tablename]

详细语法可以使用help 'list' 查看,list 后面可以跟正则表达式,列出符合表达式的表名

hbase(main):011:0> list 'test' –列出名为test的表名 
TABLE                                                                                                                                                              
test                                                                                                                                                               
1 row(s) in 0.0110 seconds 
  
=> ["test"] 
hbase(main):012:0> list 'tes.*'—列出以名称tes开头的表名 
TABLE                                                                                                                                                              
test                                                                                                                                                               
test:testtable                                                                                                                                                       
2 row(s) in 0.0150 seconds 
  
=> ["test","test:testtable"] 
hbase(main):013:0> list 'default:.*' –列出default命名空间下的表名 
TABLE                                                                                                                                                               
ambarismoketest                                                                                                                                                    
test                                                                                                                                                               
2 row(s) in 0.0150 seconds 
  
=> ["ambarismoketest","test"] 
hbase(main):014:0> list 'test:.*' –列出test这个命名空间下的表名 
TABLE                                                                                                                                                              
test:testtable                                                                                                                                                       
1 row(s) in 0.0140 seconds 
  
=> ["test:testtable"]

 

列出数据库名:list_namespace

 

hbase(main):002:0> list_namespace
NAMESPACE                                                        
default                                                          
hbase                                                            
2 row(s) in 0.5620 seconds

 

使用desc命令查看指定表的具体信息(desc等同于describe)

语法:desc '[namespacename:]tablename'

          describe '[namespacename:]tablename'

详细语法使用help  'describe'查看

hbase(main):016:0> desc 'test'  查看test这张表的信息 
Table test is ENABLED                                                                                                                                                
test                                                                                                                                                               
COLUMN FAMILIES DESCRIPTION                                                                                                                                        
{NAME => 'fam1', BLOOMFILTER =>'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS =>'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRE 
SSION => 'NONE', MIN_VERSIONS => '0',BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =>'0'}                                                         
{NAME => 'fam2', BLOOMFILTER =>'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS =>'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRE 
SSION => 'NONE', MIN_VERSIONS => '0',BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =>'0'}                                                         
2 row(s) in 0.1710 seconds 
  
hbase(main):017:0> desc 'test:testtable'查看test命名空间下testtable这张表的信息 
Table test:testtable is ENABLED                                                                                                                                     
test:testtable                                                                                                                                                      
COLUMN FAMILIES DESCRIPTION                                                                                                                                         
{NAME => 'colfam1', BLOOMFILTER =>'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS =>'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COM 
PRESSION => 'NONE', MIN_VERSIONS =>'0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =>'0'}                                                      
{NAME => 'colfam2', BLOOMFILTER =>'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS =>'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COM 
PRESSION => 'NONE', MIN_VERSIONS =>'0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =>'0'}                                                       
{NAME => 'colfam3', BLOOMFILTER =>'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS =>'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COM 
PRESSION => 'NONE', MIN_VERSIONS =>'0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =>'0'}                                                      
3 row(s) in 0.0290 seconds

 

使用put插入数据

常用语法:put '[namespacename:]tablename' , 'rowkey' , 'columnfamilyname[:column]' , 'value'

详细语法使用help 'put'查看

hbase(main):002:0> put'test','row2','fam1:a','value' 
0 row(s) in 0.3760 seconds 
hbase(main):003:0> put 'test','row1','fam1:a','value' 
0 row(s) in 0.3760 seconds 
hbase(main):004:0> put 'test','row1','fam2:a','value' 
0 row(s) in 0.3760 seconds 
hbase(main):005:0> put'test','row2','fam2:a','value' 
0 row(s) in 0.3760 seconds

 

使用scan查看全表数据

常用语法:scan '[namespacename:]tablename'

详细语法使用help 'scan'查看

hbase(main):002:0> scan 'test' 
ROW                                       COLUMN+CELL                                                                                                             
 row1                                     column=fam1:a, timestamp=1504767451331, value=value                                                                      
 row1                                     column=fam2:a, timestamp=1504767471977, value=value                                                                      
 row2                                     column=fam1:a, timestamp=1504767600686, value=value                                                                      
 row2                                     column=fam2:a, timestamp=1504767715014, value=value                                                                      
2 row(s) in 0.1120 seconds

 

查看指定列的数据:

hbase(main):004:0> scan 'test',{COLUMNS=> 'fam1:a'} 
ROW                                       COLUMN+CELL                                                                                                              
 row1                                     column=fam1:a, timestamp=1504767451331, value=value                                                                      
 row2                                      column=fam1:a,timestamp=1504767600686, value=value                                                                      
2 row(s) in 0.2390 seconds 
  
hbase(main):021:0> get 'test','row1' 
COLUMN                                     CELL                                                                                                                    
 fam1:a                                   timestamp=1504768075731, value=\x00\x00\x00\x00\x00\x00\x00\x02                                                          
 fam2:a                                   timestamp=1504767471977, value=value                                                                                     
2 row(s) in 0.1110 seconds

查看多个多个版本数据:get 'table1', 'rowkey007',{COLUMN=>'f1',VERSIONS=>2}

 

修改表的版本version数量:
alter 'table1',{NAME=>'f1',VERSIONS=>5}

 

使用get查询数据:

语法:get  '[namespacename:]tablename' , 'rowkey' [, 'columnfamliyname[:column]']

详细语法使用help 'get'查看

hbase(main):024:0> get'test','row1','fam1' 
COLUMN                                     CELL                                                                                                                    
 fam1:a                                   timestamp=1504768075731, value=\x00\x00\x00\x00\x00\x00\x00\x02                                                          
1 row(s) in 0.0690 seconds 
hbase(main):026:0> get 'test','row1','fam1:a' 
COLUMN                                     CELL                                                                                                                    
 fam1:a                                   timestamp=1504768075731, value=\x00\x00\x00\x00\x00\x00\x00\x02                                                          
1 row(s) in 0.0430 seconds

 

使用delete删除数据

常用语法:delete '[namespacename:]tablename' , 'rowkey' , 'columnfamliyname:column'

当表中存在只制定了columnfamily没有具体制定column的数据时,delete可以这么用

delete '[namespacename:]tablename' , 'rowkey' , 'columnfamliyname',不存在这样的数据,使用这样的语句不会删除数据。

更详细的语法使用help 'delete'查看

hbase(main):008:0> delete'test','row1','fam1:a' 
0 row(s) in 0.3370 seconds 
  
hbase(main):009:0> scan 'test' 
ROW                                       COLUMN+CELL                                                                                                             
 row1                                     column=fam2:a, timestamp=1504767471977, value=value                                                                      
 row2                                     column=fam1:a, timestamp=1504767600686, value=value                                                                      
 row2                                     column=fam2:a, timestamp=1504767715014, value=value                                                                      
2 row(s) in 0.0850 seconds

 

使用incr增加数据(默认使用counter实现递增)

常用语法:incr '[namespacename:]tablename' , 'rowkey' , 'columnfamilyname:column'

更详细的语法使用help 'incr'查看

hbase(main):012:0> incr'test','row1','fam1:a' 
COUNTER VALUE = 1
0 row(s) in 1.1700 seconds 
  
hbase(main):013:0> scan 'test' 
ROW                                       COLUMN+CELL                                                                                                             
 row1                                      column=fam1:a,timestamp=1504768054892, value=\x00\x00\x00\x00\x00\x00\x00\x01                                           
 row1                                     column=fam2:a, timestamp=1504767471977, value=value                                                                       
 row2                                     column=fam1:a, timestamp=1504767600686, value=value                                                                      
 row2                                      column=fam2:a, timestamp=1504767715014,value=value                                                                      
2 row(s) in 0.0410 seconds 
  
hbase(main):014:0> incr'test','row1','fam1:a' 
COUNTER VALUE = 2
0 row(s) in 0.0480 seconds 
  
hbase(main):015:0> scan 'test' 
ROW                                       COLUMN+CELL                                                                                                             
 row1                                     column=fam1:a, timestamp=1504768075731,value=\x00\x00\x00\x00\x00\x00\x00\x02                                           
 row1                                     column=fam2:a, timestamp=1504767471977, value=value                                                                      
 row2                                     column=fam1:a, timestamp=1504767600686, value=value                                                                      
 row2                                     column=fam2:a, timestamp=1504767715014, value=value                                                                      
2 row(s) in 0.0770 seconds

 

使用get_counter查看当前的counter值:

hbase(main):027:0> get_counter'test','row1','fam1:a' 
COUNTER VALUE = 2

 

使用put更新数据:

hbase(main):029:0> scan 'test' 
ROW                                        COLUMN+CELL                                                                                                             
 row1                                     column=fam1:a, timestamp=1504768075731,value=\x00\x00\x00\x00\x00\x00\x00\x02                                           
 row1                                     column=fam2:a, timestamp=1504767471977, value=value                                                                      
 row2                                     column=fam1:a, timestamp=1504767600686, value=value                                                                      
 row2                                     column=fam2:a, timestamp=1504767715014, value=value                                                                      
2 row(s) in 0.0270 seconds 
  
hbase(main):030:0> put'test','row1','fam1:a','newvalue' 
0 row(s) in 0.0250 seconds 
  
hbase(main):031:0> scan 'test' 
ROW                                        COLUMN+CELL                                                                                                             
 row1                                     column=fam1:a, timestamp=1504768397480, value=newvalue                                                                    
 row1                                     column=fam2:a, timestamp=1504767471977, value=value                                                                      
 row2                                     column=fam1:a, timestamp=1504767600686, value=value                                                                      
 row2                                     column=fam2:a, timestamp=1504767715014, value=value                                                                      
2 row(s) in 0.0760 seconds

 

使用is_enabled/is_disabled查看表是否被启用/禁用

hbase(main):032:0> is_enable 'test' 
NoMethodError: undefined method `is_enable'for #<Object:0x15d114ce> 
  
hbase(main):033:0> is_enabled 'test' 
true                                                                                                                                                               
0 row(s) in 0.1180 seconds 
  
hbase(main):034:0> is_disabled 'test' 
false                                                                                                                                                              
0 row(s) in 0.0190 seconds

 

使用disable禁用表

hbase(main):001:0> disable 'test' 
0 row(s) in 4.7170 seconds 
  
hbase(main):002:0> is_disabled 'test' 
true                                                                                                                                                               
0 row(s) in 0.0210 seconds

 

使用enbale启用表

hbase(main):003:0> enable 'test' 
0 row(s) in 2.2910 seconds 
  
hbase(main):004:0> is_enabled 'test' 
true                                                                                                                                                               
0 row(s) in 0.0180 seconds

 

删除fam1这个columnfamily:

hbase(main):007:0> alter'test',{NAME=>'fam1',METHOD=>'delete'} 
Updating all regions with the new schema...
1/1 regions updated. 
Done.
0 row(s) in 2.1980 seconds 
  
hbase(main):008:0> desc 'test' 
Table test is ENABLED                                                                                                                                               
test                                                                                                                                                               
COLUMN FAMILIES DESCRIPTION                                                                                                                                        
{NAME => 'fam2', BLOOMFILTER => 'ROW',VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE',DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRE 
SSION => 'NONE', MIN_VERSIONS => '0',BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =>'0'}                                                          
1 row(s) in 0.0350 seconds

 

清空表数据:

hbase(main):009:0> truncate 'test' 
Truncating 'test' table (it may take awhile): 
 -Disabling table... 
 -Truncating table... 
0 row(s) in 3.6540 seconds 
  
hbase(main):010:0> scan 'test' 
ROW                                       COLUMN+CELL                                                                                                             
0 row(s) in 0.0460 seconds

 

删除表(必须先disable表才能drop表):

hbase(main):011:0> disable 'test' 
0 row(s) in 2.2680 seconds 
  
hbase(main):012:0> drop 'test' 
0 row(s) in 1.2680 seconds 
  
hbase(main):014:0> list 'test' 
TABLE                                                                                                                                                              
0 row(s) in 0.0110 seconds 
  
=> []

 

参考博客:

https://blog.csdn.net/weixin_34234721/article/details/89806302

posted @ 2019-11-04 10:16  xd_xumaomao  阅读(323)  评论(0编辑  收藏  举报