温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Hbase基本语句用法

发布时间:2020-04-14 07:44:02 来源:网络 阅读:1290 作者:天黑顺路 栏目:关系型数据库

使用hbase shell命令进入hbase:

[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# hbase shell
SLF4J: Class path contains multiple SLF4Jbindings.
SLF4J: Found binding in[jar:file:/usr/hdp/2.4.3.0-227/hadoop/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in[jar:file:/usr/hdp/2.4.3.0-227/zookeeper/lib/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Seehttp://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type[org.slf4j.impl.Log4jLoggerFactory]
HBase Shell; enter 'help<RETURN>' forlist of supported commands.
Type "exit<RETURN>" toleave the HBase Shell
Version 1.1.2.2.4.3.0-227, rUnknown, FriSep  9 23:34:30 UTC 2016
 
hbase(main):001:0>

 

输入help 查看帮助信息

hbase(main):001:0> help
HBase Shell, version 1.1.2.2.4.3.0-227,rUnknown, Fri Sep  9 23:34:30 UTC 2016
Type 'help "COMMAND"', (e.g.'help "get"' -- the quotes are necessary) for help on a specificcommand.
Commands are grouped. Type 'help"COMMAND_GROUP"', (e.g. 'help "general"') for help on acommand group.
 
COMMAND GROUPS:
 Group name: general
 Commands: status, table_help, version, whoami
…

 

查看服务器状态:

hbase(main):002:0> status
1 active master, 0 backup masters, 4servers, 0 dead, 20.5000 average load

 

查看版本信息

hbase(main):003:0> version
1.1.2.2.4.3.0-227, rUnknown, Fri Sep  9 23:34:30 UTC 2016

 

查看当前用户:

hbase(main):004:0> whoami
root (auth:SIMPLE)
groups: root

 

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

hbase(main):005:0>  table_help
Help for table-reference commands.
 
You can either create a table via 'create'and then manipulate the table via commands like 'put', 'get', etc.
See the standard help information for howto use each of these commands.
…

 

创建表

常用语法: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"]

 

使用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查询数据:

语法: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 'te            
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI