HBase Shell操作命令

一、general操作

1.1 获取帮助

#引号不能省略
help 'status'

1.2查询服务器状态


 1. status
hbase(main):002:0> status
1 active master, 0 backup masters, 3 servers, 0 dead, 1.0000 average load

1.3查询HBase版本

version
hbase(main):003:0> version
1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr  6 19:36:54 PDT 2017

二、namespace操作

2.1命名空间

HBase命名空间:它是系统级别的命名空间,通常包含HBase自身的元数据和系统表。这些系统表存储了HBase集群的结构和配置信息,对于HBase的运行和管理至关重要。例如,HBase中的hbase:meta表就位于这个命名空间中,它保存了所有用户表的元数据,包括表名、列族信息以及每个表的RegionServer位置等。
default命名空间:这是为用户创建的默认命名空间。当用户在HBase中创建表而没有指定命名空间时,表会被创建在default命名空间中。这个命名空间是为用户自定义表设计的,用户可以在这个命名空间中创建、管理和查询自己的数据表。
NAMESPACE 是一个用于组织和管理表的高级特性。

list_namespace
hbase(main):001:0> list_namespace
NAMESPACE                                                                    
default                                                                      
hbase                                                                        
2 row(s) in 0.1380 seconds

2.2查看命名空间下的表

list_namespace_tables'hbase'
hbase(main):003:0> list_namespace_tables'hbase'
TABLE                                                                        
meta                                                                         
namespace                                                                    
2 row(s) in 0.0190 seconds

2.3创建命名空间

命令格式:create ‘空间名’

create_namespace 'my_ns1'
hbase(main):005:0> create_namespace 'my_ns1'
0 row(s) in 0.8950 seconds

2.4查看命名空间

describe_namespace 'my_ns1'
hbase(main):007:0> describe_namespace 'my_ns1'
DESCRIPTION                                                                  
{NAME => 'my_ns1'}                                                           
1 row(s) in 0.0140 seconds

2.5删除命名空间

drop_namespace 'my_ns1'
hbase(main):008:0> drop_namespace 'my_ns1'
0 row(s) in 0.8860 seconds

三、DDL操作(表操作)

3.1创建表

命令格式:create ‘表名’,‘列名1’,‘列名2…

create 'scores','student','courses'
hbase(main):004:0> create 'scores','student','courses'
0 row(s) in 1.2310 seconds

=> Hbase::Table - scores

3.2获取表信息

