HBase shell详情

概述

HBase 为用户提供了一个非常方便的使用方式,我们称之为"HBase Shell"。

HBase Shell提供了大多数的HBase命令,通过HBase Shell用户可以方便地创建、删除及修改表,还可以向表中添加数据、列出表中的相关信息等。

备注:写错HBase Shell命令时用键盘上的"Ctrl+Backspace"进行删除,"Backspace" 不起作用,另外结尾不能有任何符号。

在启动HBase之后,用户可以通过下面的命令进入HBase Shell之中,命令如下图所示:

[root@liaozhongmin5 local]# hbase shell
HBase Shell; enter 'help' for list of supported commands.
Type "exit" to leave the HBase Shell
Version 0.94.7, r1471806, Wed Apr 24 18:44:36 PDT 2013

hbase(main):001:0> 
具体的HBase Shell命令如下表所示:

HBase shell详情_第1张图片

一般操作

1.查看服务器状态

hbase(main):028:0> status
1 servers, 0 dead, 6.0000 average load

hbase(main):029:0> 

2.查看HBase版本

hbase(main):029:0> version
0.94.7, r1471806, Wed Apr 24 18:44:36 PDT 2013

hbase(main):030:0> 

DDL操作

1.create 命令

创建一个具有三个列族"member_id"和"address"即"info"的表"member",其中表名、行和列都要用单引号括起来,并以逗号隔开。

hbase(main):025:0> create 'member','member_id','address','info'
0 row(s) in 1.1320 seconds

hbase(main):026:0> 

注:语句最后不能有分号,加入不小心多写了个分号,则可以使用qui命令退出错误控制台,如下:

hbase(main):030:0> list;
hbase(main):031:0* 
hbase(main):032:0* qui
TABLE                                                                                                                                                     
member                                                                                                                                                    
membermember_id                                                                                                                                           
scores                                                                                                                                                    
tb_myHbase                                                                                                                                                
users                                                                                                                                                     
5 row(s) in 0.1300 seconds

NameError: undefined local variable or method `qui' for #

hbase(main):033:0> 


2.list命令

查看当前HBase中具有哪些表

hbase(main):026:0> list
TABLE                                                                                                                                                     
member                                                                                                                                                    
scores                                                                                                                                                    
tb_myHbase                                                                                                                                                
users                                                                                                                                                     
4 row(s) in 0.0520 seconds

hbase(main):027:0> 


3.describe 命令

查看表的描述信息。

hbase(main):028:0> describe 'member'
DESCRIPTION                                                                                         ENABLED                                               
 'member', {NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SC true                                                  
 OPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP                                                       
 _DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', B                                                       
 LOCKCACHE => 'true'}, {NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLI                                                       
 CATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '21474836                                                       
 47', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK =>                                                        
 'true', BLOCKCACHE => 'true'}, {NAME => 'member_id', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER =>                                                       
  'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TT                                                       
 L => '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCO                                                       
 DE_ON_DISK => 'true', BLOCKCACHE => 'true'}                                                                                                              
1 row(s) in 0.0320 seconds

hbase(main):029:0> 

4.删除一个列族:disable,alter,enable

我们之前建了3个列族,如果觉得member_id这个列族是多余的,要将其删除。

alter 'member','delete'=>'member_id'

会出现如下错误:

ERROR: org.apache.hadoop.hbase.TableNotDisabledException: org.apache.hadoop.hbase.TableNotDisabledException: member
解决方案:先disable这张表

hbase(main):004:0> disable 'member' 
0 row(s) in 2.0960 seconds

hbase(main):005:0> 
删除刚才的列族

alter 'member','delete'=>'member_id'
查看表情况

hbase(main):017:0> describe 'member'
DESCRIPTION                                                                                         ENABLED                                               
 'member', {NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SC false                                                 
 OPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP                                                       
 _DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', B                                                       
 LOCKCACHE => 'true'}, {NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLI                                                       
 CATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '21474836                                                       
 47', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK =>                                                        
 'true', BLOCKCACHE => 'true'}                                                                                                                            
1 row(s) in 0.0490 seconds
我们可以发现member_id字段已经被删除了。

启用表

hbase(main):021:0> enable 'member'
0 row(s) in 2.0860 seconds



5.删除一个表 disable,drop

先disable表,再drop删除表,然后用list检查

hbase(main):022:0> disable 'member'
0 row(s) in 2.1140 seconds

hbase(main):023:0> drop 'member'
0 row(s) in 1.1670 seconds

hbase(main):024:0> list
TABLE                                                                                                                                                     
scores                                                                                                                                                    
tb_myHbase                                                                                                                                                
users                                                                                                                                                     
3 row(s) in 0.0250 seconds

hbase(main):025:0> 

6.查询表是否存在

hbase(main):026:0> exists 'member'
Table member does not exist                                                                                                                               
0 row(s) in 0.0740 seconds

hbase(main):027:0> 

7.判断表是否enable

hbase(main):028:0> is_enable 'users'
NoMethodError: undefined method `is_enable' for #

