【大数据学习 | HBASE】hbase shell基础实操

时间:2024-11-10 07:22:44

1. 查看hbase状态

# 进入hbase 命令行
hbase shell
status

我这里没启用高可用hbase

1 active master, 0 backup masters, 2 servers, 1 dead, 1.0000 average load
Took 5.2635 seconds  

2. 查看版本号

version
hbase:002:0> version
2.4.11, r7e672a0da0586e6b7449310815182695bc6ae193, Tue Mar 15 10:31:00 PDT 2022
Took 0.0006 seconds  

3. 命名空间操作

# 创建命名空间
create_namespace '命名空间名'

# 显示所有命名空间
list_namespace

# 删除命名空间
drop_namespace '命名空间名'

# 查看命名空间中的表有什么
list_namespace_tables
hbase:001:0> list_namespace
NAMESPACE                                                                                                      
default                                                                                                        
hbase                                                                                                          
2 row(s)
Took 0.8956 seconds                                                                                            
hbase:002:0> create_namespace 'hainiu'
Took 0.3163 seconds                                                                                            
hbase:003:0> list_namespace
NAMESPACE                                                                                                      
default                                                                                                        
hainiu                                                                                                         
hbase                                                                                                          
3 row(s)
Took 0.0371 seconds                                                                                            
hbase:004:0> drop_namespace 'hainiu'
Took 0.1986 seconds                                                                                            
hbase:005:0> list_namespace
NAMESPACE                                                                                                      
default                                                                                                        
hbase                                                                                                          
2 row(s)
Took 0.0247 seconds         

4. 创建表

# 创建默认命名空间的表
create '表名称', '列族名称1','列族名称2','列族名称N'

# 创建带有命名空间的表
create '命名空间:表名称', '列族名称1','列族名称2','列族名称N'
hbase:007:0> create 'hainiu:info','info1','info2', 'info3'
Created table hainiu:info
Took 2.4064 seconds                                                                                            
=> Hbase::Table - hainiu:info
-- 指定了命名空间,即在该指定的命名空间下创建表,否则就默认在default下创建表。
-- 创建表必须指明列族
-- hainiu是命名空间,info是表,info1,info2,info3是列族

创建完之后有一个region是上线的

状态和该表region的位置:

5. 列出所有的表

# 查看所有的表
list

# 查询指定命名空间下的表
list_namespace_tables '命名空间'
hbase:012:0> list
TABLE                                                                                                          
hainiu:info                                                                                                    
1 row(s)
Took 0.1127 seconds                                                                                            
=> ["hainiu:info"]
hbase:013:0> list_namespace_tables 'hainiu'
TABLE                                                                                                          
info                                                                                                           
1 row(s)
Took 0.0161 seconds                                                                                            
=> ["info"]

只显示用户创建的表信息

6. 获取表的描述

# 默认命名空间
describe '表名'

# 指定命名空间
describe '命名空间:表名'
hbase:001:0> describe 'hainiu:info'
Table hainiu:info is ENABLED                                                                                   
hainiu:info                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                    
{NAME => 'info1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', D
ATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true
', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                             

{NAME => 'info2', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', D
ATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true
', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                             

{NAME => 'info3', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', D
ATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true
', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                             

3 row(s)
Quota is disabled
Took 1.6924 seconds 

7. 删除列族

# 删除hainiu_table 表的 cf3 列族
alter 'hainiu:info',{NAME=>'info',METHOD=>'delete'}
# 查看表结构
describe 'hainiu:info'
hbase:001:0> alter 'hainiu:info',{NAME=>'info3',METHOD=>'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 4.1782 seconds                                                                                            
hbase:002:0> describe 'hainiu:info'
Table hainiu:info is ENABLED                                                                                   
hainiu:info                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                    
{NAME => 'info1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', D
ATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true
', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                             

{NAME => 'info2', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', D
ATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true
', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                             

2 row(s)
Quota is disabled
Took 0.0695 seconds   

8. 其他ddl操作

# 把表设置为disable(下线)
disable '表名'

# drop表
# 先把表下线,再drop表
disable '表名'
# 启动表
enable '表名'
drop '表名'  

# 判断表是否存在
exists '表名'

# 判断表是否下线
is_disabled '表名'

# 判断表是否上线
is_enabled '表名'
hbase:003:0> create 'hainiu:student', 'name', 'age'
Created table hainiu:student
Took 1.3426 seconds                                                                                            
=> Hbase::Table - hainiu:student
hbase:004:0> disable 'hainiu:student'
Took 0.7914 seconds                                                                                            
hbase:005:0> drop 'hainiu:student'
Took 0.7322 seconds                                                                                            
hbase:006:0> list_namespace_tables 'hainiu'
TABLE                                                                                                          
info                                                                                                           
1 row(s)
Took 0.0437 seconds                                                                                            
=> ["info"]
-- 先下线表,才可以删除表
hbase:001:0> exists 'hainiu:info'
Table hainiu:info does exist                                                                                   
Took 1.2314 seconds                                                                                            
=> true
hbase:002:0> is_disabled 'hainiu:info'
false                                                                                                          
Took 0.0261 seconds                                                                                            
=> false