【Hbase学习系列】hbase shell常用命令及使用示例

该系列博文为笔者学习使用hbase时笔记,能使快速入门hbase的一些使用。如发现问题,欢迎批评指正。

章节博文
1【Hbase学习系列】hbase shell常用命令及使用示例
2【Hbase学习系列】Hbase常用过滤器使用详解
3【Hbase学习系列】Hbase加载数据 / 写数据入Hbase

【Hbase学习系列】hbase shell常用命令及使用示例

1. hbase shell常用命令

序号命令描述
(1)$ ./bin/hbase shell运行hbase shell
(2)help
help list
获取(具体命令的)帮助 / 使用说明
(3)status查看hbase状态
(4)version查看hbase版本
(5)whoami查看当前用户
(6)list列出Hbase中存在的所有表
(7)list ‘表名’查看指定表,确认表是否存在(注意:表名必须加单引号)
(8)create ‘表名’,‘列簇1’(,列簇2,……)创建表,必须指定列簇名(一个或多个均可)
(9)exists ‘表名’测试表是否存在
(10)desc ‘表名’
describe ‘表名’
查看表结构和配置
(11)put ‘表名’ ,‘行键1’,‘列簇:列’,‘值’向表中插入数据
(12)scan '表名’
scan ‘表名’,COLUMN=>'列簇’
scan ‘表名’,COLUMNS=>‘列簇:列’
列出hbase表所有数据
列出指定列簇/列的数据
(13)get ‘表名’,‘行键’
get ‘表名’,'行键,‘列簇’
get ‘表名’,‘行键’,‘列簇:列’
取出表中指定行 / 指定行的列簇 / 指定行的cell数据
(14)deleteall ‘表名’,‘行键’
delete ‘表名’ ‘行键’,'列簇’
delete ‘表名’ ‘行键’,‘列簇:列’
删除指定行 / 指定行的指定列簇/ cell(即指定行,列簇,列)的数据
(15)alter ‘表名’,‘新列簇名’
alter ‘表名’,NAME=>‘新增列簇名’
新增一个列簇(会丢失该列簇下所有数据)
(16)alter ‘表名’,‘delete’=>列簇名
alter ‘表名’,NAME=>‘列簇名’,METHOD=>‘delete’
删除一个列簇
(17)count ‘表名’统计表中数据行数
(18)truncate ‘表名’清空表
(19)disable ‘表名’
enable ‘表名’
使表失效/生效
(20)is_disabled ‘表名’
is_enabled ‘表名’
检查表是否失效/生效
(21)drop ‘表名’删除表(drop表之前需要先disable)
(22)exit退出hbase shell
(23)@shell.hbase.configuration.get("****")获取hbase配置

2. 使用示例

(1) 运行hbase shell

[user1@test /hbase/bin]$ .hbase shell

HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell
Version 2.2.5, rf76a601273e834267b55c0cda12474590283fd4c, 2020年 05月 21日 星期四 18:34:40 CST
Took 0.0026 seconds
hbase(main):001:0>

(2) 获取帮助

hbase(main):001:0> help

hbase(main):001:0> help 'list'
List all user tables in hbase. Optional regular expression parameter could
be used to filter the output. Examples:

  hbase> list
  hbase> list 'abc.*'
  hbase> list 'ns:abc.*'
  hbase> list 'ns:.*'

(3) 查看hbase状态

hbase(main):073:0> status
1 active master, 0 backup masters, 1 servers, 0 dead, 6.0000 average load
Took 0.0158 seconds

(4) 查看hbase版本

hbase(main):075:0> version
2.2.5, rf76a601273e834267b55c0cda12474590283fd4c, 2020年 05月 21日 星期四 18:34:40 CST
Took 0.0003 seconds

(5) 查看当前用户

hbase(main):081:0> whoami
user1 (auth:SIMPLE)
    groups: user1
Took 0.0177 seconds

(6) 列出hbase中存在的所有表

hbase(main):001:0> list
TABLE                                                                                                    
test1                                                                                                    
test_hbase                                                                                               
2 row(s)
Took 0.4269 seconds                                                                                      
=> ["test1", "test_hbase"]

(7) 查看指定表

hbase(main):002:0> list 'test1'
TABLE                                                                                                    
test1                                                                                                    
1 row(s)
Took 0.0033 seconds                                                                                      
=> ["test1"]

(8) 创建表

hbase(main):012:0> create 'test2','f1','f2'
Created table test2
Took 0.7636 seconds                                                                                      
=> Hbase::Table - test2

(9) 测试表是否存在

hbase(main):015:0> exists 'test2'
Table test2 does exist                                                                                   
Took 0.0072 seconds                                                                                      
=> true

(10) 查看表结构和配置

hbase(main):003:0> desc 'test1'
Table test1 is ENABLED                                                                                   
test1                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                              
{NAME => 'f1', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_D
ELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER',
 MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN
_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 
'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                      

1 row(s)

QUOTAS                                                                                                   
0 row(s)
Took 0.2032 seconds

(11) 向表中插入数据

hbase(main):019:0> put 'test2','row1','f2:num','123'
Took 0.0022 seconds     
                                                                                 
hbase(main):020:0> scan 'test2'
ROW                         COLUMN+CELL                                                                                
 row1                    column=f2:num, timestamp=1601199831269, value=123                            
2 row(s)
Took 0.0038 seconds 

(12) 查看表中所有数据

hbase(main):004:0> scan 'test1'
ROW                         COLUMN+CELL                                                                  
 http://www.test5           column=f1:host, timestamp=1601191913849, value=894spark                      
 http://www.test5           column=f1:ip, timestamp=1601191913849, value=jse8dis                         
 http://www.test6           column=f1:host, timestamp=1601191913553, value=8932spark                     
 http://www.test6           column=f1:ip, timestamp=1601191913553, value=ri3test                         
2 row(s)
Took 0.0334 seconds

hbase(main):005:0> scan 'test1',COLUMN=>'f1'
ROW                                COLUMN+CELL                                                                                       
 http://www.test7                  column=f1:host, timestamp=1601259745311, value=894spark                                           
 http://www.test7                  column=f1:ip, timestamp=1601259745311, value=1239843                                              
 http://www.test8                  column=f1:host, timestamp=1601259745552, value=8932spark                                          
 http://www.test8                  column=f1:ip, timestamp=1601259745552, value=4568390                                              
2 row(s)
Took 0.0084 seconds
                                                                                                                  
hbase(main):006:0> scan 'test1',COLUMNS=>'f1:host'
ROW                                COLUMN+CELL                                                                                       
 http://www.test7                  column=f1:host, timestamp=1601259745311, value=894spark                                           
 http://www.test8                  column=f1:host, timestamp=1601259745552, value=8932spark                                          
2 row(s)
Took 0.0046 seconds

(13) 取出表中指定一行 / 指定行的列簇 / 指定cell 的数据

hbase(main):011:0> get 'test1','http://www.test5'
COLUMN                      CELL                                                                         
 f1:host                    timestamp=1601191913849, value=894spark                                      
 f1:ip                      timestamp=1601191913849, value=jse8dis                                       
1 row(s)
Took 0.0085 seconds 

hbase(main):007:0> get 'test1','http://www.test5','f1'
COLUMN                      CELL                                                                         
 f1:host                    timestamp=1601191913849, value=894spark                                      
 f1:ip                      timestamp=1601191913849, value=jse8dis                                         
1 row(s)
Took 0.0311 seconds

hbase(main):016:0> get 'test1','http://www.test5','f1:host'
COLUMN                      CELL                                                                         
 f1:host                    timestamp=1601191913849, value=894spark                                      
1 row(s)
Took 0.0122 seconds

(14) 删除数据

hbase(main):021:0> deleteall 'test2','f1'
Took 0.0045 seconds

hbase(main):009:0> delete 'test2','row1','f1'
Took 0.0300 seconds   
                                                                                                               
hbase(main):010:0> delete 'test2','row1','f1:test'
Took 0.0025 seconds

(15) 新增列簇

① alter ‘表名’,‘新列簇名’(当列簇f2不存在时,写入数据报错,后新增列簇成功,则可正常写入数据)

hbase(main):054:0> put 'test3','row1','f2:test','456'

ERROR: org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException: Column family f2 does not exist in region test3,,1601263806214.54872290dadf3004aa74b6129f9854e1. in table 'test3', {NAME => 'f1', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}
        at org.apache.hadoop.hbase.regionserver.HRegion.doBatchMutate(HRegion.java:4352)
        at org.apache.hadoop.hbase.regionserver.HRegion.put(HRegion.java:3135)
        at org.apache.hadoop.hbase.regionserver.RSRpcServices.mutate(RSRpcServices.java:2910)
        at org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos$ClientService$2.callBlockingMethod(ClientProtos.java:42276)
        at org.apache.hadoop.hbase.ipc.RpcServer.call(RpcServer.java:418)
        at org.apache.hadoop.hbase.ipc.CallRunner.run(CallRunner.java:133)
        at org.apache.hadoop.hbase.ipc.RpcExecutor$Handler.run(RpcExecutor.java:338)
        at org.apache.hadoop.hbase.ipc.RpcExecutor$Handler.run(RpcExecutor.java:318)

For usage try 'help "put"'

Took 0.0057 seconds                                                                                      
hbase(main):055:0> alter 'test3','f2'
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 1.7982 seconds                                                                                      
hbase(main):056:0> put 'test3','row1','f2:test','456'
Took 0.0022 seconds                                                                                      
hbase(main):057:0> scan 'test3'
ROW                         COLUMN+CELL                                                                  
 row1                       column=f2:test, timestamp=1601263900993, value=456                           
1 row(s)
Took 0.0025 seconds

② alter ‘表名’,NAME=>‘新增列簇名’

hbase(main):062:0> alter 'test3',NAME=>'f2'
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 1.8060 seconds

(16) 删除列簇

① alter ‘表名’,‘delete’=>列簇名

hbase(main):060:0> alter 'test3','delete'=>'f2'
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 1.7949 seconds                                                                                      
hbase(main):061:0> scan 'test3'
ROW                         COLUMN+CELL                                                                  
0 row(s)
Took 0.0019 seconds

② alter ‘表名’,NAME=>‘列簇名’,METHOD=>‘delete’

hbase(main):067:0> alter 'test3',NAME=>'f2',METHOD=>'delete'
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 1.8127 seconds

(17) 统计表中数据行数

hbase(main):024:0> count 'test2'
2 row(s)
Took 0.0167 seconds                                                                                      
=> 2

(18) 清空表内所有数据

hbase(main):014:0> truncate 'test2'
Truncating 'test2' table (it may take a while):
Disabling table...
Truncating table...
Took 1.1366 seconds

(19) 使表失效/生效

hbase(main):021:0> disable 'test2'
Took 0.4202 seconds                                                                                      
hbase(main):022:0> scan 'test2'
ROW                         COLUMN+CELL                                                                  
org.apache.hadoop.hbase.TableNotEnabledException: test2 is disabled.
        at org.apache.hadoop.hbase.client.ConnectionImplementation.relocateRegion(ConnectionImplementation.java:761)
        at org.apache.hadoop.hbase.client.RpcRetryingCallerWithReadReplicas.getRegionLocations(RpcRetryingCallerWithReadReplicas.java:328)
        at org.apache.hadoop.hbase.client.ScannerCallable.prepare(ScannerCallable.java:139)
        at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas$RetryingRPC.prepare(ScannerCallableWithReplicas.java:408)
        at org.apache.hadoop.hbase.client.RpcRetryingCallerImpl.callWithRetries(RpcRetryingCallerImpl.java:105)
        at org.apache.hadoop.hbase.client.ResultBoundedCompletionService$QueueingFuture.run(ResultBoundedCompletionService.java:80)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

ERROR: Table test2 is disabled!

For usage try 'help "scan"'

Took 0.1071 seconds  
                                                                                    
hbase(main):023:0> enable 'test2'
Took 0.7314 seconds                                                                                      

(20) 检查表是否失效/生效

hbase(main):017:0> is_disabled 'test2'
false                                                                                                                                
Took 0.0464 seconds                                                                                                                  
=> 1

hbase(main):018:0> is_enabled 'test2'
true                                                                                                                                 
Took 0.0045 seconds                                                                                                                  
=> true

(21) 删除表(表需要先disable,再drop)

hbase(main):001:0> drop 'test2'

ERROR: Table test2 is enabled. Disable it first.

For usage try 'help "drop"'

Took 0.5084 seconds 
                                                                                     
hbase(main):002:0> disable 'test2'
Took 0.4476 seconds 
                                                                                     
hbase(main):003:0> drop 'test2'
Took 0.2306 seconds 

(22) 退出hbase shell

hbase(main):004:0> exit

(23) 获取hbase配置

hbase(main):001:0> @shell.hbase.configuration.get("hbase.zookeeper.quorum")
=> "localhost"

hbase(main):004:0> @shell.hbase.configuration.get("hbase.zookeeper.property.clientPort")
=> "2181"

附:Hbase官方文档

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值