hbase(main):029:0> is_enabled 'users'
true                                                                                                                                                      
0 row(s) in 0.0060 seconds

hbase(main):030:0> disable 'users'
0 row(s) in 2.0880 seconds

hbase(main):031:0> is_enabled 'users'
false                                                                                                                                                     
0 row(s) in 0.0040 seconds

hbase(main):032:0> 
注:另外还有一个命令与他对应即判断是否disable:is_disabled '表名'



DML操作

1.插入数据

put 'member','scutshuxue','info:age','24'
put 'member','scutshuxue','info:birthday','1987-06-17
put 'member','scutshuxue','info:company','alibaba'
put 'member','scutshuxue','address:contry','china'
put 'member','scutshuxue','address:province','zhejiang'
put 'member','scutshuxue','address:city','hangzhou'
put 'member','xiaofeng','info:birthday','1987-4-17'
put 'member','xiaofeng','info:favorite','movie'
put 'member','xiaofeng','info:company','alibaba'
put 'member','xiaofeng','address:contry','china'
put 'member','xiaofeng','address:province','guangdong'
put 'member','xiaofeng','address:city','jieyang'
put 'member','xiaofeng','address:town','xianqiao'
注:插入语句后面一定不要有空格。


2.获取一条数据

hbase(main):012:0> get 'member','xiaofeng'
COLUMN                                  CELL                                                                                                              
 address:city                           timestamp=1423056496524, value=jieyang                                                                            
 address:contry                         timestamp=1423056496475, value=china                                                                              
 address:province                       timestamp=1423056496496, value=guangdong                                                                          
 address:town                           timestamp=1423056499535, value=xianqiao                                                                           
 info:birthday                          timestamp=1423056496348, value=1987-4-17                                                                          
 info:company                           timestamp=1423056496447, value=alibaba                                                                            
 info:favorite                          timestamp=1423056496386, value=movie                                                                              
7 row(s) in 0.0780 seconds

hbase(main):013:0> 

3.获取一条记录中某个列族的信息

hbase(main):013:0> get 'member','xiaofeng','info'
COLUMN                                  CELL                                                                                                              
 info:birthday                          timestamp=1423056496348, value=1987-4-17                                                                          
 info:company                           timestamp=1423056496447, value=alibaba                                                                            
 info:favorite                          timestamp=1423056496386, value=movie                                                                              
3 row(s) in 0.0160 seconds

hbase(main):014:0> 

4.获取一条记录的某个列族中的某个列的信息

hbase(main):015:0> get 'member','xiaofeng','info:company'
COLUMN                                  CELL                                                                                                              
 info:company                           timestamp=1423056496447, value=alibaba                                                                            
1 row(s) in 0.0130 seconds

hbase(main):016:0> 


5.更新一条记录

把xiaofeng的年龄改为25岁

hbase(main):001:0> get 'member','xiaofeng','info:age'
COLUMN                                  CELL                                                                                                              
 info:age                               timestamp=1423057077614, value=23                                                                                 
1 row(s) in 0.7080 seconds

hbase(main):002:0> put 'member','xiaofeng','info:age','25'
0 row(s) in 0.0180 seconds

hbase(main):003:0> get 'member','xiaofeng','info:age'
COLUMN                                  CELL                                                                                                              
 info:age                               timestamp=1423057248381, value=25                                                                                 
1 row(s) in 0.0110 seconds

hbase(main):004:0> 

6.根据TimeStamp来获取记录

