第7课 Hbase 使用教程

本文详细介绍HBase Shell的基本操作及常用命令,包括创建表、插入数据、查询数据等,并解释了HBase中的列簇概念及其重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:

  • 本文基于Centos 6.x + CDH 5.x 
  • 本例中 Hbase 是安装成集群模式的
本文通过建立student表等相关操作,简单介绍一下hbase的shell操作

建立student 表

使用 hbase shell命令进入hbase的命令行
[plain]  view plain  copy
  1. [root@localhost conf]# hbase shell  
  2. 2014-08-22 16:10:47,662 INFO  [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available  
  3. HBase Shell; enter 'help<RETURN>' for list of supported commands.  
  4. Type "exit<RETURN>" to leave the HBase Shell  
  5. Version 0.96.1.1-cdh5.0.1, rUnknown, Tue May  6 13:27:24 PDT 2014  

然后用 create建立一个表,我们建立的表有如下属性
表名: student
列簇:sid, name, age

什么是列簇?

Hbase是面向列存储的数据库。Hbase中数据列是由列簇来组织的。一个列簇相当于你在mysql中这个表的多个列定义的总和。但是特别的是,一个表可以对多个列簇。具体列簇里面有哪些列是开始时不用指定的。暂时只需要知道这么多,等做了以后慢慢去理解消化,我们学习的时候一定要掌握方法,先做再想为什么这么做,是最高效的学习方式。

为什么要有列簇?

在同一个列簇中的列是存放在一个实例上的。所以对于列簇的理解我的猜测是这样的,刚开始可能没有列簇。虽然nosql是不用定义列的,但是由于我们的hadoop是分布式的,肯定会有一些列在这台机子上,有一些列在那些机子上,为了性能问题,需要弄出一个算法来把一些经常在一起使用的列放到一台机子上,最简单的算法就是由用户自己去定,这就产生了列簇,也就是列的集合,在同一个列簇中的列都在一个机子上。

说完了概念,我们来建立一下这个表
[plain]  view plain  copy
  1. hbase(main):001:0> create 'student', 'info'  
  2. 0 row(s) in 4.3300 seconds  
  3.   
  4. => Hbase::Table - student  

增加数据

使用put增加一行,这里说的一行意思是:一个表的一个列簇中的一个行,在mysql中就相当于只增加 一行中的一列
[plain]  view plain  copy
  1. hbase(main):002:0> put 'student','row1','info:name','jack'  
  2. 0 row(s) in 0.1990 seconds  
意思是往 student 的 name 列中插入一个值 jack
我们查一下这条数据
[plain]  view plain  copy
  1. hbase(main):003:0> get 'student','row1','info:name'  
  2. COLUMN                        CELL                                                                                 
  3.  info:name                    timestamp=1408697225683, value=jack                                                  
  4. 1 row(s) in 0.0490 seconds  
查出来了。
怎么样?是不是感觉这么费劲才插入了一个行的一个列?这是以为hbase是基于google的工程师 Fay Chang (应该是个华裔) 的关于bigtable的论坛写的,而bigtable就是拥有超大列数的表格,大到什么程度?大到一台电脑放不下了,必须用多台电脑分布式的存放,才能放的下,所以数据的操作都是以一行一列为最小单位的。
这个row1 是rowkey

rowkey

行以rowkey作为唯一标示。Rowkey是一段字节数组,这意味着,任何东西都可以保存进去,例如字符串、或者数字。行是按字典的排序由低到高存储在表中


我们继续插入这行别的列
[plain]  view plain  copy
  1. hbase(main):004:0> put 'student','row1','info:sid','1'  
  2. 0 row(s) in 0.0200 seconds  
  3.   
  4. hbase(main):005:0> put 'student','row1','info:age','22'  
  5. 0 row(s) in 0.0210 seconds  
然后我们用scan命令查询一下整个表
[plain]  view plain  copy
  1. hbase(main):006:0> scan 'student'  
  2. ROW                           COLUMN+CELL                                                                          
  3.  row1                         column=info:age, timestamp=1408697651322, value=22                                   
  4.  row1                         column=info:name, timestamp=1408697225683, value=jack                                
  5.  row1                         column=info:sid, timestamp=1408697640490, value=1                                    
  6. 1 row(s) in 0.0580 seconds  

可以看到有三条记录,但是都是一个row里面的,这个row才是相当于mysql的一行
继续插入别的记录,最终结果是这样
[plain]  view plain  copy
  1. hbase(main):005:0> scan 'student'  
  2. ROW                              COLUMN+CELL                                                                                    
  3.  row1                            column=info:age, timestamp=1420817226790, value=22                                             
  4.  row1                            column=info:name, timestamp=1420817205836, value=jack                                          
  5.  row1                            column=info:sid, timestamp=1420817219869, value=1                                              
  6.  row2                            column=info:age, timestamp=1420817278346, value=28                                             
  7.  row2                            column=info:name, timestamp=1420817252182, value=terry                                         
  8.  row2                            column=info:sid, timestamp=1420817267780, value=2                                              
  9.  row3                            column=info:age, timestamp=1420817315351, value=18                                             
  10.  row3                            column=info:name, timestamp=1420817294342, value=billy                                         
  11.  row3                            column=info:sid, timestamp=1420817304621, value=3                                              
  12.  row4                            column=info:name, timestamp=1420858768667, value=karry                                         
  13.  row4                            column=info:sid, timestamp=1420858794556, value=4                                              
  14. 4 row(s) in 1.0990 seconds  

命令介绍

有了基础数据我们就可以通过一边操作一边学习hbase的命令了,比如上个例子我们学习到了一个新的命令  scan

scan 查询数据表

scan命令如果不带任何参数相当于sql中的 select * from table

Limit 查询后显示的条数

用limit可以限制查询的条数
[plain]  view plain  copy
  1. scan 'student',{'LIMIT'=>2}  

效果如下
[plain]  view plain  copy
  1. hbase(main):006:0> scan 'student',{'LIMIT'=>2}  
  2. ROW                              COLUMN+CELL                                                                                    
  3.  row1                            column=info:age, timestamp=1420817226790, value=22                                             
  4.  row1                            column=info:name, timestamp=1420817205836, value=jack                                          
  5.  row1                            column=info:sid, timestamp=1420817219869, value=1                                              
  6.  row2                            column=info:age, timestamp=1420817278346, value=28                                             
  7.  row2                            column=info:name, timestamp=1420817252182, value=terry                                         
  8.  row2                            column=info:sid, timestamp=1420817267780, value=2                                              
  9. 2 row(s) in 0.8250 seconds  

STARTROW 起点rowkey

用startrow可以定义查询返回结果的起点rowkey,相当于大于等于,比如

[plain]  view plain  copy
  1. hbase(main):007:0> scan 'student',{'STARTROW'=>'row2'}  
  2. ROW                              COLUMN+CELL                                                                                    
  3.  row2                            column=info:age, timestamp=1420817278346, value=28                                             
  4.  row2                            column=info:name, timestamp=1420817252182, value=terry                                         
  5.  row2                            column=info:sid, timestamp=1420817267780, value=2                                              
  6.  row3                            column=info:age, timestamp=1420817315351, value=18                                             
  7.  row3                            column=info:name, timestamp=1420817294342, value=billy                                         
  8.  row3                            column=info:sid, timestamp=1420817304621, value=3                                              
  9.  row4                            column=info:name, timestamp=1420858768667, value=karry                                         
  10.  row4                            column=info:sid, timestamp=1420858794556, value=4  

STARTROW 可以使用通配符,比如
[plain]  view plain  copy
  1. hbase(main):008:0> scan 'student',{'STARTROW'=>'row*'}  
  2. ROW                              COLUMN+CELL                                                                                    
  3.  row1                            column=info:age, timestamp=1420817226790, value=22                                             
  4.  row1                            column=info:name, timestamp=1420817205836, value=jack                                          
  5.  row1                            column=info:sid, timestamp=1420817219869, value=1                                              
  6.  row2                            column=info:age, timestamp=1420817278346, value=28                                             
  7.  row2                            column=info:name, timestamp=1420817252182, value=terry                                         
  8.  row2                            column=info:sid, timestamp=1420817267780, value=2                                              
  9.  row3                            column=info:age, timestamp=1420817315351, value=18                                             
  10.  row3                            column=info:name, timestamp=1420817294342, value=billy                                         
  11.  row3                            column=info:sid, timestamp=1420817304621, value=3                                              
  12.  row4                            column=info:name, timestamp=1420858768667, value=karry                                         
  13.  row4                            column=info:sid, timestamp=1420858794556, value=4                                              
  14. 4 row(s) in 0.2830 seconds  

多个参数可以同时使用,比如我要查询startrow = row2 并且只返回一条
[plain]  view plain  copy
  1. hbase(main):009:0> scan 'student',{'STARTROW'=>'row2','LIMIT'=>1}  
  2. ROW                              COLUMN+CELL                                                                                    
  3.  row2                            column=info:age, timestamp=1420817278346, value=28                                             
  4.  row2                            column=info:name, timestamp=1420817252182, value=terry                                         
  5.  row2                            column=info:sid, timestamp=1420817267780, value=2                                              
  6. 1 row(s) in 0.1890 seconds  

STOPROW 定义查询的结束rowkey

跟startrow类似,同上

COLUMNS 控制返回的字段列表

就相当于sql中的 select xx,xxx,xxx  from 这里面的列定义。比如我只需要查询所有学生的名字和年龄,不需要sid信息
[plain]  view plain  copy
  1. hbase(main):011:0> scan 'student',{'COLUMNS'=>['info:name','info:age'],LIMIT=>3}  
  2. ROW                              COLUMN+CELL                                                                                    
  3.  row1                            column=info:age, timestamp=1420817226790, value=22                                             
  4.  row1                            column=info:name, timestamp=1420817205836, value=jack                                          
  5.  row2                            column=info:age, timestamp=1420817278346, value=28                                             
  6.  row2                            column=info:name, timestamp=1420817252182, value=terry                                         
  7.  row3                            column=info:age, timestamp=1420817315351, value=18                                             
  8.  row3                            column=info:name, timestamp=1420817294342, value=billy                                         
  9. 3 row(s) in 0.4470 seconds  

注意写列名的时候要记得带上列簇!比如 info:name

TIMESTAMP 使用时间来精确定位数据

timestamp可以精确的指定某一条记录
[plain]  view plain  copy
  1. hbase(main):012:0> scan 'student',{'TIMESTAMP'=>1420817315351}  
  2. ROW                              COLUMN+CELL                                                                                    
  3.  row3                            column=info:age, timestamp=1420817315351, value=18                                             
  4. 1 row(s) in 0.1920 seconds  

get 获取一行数据

用get可以只获取一行数据
[plain]  view plain  copy
  1. hbase(main):073:0> get 'student','row1'  
  2. COLUMN                           CELL                                                                                           
  3.  info:age                        timestamp=1420817226790, value=22                                                              
  4.  info:name                       timestamp=1420817205836, value=jack                                                            
  5.  info:sid                        timestamp=1420817219869, value=1                                                               
  6. 3 row(s) in 0.1730 seconds  

可以跟上更复杂的参数
[plain]  view plain  copy
  1. hbase(main):076:0> get 'student','row1',{COLUMN=>['info:name','info:sid']}  
  2. COLUMN                           CELL                                                                                           
  3.  info:name                       timestamp=1420817205836, value=jack                                                            
  4.  info:sid                        timestamp=1420817219869, value=1                                                               
  5. 2 row(s) in 0.0490 seconds  
  6.   
  7. hbase(main):077:0> get 'student','row1',{COLUMN=>['info:name','info:sid'],TIMESTAMP=>1420817219869,VERSION=>1}  
  8. COLUMN                           CELL                                                                                           
  9.  info:sid                        timestamp=1420817219869, value=1                                                               
  10. 1 row(s) in 0.0740 seconds  


describe 查看表信息

describe 可以查看表的信息,这个命令会常常用到
[plain]  view plain  copy
  1. hbase(main):013:0> describe 'student'  
  2. DESCRIPTION                                                                       ENABLED                                       
  3.  'student', {NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', true                                          
  4.   REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS                                                
  5.  => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', I                                               
  6.  N_MEMORY => 'false', BLOCKCACHE => 'true'}                                                                                     
  7. 1 row(s) in 7.6720 seconds  

alter 修改表的列簇

用alter可以修改表的列簇,hbase的一个表其实全部信息就是列簇的信息了,比如我们可以增加一个列簇f2
[plain]  view plain  copy
  1. alter 'student', {NAME => 'f2', VERSION => 2}  

这个VERSION官方说是每个字段可以有2个版本,就是一个行的一个列元素可以存成两个值,拥有不同的version
添加完再看下表结构
[plain]  view plain  copy
  1. hbase(main):057:0> describe 'student'  
  2. DESCRIPTION                                                                       ENABLED                                       
  3.  'student', {NAME => 'f2', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', R true                                          
  4.  EPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS => '1', TTL => 'FOREVER                                               
  5.  ', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_                                               
  6.  MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'info', DATA_BLOCK_ENCODING =                                               
  7.  > 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPR                                               
  8.  ESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => '                                               
  9.  false', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}                                                      
  10. 1 row(s) in 0.6180 seconds  

可以看到有两个列簇,一个是f2,一个是info


用 TTL 控制表的数据自动过期

不过我这边用一个比较实用的例子来教大家操作alter:在实际生产环境上经常需要给表增加过期时间,方便表自动清理早期的数据,防止数据过多,毕竟能用hadoop的环境数据量那都是“海量” 
现在我把f2这个列簇的TTL修改为20秒
[plain]  view plain  copy
  1. alter 'student', {NAME => 'f2', TTL => 20}  

然后再看下表信息
[plain]  view plain  copy
  1. hbase(main):061:0> describe 'student'  
  2. DESCRIPTION                                                                       ENABLED                                       
  3.  'student', {NAME => 'f2', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', R true                                          
  4.  EPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS => '1', TTL => '20 SECO                                               
  5.  NDS', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536',                                                
  6.  IN_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'info', DATA_BLOCK_ENCODIN                                               
  7.  G => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', CO                                               
  8.  MPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS =                                               
  9.  > 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}                                                   
  10. 1 row(s) in 0.1540 seconds  

可以看到f2的 TTL 被设置为20 seconds。
然后我们测试一下添加一个记录到f2去,然后等20秒再去看下
[plain]  view plain  copy
  1. hbase(main):065:0> put 'student','row3','f2:grade','2'  
  2. 0 row(s) in 0.0650 seconds  
  3.   
  4. hbase(main):066:0> scan 'student',{STARTROW=>'row3',LIMIT=>1}  
  5. ROW                              COLUMN+CELL                                                                                    
  6.  row3                            column=f2:grade, timestamp=1420872179176, value=2                                              
  7.  row3                            column=info:age, timestamp=1420817315351, value=18                                             
  8.  row3                            column=info:name, timestamp=1420817294342, value=billy                                         
  9.  row3                            column=info:sid, timestamp=1420817304621, value=3                                              
  10. 1 row(s) in 0.0630 seconds  
  11.   
  12. hbase(main):067:0> scan 'student',{STARTROW=>'row3',LIMIT=>1}  
  13. ROW                              COLUMN+CELL                                                                                    
  14.  row3                            column=info:age, timestamp=1420817315351, value=18                                             
  15.  row3                            column=info:name, timestamp=1420817294342, value=billy                                         
  16.  row3                            column=info:sid, timestamp=1420817304621, value=3                                              
  17. 1 row(s) in 0.1370 seconds  

会看到刚添加进去的时候row2还有 f2:grade的数据,但是过了一会儿去看就没了

使用alter删除列簇

使用alter删除列簇的操作是带上一个METHOD参数,并写值为 delete
[plain]  view plain  copy
  1. hbase(main):068:0> alter 'student', {NAME => 'f2', METHOD=>'delete'}  
  2. Updating all regions with the new schema...  
  3. 0/1 regions updated.  
  4. 1/1 regions updated.  
  5. Done.  
  6. 0 row(s) in 3.9750 seconds  
  7.   
  8. hbase(main):069:0> describe 'student'  
  9. DESCRIPTION                                                                       ENABLED                                       
  10.  'student', {NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', true                                          
  11.   REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS                                                
  12.  => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', I                                               
  13.  N_MEMORY => 'false', BLOCKCACHE => 'true'}                                                                                     
  14. 1 row(s) in 0.2210 seconds  

count 统计表中的数据

跟传统的关系型数据库不一样,这个命令可能会执行很久
[plain]  view plain  copy
  1. hbase(main):082:0> count 'student'  
  2. 4 row(s) in 0.6410 seconds  
  3.   
  4. => 4  

这个命令还有一个很奇怪的功能,就是在统计的时候可以每隔X行显示一下数据的rowkey,可能是方便统计的时候看下统计到哪里了,比如我分别用间隔2行跟间隔1行做了实验
[plain]  view plain  copy
  1. hbase(main):083:0> count 'student',2  
  2. Current count: 2, row: row2                                                                                                     
  3. Current count: 4, row: row4                                                                                                     
  4. 4 row(s) in 0.0480 seconds  
  5.   
  6. => 4  
  7. hbase(main):084:0> count 'student',1  
  8. Current count: 1, row: row1                                                                                                     
  9. Current count: 2, row: row2                                                                                                     
  10. Current count: 3, row: row3                                                                                                     
  11. Current count: 4, row: row4                                                                                                     
  12. 4 row(s) in 0.0650 seconds  
  13.   
  14. => 4  



list 查看数据库中的所有表

用list可以列出当前hbase中的所有表
[plain]  view plain  copy
  1. hbase(main):079:0> list  
  2. TABLE                                                                                                                           
  3. employee                                                                                                                        
  4. employee2                                                                                                                       
  5. student                                                                                                                         
  6. 3 row(s) in 0.2020 seconds  
  7.   
  8. => ["employee", "employee2", "student"]  

status 命令

查询服务状态
[plain]  view plain  copy
  1. hbase(main):013:0> status  
  2. 1 servers, 0 dead, 3.0000 average load  
[plain]  view plain  copy
  1. hbase(main):070:0> list  
  2. TABLE                                                                                                                           
  3. employee                                                                                                                        
  4. employee2                                                                                                                       
  5. student                                                                                                                         
  6. 3 row(s) in 0.6380 seconds  
  7.   
  8. => ["employee", "employee2", "student"]  


version 

查询版本号

whoami

看连接用户
[javascript]  view plain  copy
  1. hbase(main):014:0> whoami  
  2. root (auth:SIMPLE)  

truncate 快速清除数据

跟一般数据库中的truncate不太一样,如果你执行 truncate,hbase就是帮你把表停掉,删掉再重建一次,只是这个动作不用你手动做了而已
[plain]  view plain  copy
  1. hbase(main):086:0> truncate 'student'  
  2. Truncating 'student' table (it may take a while):  
  3.  - Disabling table...  
  4.  - Dropping table...  
  5.  - Creating table...  
  6. 0 row(s) in 4.6330 seconds  

主要的命令就介绍到这里,更详细的命令参考 http://wiki.apache.org/hadoop/Hbase/Shell

参考资料

  • http://wiki.apache.org/hadoop/Hbase/Shell
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值