HBase是一种构建在HDFS之上的分布式、面向列的存储系统。在需要实时读写、随机访问超大规模数据集时,可以使用HBase。
HBase的特点
- 大:一个表可以有上亿行,上百万列。
- 面向列:面向列表(簇)的存储和权限控制,列(簇)独立检索。
- 稀疏:对于为空(NULL)的列,并不占用存储空间,因此,表可以设计的非常稀疏。
- 无模式:每一行都有一个可以排序的主键和任意多的列,列可以根据需要动态增加,同一张表中不同的行可以有截然不同的列。
- 数据多版本:每个单元中的数据可以有多个版本,默认情况下,版本号自动分配,版本号就是单元格插入时的时间戳。
- 数据类型单一:HBase中的数据都是字符串,没有类型。
2.1. 掌握HBase shell命令使用(创建一个学生信息表:rowkey要求是学号,一个列簇,包含姓名、性别、年龄、住址)
1.常用命令
1.1.hbase shell
进入hbase shell界面。

1.2.list
查看已经存在的表。

1.3.status
查询服务器状态。

1.4.version
查询hbase版本。

表操作
2.1.创建表
在shell环境中使用disable命令可以把表设为离线,使用alter命令可以进行必要的修改,而enable命令则可以把表设置为在线。
建表过程由于不熟悉hbase shell建表格式,出现了很多错误,后面在一篇博客上看到了解释后才恍然大悟。

例1:创建一个简单表,只包含一个‘data’列’。表和列族属性默认:
#create ‘test’ ,‘data’

例2:创建一个学生信息表:rowkey要求是学号,一个列簇,包含姓名、性别、年龄、住址:

例3:创建多个列簇:,包含姓名、性别、年龄、住址。表和列族属性默认:
#create 'student',{NAME=>'name'},{NAME=>'sex'},{NAME=>'age'},{NAME=>'adress'}

2.2.删除表
删除表需要先将表置为离线,然后再删除。
例:删除test表
#disable ‘test’
#drop ‘test’


2.3.查看表结构
# 语法:describe <table>
例:查看student表的结构
#describe ‘student’

2.4.修改表结构
注意:修改表结构必须先将标置为离线
# 语法:alter 't1', {NAME => 'f1'}, {NAME => 'f2', METHOD => 'delete'}
例:将name的版本设为2
#disable ‘student’
#alter ‘student’,{NAME=>’name’,VERSIONS=’2’}
#enable ‘student’

由于以下需要,所以又把name的版本改了回去。
3.权限管理
3.1.分配权限
# 语法 : grant <user> <permissions> <table> <column family> <column qualifier> 参数后面用逗号分隔
# 权限用五个字母表示: "RWXCA". READ('R'), WRITE('W'), EXEC('X'), CREATE('C'), ADMIN('A')
例:给用户‘root’分配对表‘student’有读写的权限:
#grant ‘root’,’RW’,’student’
3.2.查看权限

3.3.收回权限

4.增删改查
4.1.插入记录
# 语法:put <table>,<rowkey>,<family:column>,<value>,<timestamp>
#例:插入数据到student1表中,rowkey要求是学号,包含姓名、性别、年龄、住址

这里我插入了四个数据。
4.2.查询记录
① 查询某行记录
# 语法:get <table>,<rowkey>,[<family:column>,....]
例1:查询表student1中学号为201601的记录
# get 'student1','201601'

例2:查询表student1中学号为201601中的f1下的name
# get 'student1','201601',‘f1:name’

②扫描表
# 语法:scan <table>, {COLUMNS => [ <family:column>,.... ], LIMIT => num}
#LIMIT控制数据个数
例:扫描表student1前3条数据
#scan ‘student’,{LIMIT=>3}

③查询表中的数据行数
# 语法:count <table>, {INTERVAL => intervalNum, CACHE => cacheNum}
# INTERVAL设置多少行显示一次及对应的rowkey,默认1000;CACHE每次去取的缓存区大小,默认是10,调整该参数可提高查询速度
例:查询表student1中的行数,每100条显示一次,缓存区为500
#count ‘student1’,{INTERVAL=>100,CACHE=>500}

4.3.修改记录
修改记录其实就是插入覆盖。
# 语法:put <table>,<rowkey>,<family:column>,<value>,<timestamp>
例:修改表student中学号为201601的名字
#put 'student1','201601','f1:name','jok'

5.掌握HBase的预分区功能,能够采用预分区设计表
5.1.什么是预分区
HBase表在刚刚被创建时,只有1个分区(region),当一个region过大(达到hbase.hregion.max.filesize属性中定义的阈值,默认10GB)时,表将会进行split,分裂为2个分区。表在进行split的时候,会耗费大量的资源,频繁的分区对HBase的性能有巨大的影响,所以HBase提供了预分区功能,即用户可以在创建表的时候对表按照一定的规则分区。
5.2.预分区设计
①通过手动分区方法
# 语法:create <table>, {NAME => <family>},SPLITS => [,,,,]
示例:
#create 't1', 'f1', SPLITS => ['10', '20', '30', '40']
#create 't1', {NAME =>'f1', TTL => 180}, SPLITS => ['10', '20', '30', '40']
#create 't1', {NAME =>'f1', TTL => 180}, {NAME => 'f2', TTL => 240}, SPLITS => ['10', '20', '30', '40']
注意:SPLITS里面的值代表分区位置,上面的10,20,30,40表示分区位置在这里,所以总共有五个分区。
例:手动分区创建一个有五个分区的表t1
#create 't1', 'f1', SPLITS => ['10', '20', '30', '40']

Web界面查看表结构,http://master:60010(hbase端口号60010)

②通过文件分区方法
# 语法:create <table>, {NAME => <family>},SPLITS => filepath
示例:
#create 't1', 'f1', SPLITS_FILE => '/tmp/splits.txt'
#create 't1', {NAME =>'f1', TTL => 180}, SPLITS_FILE => '/tmp/splits.txt'
#create 't1', {NAME =>'f1', TTL => 180}, {NAME => 'f2', TTL => 240}, SPLITS_FILE => '/tmp/splits.txt'
例:通过文件splits.txt内容建立分区t2
# create 't2', 'f1', SPLITS_FILE => '/tmp/splits.txt'
文件内容:

建立分区表:

Web界面查看分区:

网友评论