hbase(main):007:0> get 'member','xiaofeng',{column=>'info:age',timestamp=>1423057248381}
NameError: undefined local variable or method `column' for #

hbase(main):008:0> get 'member','xiaofeng',{COLUMN=>'info:age',TIMESTAMP=>1423057248381}
COLUMN                                  CELL                                                                                                              
 info:age                               timestamp=1423057248381, value=25                                                                                 
1 row(s) in 0.0070 seconds
注:要注意大小写,另外TIMESTAMP后面的时间戳不用单引号。

7.根据版本号获取记录

hbase(main):025:0> get 'member','xiaofeng',{COLUMN=>'info:company',VERSION=>3}
COLUMN                                  CELL                                                                                                              
 info:company                           timestamp=1423056496447, value=alibaba                                                                            
1 row(s) in 0.0100 seconds

hbase(main):026:0> 



8.全表扫描

hbase(main):009:0> scan 'member'
ROW                                     COLUMN+CELL                                                                                                       
 lavimer                                column=address:city, timestamp=1423056054519, value=ganzhou                                                       
 lavimer                                column=info:name, timestamp=1423056214559, value=liaozhongmin                                                     
 scutshuxue                             column=address:city, timestamp=1423056496301, value=hangzhou                                                      
 scutshuxue                             column=address:contry, timestamp=1423056399271, value=china                                                       
 scutshuxue                             column=address:province, timestamp=1423056496250, value=zhejiang                                                  
 scutshuxue                             column=info:age, timestamp=1423056337733, value=24                                                                
 scutshuxue                             column=info:birthday, timestamp=1423056367576, value=1987-06-17                                                   
 scutshuxue                             column=info:company, timestamp=1423056370340, value=alibaba                                                       
 xiaofeng                               column=address:city, timestamp=1423056496524, value=jieyang                                                       
 xiaofeng                               column=address:contry, timestamp=1423056496475, value=china                                                       
 xiaofeng                               column=address:province, timestamp=1423056496496, value=guangdong                                                 
 xiaofeng                               column=address:town, timestamp=1423056499535, value=xianqiao                                                      
 xiaofeng                               column=info:age, timestamp=1423057248381, value=25                                                                
 xiaofeng                               column=info:birthday, timestamp=1423056496348, value=1987-4-17                                                    
 xiaofeng                               column=info:company, timestamp=1423056496447, value=alibaba                                                       
 xiaofeng                               column=info:favorite, timestamp=1423056496386, value=movie                                                        
3 row(s) in 0.1090 seconds

hbase(main):010:0> 

9.删除某条记录中某个列族的某个列

删除xiaofeng年龄这一列。

hbase(main):010:0> delete 'member','xiaofeng','info:age'
0 row(s) in 0.0060 seconds

hbase(main):011:0> get 'member','xiaofeng','info:age'
COLUMN                                  CELL                                                                                                              
0 row(s) in 0.0330 seconds

hbase(main):012:0> get 'member','xiaofeng','info'
COLUMN                                  CELL                                                                                                              
 info:birthday                          timestamp=1423056496348, value=1987-4-17                                                                          
 info:company                           timestamp=1423056496447, value=alibaba                                                                            
 info:favorite                          timestamp=1423056496386, value=movie                                                                              
3 row(s) in 0.0470 seconds

hbase(main):013:0> 


10.删除整行

hbase(main):013:0> deleteall 'member','scutshuxue'
0 row(s) in 0.0080 seconds

hbase(main):014:0> scan 'member'
ROW                                     COLUMN+CELL                                                                                                       
 lavimer                                column=address:city, timestamp=1423056054519, value=ganzhou                                                       
 lavimer                                column=info:name, timestamp=1423056214559, value=liaozhongmin                                                     
 xiaofeng                               column=address:city, timestamp=1423056496524, value=jieyang                                                       
 xiaofeng                               column=address:contry, timestamp=1423056496475, value=china                                                       
 xiaofeng                               column=address:province, timestamp=1423056496496, value=guangdong                                                 
 xiaofeng                               column=address:town, timestamp=1423056499535, value=xianqiao                                                      
 xiaofeng                               column=info:birthday, timestamp=1423056496348, value=1987-4-17                                                    
 xiaofeng                               column=info:company, timestamp=1423056496447, value=alibaba                                                       
 xiaofeng                               column=info:favorite, timestamp=1423056496386, value=movie                                                        
2 row(s) in 0.0430 seconds

hbase(main):015:0> 

11.查询表中有多少行

hbase(main):015:0> count 'member'
2 row(s) in 0.2170 seconds

hbase(main):016:0> 

12.给某条记录的某个列族增加一列并使用counter实现递增

hbase(main):018:0> incr 'member','xiaofeng','info:age'
COUNTER VALUE = 1

hbase(main):019:0> get 'member','xiaofeng','info:age'
COLUMN                                  CELL                                                                                                              
 info:age                               timestamp=1423058331353, value=\x00\x00\x00\x00\x00\x00\x00\x01                                                   
1 row(s) in 0.0100 seconds

hbase(main):021:0> incr 'member','xiaofeng','info:age'
COUNTER VALUE = 2

hbase(main):022:0> 

13.获取当前count的值

hbase(main):022:0> get_counter 'member','xiaofeng','info:age'
COUNTER VALUE = 2

hbase(main):023:0> 


14.清空整张表

hbase(main):026:0> truncate 'member'
Truncating 'member' table (it may take a while):
 - Disabling table...
 - Dropping table...
 - Creating table...
0 row(s) in 5.5960 seconds

hbase(main):027:0> scan 'member'
ROW                                     COLUMN+CELL                                                                                                       
0 row(s) in 0.0150 seconds

hbase(main):028:0> 

你可能感兴趣的:(HBase)