describe ‘scores'
hbase(main):001:0> describe 'scores'
Table scores is ENABLED                                                                                                                                                                    
scores                                                                                                                                                                                     
COLUMN FAMILIES DESCRIPTION                                                                                                                                                                
{NAME => 'courses', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN
_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                                                    
{NAME => 'student', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN
_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}      
exists

3.3增加表的列族

增加department列族

disable 'scores'#关闭
alter 'scores',{NAME => 'department',VERSIONS => 3}
enable 'scores'#启动
hbase(main):002:0> disable 'scores'
0 row(s) in 2.2620 seconds

hbase(main):003:0> alter 'scores',{NAME => 'department',VERSIONS => 3}

Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.8990 seconds
hbase(main):004:0> enable 'scores'

3.4删除表的列族

disable 'scores'#关闭
alter 'scores',{NAME => 'department',METHOD => 'delete'}
enable 'scores'#启动
hbase(main):005:0> disable 'scores'
0 row(s) in 2.2510 seconds

hbase(main):006:0> alter 'scores',{NAME => 'department',METHOD => 'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.1410 seconds

hbase(main):007:0> enable 'scores'
0 row(s) in 1.2380 seconds

3.5查询表是否存在

exists 'scores'

hbase(main):001:0> exists 'scores'
Table scores does exist                                                                                                                                                                    
0 row(s) in 0.1580 seconds

3.6判断表是否enable/disable

is_enabled 'scores'
is_disabled 'scores'
hbase(main):002:0> is_enabled 'scores'
true                                                                                                                                                                                       
0 row(s) in 0.0170 seconds

hbase(main):003:0> is_disabled 'scores'
false                                                                                                                                                                                      
0 row(s) in 0.0170 seconds

3.7删除表

disable 'scores'
drop 'scores'
exists 'scores'
hbase(main):004:0> disable 'scores'
0 row(s) in 2.2870 seconds

hbase(main):005:0> drop 'scores'
0 row(s) in 1.2570 seconds
Table scores does not exist                                                                                                                                                                
0 row(s) in 0.1960 seconds

四、DML操作

4.1插入记录

格式:put ‘表名’, ‘行键’, ‘列族:列名’, ‘值’

put 'scores','1','student:name','xiaoming'
put 'scores','1','courses:python','90'
put 'scores','1','courses:java','90'
put 'scores','2','student:name','xiaokai'
put 'scores','2','courses:python','80'
put 'scores','2','courses:java','80'
put 'scores','3','student:name','xiaohong'
put 'scores','3','courses:python','95'
put 'scores','3','courses:java','95'
hbase(main):001:0> put 'scores','1','student:name','xiaoming'
0 row(s) in 0.2580 seconds

hbase(main):002:0> put 'scores','1','courses:python','90'
0 row(s) in 0.0200 seconds

hbase(main):003:0> put 'scores','1','courses:java','90'
0 row(s) in 0.0140 seconds

hbase(main):004:0> 

4.2读取表数据

get 'scores','1'
get 'scores','1','courses'
get 'scores','1','courses:python'
hbase(main):001:0> get 'scores','1'
COLUMN                                          CELL                                                                                                                                       
 courses:java                                   timestamp=1707682372256, value=90                                                                                                          
 courses:python                                 timestamp=1707682367650, value=90                                                                                                          
 student:name                                   timestamp=1707682361045, value=xiaoming                                                                                                    
1 row(s) in 0.1960 seconds

hbase(main):002:0> get 'scores','1','courses'
COLUMN                                          CELL                                                                                                                                       
 courses:java                                   timestamp=1707682372256, value=90                                                                                                          
 courses:python                                 timestamp=1707682367650, value=90                                                                                                          
1 row(s) in 0.0190 seconds

hbase(main):003:0> get 'scores','1','courses:python'
COLUMN                                          CELL                                                                                                                                       
 courses:python                                 timestamp=1707682367650, value=90                                                                                                          
1 row(s) in 0.0130 seconds

4.3扫描表数据

scan 'scores'
scan 'scores',{LIMIT=>2}
scan 'scores',{STARTROW=>'1',ENDROW=>3}
scan 'scores',{COLUMN=>'courses:python'}
hbase(main):004:0> scan 'scores'
ROW                                             COLUMN+CELL                                                                                                                                
 1                                              column=courses:java, timestamp=1707682372256, value=90                                                                                     
 1                                              column=courses:python, timestamp=1707682367650, value=90                                                                                   
 1                                              column=student:name, timestamp=1707682361045, value=xiaoming                                                                               
 2                                              column=courses:java, timestamp=1707682397313, value=80                                                                                     
 2                                              column=courses:python, timestamp=1707682394587, value=80                                                                                   
 2                                              column=student:name, timestamp=1707682391577, value=xiaokai                                                                                
 3                                              column=courses:java, timestamp=1707682406376, value=95                                                                                     
 3                                              column=courses:python, timestamp=1707682403146, value=95                                                                                   
 3                                              column=student:name, timestamp=1707682400472, value=xiaohong                                                                               
3 row(s) in 0.0440 seconds

4.4统计记录数

count 'scores'
hbase(main):001:0> count 'scores'
3 row(s) in 0.2330 seconds

=> 3

4.5删除列

删除scores表中的列english

delete 'scores','1','courses:python'
delete 'scores','1','courses'
hbase(main):002:0> delete 'scores','1','courses:python'
0 row(s) in 0.0620 seconds

4.6删除所有行

deleteall 'scores','1'
hbase(main):003:0> deleteall 'scores','1'
0 row(s) in 0.0160 seconds
hbase(main):004:0> scan 'scores'
ROW                                             COLUMN+CELL                                                                                                                                
 2                                              column=courses:java, timestamp=1707682397313, value=80                                                                                     
 2                                              column=courses:python, timestamp=1707682394587, value=80                                                                                   
 2                                              column=student:name, timestamp=1707682391577, value=xiaokai                                                                                
 3                                              column=courses:java, timestamp=1707682406376, value=95                                                                                     
 3                                              column=courses:python, timestamp=1707682403146, value=95                                                                                   
 3                                              column=student:name, timestamp=1707682400472, value=xiaohong                                                                               
2 row(s) in 0.0210 seconds

4.7删除表中所有数据

truncate 'scores'
hbase(main):005:0> truncate 'scores'
Truncating 'scores' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.5240 seconds

hbase(main):006:0> scan 'scores'
ROW                                             COLUMN+CELL                                                                                                                                
0 row(s) in 0.1390 seconds

hbase(main):007:0> 

五、授权

				   	**HBase权限与命令对照表**

HBase Shell操作命令_第1张图片

5.1在HBase中启动授权机制需要修改配置文件hbase-site.xml 添加的内容如下


      hbase.security.authorization</name>   
      true</value> 
   </property> 

   hbase.coprocessor.master.classes</name>   
   org.apache.hadoop.hbase.security.access.AccessController</value> 
</property> 

   hbase.coprocessor.region.classes</name>   
   org.apache.hadoop.hbase.security.access.AccessController,     
   org.apache.hadoop.hbase.security.token.TokenProvider</value> 
</property> 

   hbase.superuser</name>   
   hbase,root,administrator</value> 
</property> 

5.2授予用户xiaoxi对命名空间my_ns写权限和撤销权限

hbase(main):004:0> grant 'xiaoxi','W','@my_ns'

hbase(main):005:0> revoke 'xiaoxi','@my_ns'

你可能感兴趣的:(大数据,hbase,数据库,大数据,zookeeper,hadoop)