一、Hbase简介
1.什么是Hbase
HBASE是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBASE技术可在廉价PC Server上搭建起大规模结构化存储集群。
HBASE的目标是存储并处理大型的数据,更具体来说是仅需使用普通的硬件配置,就能够处理由成千上万的行和列所组成的大型数据。
HBASE是Google Bigtable的开源实现,但是也有很多不同之处。比如:Google Bigtable利用GFS作为其文件存储系统,HBASE利用Hadoop HDFS作为其文件存储系统;Google运行MAPREDUCE来处理Bigtable中的海量数据,HBASE同样利用Hadoop MapReduce来处理HBASE中的海量数据;Google Bigtable利用Chubby作为协同服务,HBASE利用Zookeeper作为对应。
2.与传统数据库的对比
传统数据库遇到的问题:
1)数据量很大的时候无法存储
2)没有很好的备份机制
3)数据达到一定数量开始缓慢,很大的话基本无法支撑
HBASE优势:
1)线性扩展,随着数据量增多可以通过节点扩展进行支撑
2)数据存储在hdfs上,备份机制健全
3)通过zookeeper协调查找数据,访问速度块。
3.hbase集群中的角色
1、一个或者多个主节点,Hmaster
2、多个从节点,HregionServer
二、Hbase安装
1.上传hbase安装包(1.2.6)
2.解压
3.配置hbase集群,要修改3个文件(首先Zookeeper集群需要安装好) 注意:要把hadoop的hdfs-site.xml和core-site.xml放到hbase/conf下(我在配置的时候,没有放着两个文件,也可以用)
3.1 vim hbase-env.sh
export JAVA_HOME=/root/app/jdk1.8.0_55
//告诉hbase使用外部的zk
export HBASE_MANAGES_ZK=false
3.2 vim hbase-site.xml
<configuration>
<!-- 指定hbase在HDFS上存储的路径 -->
<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop01:9000/hbase</value>
</property>
<!-- 指定hbase是分布式的 -->
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!-- 指定zk的地址,多个用“,”分割 -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop01:2181,hadoop02:2181,hadoop03:2181</value>
</property>
</configuration>
3.4 vim regionservers
hadoop02
hadoop03
3.5 配置环境变量
vim /etc/profiles
3.6 拷贝hbase到其他节点
scp -r /root/app/hbase-1.2.6-hadoop2/ hadoop02:/root/app/
scp -r /root/app/hbase-1.2.6-hadoop2/ hadoop02:/root/app/
4.启动
在启动之前,需要首先启动zookeeper
start-hbase.sh
三、Hbase原理
1.三个概念:
Zookeeper
保证任何时候,集群中只有一个HMaster;
实时监控HRegion Server的上线和下线信息,并实时通知给HMaster;
存储HBase的schema和table元数据;
HMaster需要知道哪些HRegionServer是活的,可用的。及HRegionServer的位置信息,以便管理HRegionServer。这些信息都有Zookeeper提供!
HMaster
理论上HMaster可以启动多个,但是Zookeeper有Master Election机制保证且允许总有且只有一个Master在运行,来负责Table和Region的管理工作。
管理HRegionServer的负载均衡,调整Region分布;
Region Split后,负责新Region的分布;
在HRegionServer停机后,负责失效HRegionServer上Region迁移工作。
Region Server
监控维护Region,处理对这些Region的响应,请求;
负责切分在运行过程中变得过大的Region。
2.注意:
1)Client访问hbase上数据时并不需要Hmaster参与,数据的读写也只是访问RegioneServer,
HMaster仅仅维护着table和Region的元数据信息,负载很低。
2)HBase是通过DFS client把数据写到HDFS上的
3)每一个HRegionServer有多个HRegion,每一个HRegion有多个Store,每一个Store对应一个列簇。
4)HFile是HBase中真正实际数据的存储格式,HFile是二进制格式文件,StoreFile就是对HFile进行了封装(其实就是一个东西),
然后进行数据的存储。
5)HStore由MemStore(只有一个)和StoreFile(多个)组成。
6)HLog记录数据的变更信息,用来做数据恢复。
3.Hbase 读写数据
1)Hbase 写数据流程
Client先访问zookeeper,然后找到meta表的数据,从meta表获取相应region信息,
根据namespace、表名和rowkey根据meta表的数据找到写入数据对应的region信息
找到对应的regionserver
把数据分别写到HLog和MemStore上一份
MemStore达到一个阈值后则把数据刷成一个StoreFile文件。(若MemStore中的数据有丢失,则可以总HLog上恢复)
当多个StoreFile文件达到一定的大小后,会触发Compact合并操作,合并为一个StoreFile,(这里同时进行版本的合并和数据删除。)
当StoreFile大小超过一定阈值后,会把当前的Region分割为两个(Split),并由Hmaster分配到相应的HRegionServer,实现负载均衡 2)HBase读数据流程
Client先访问zookeeper,从meta表读取region的位置,然后读取meta表中的数据。meta中又存储了用户表的region信息。
根据namespace、表名和rowkey在meta表中找到对应的region信息
找到这个region对应的regionserver
查找对应的region
先从MemStore找数据,如果没有,再到StoreFile上读(为了读取的效率)。
四、Hbase学习
1.HBase 数据模型
1.1.Row Key
与nosql数据库们一样,row key是用来检索记录的主键。访问HBASE table中的行,只有三种方式:
1.通过单个row key访问
2.通过row key的range(正则)
3.全表扫描
Row key行键 (Row key)可以是任意字符串(最大长度 是 64KB,实际应用中长度一般为 10-100bytes),在HBASE内部,row key保存为字节数组。存储时,数据按照Row key的字典序(byte order)排序存储。设计key时,要充分排序存储这个特性,将经常一起读取的行存储放到一起。(位置相关性)
1.2.Columns Family
列簇 :HBASE表中的每个列,都归属于某个列族。列族是表的schema的一部 分(而列不是),必须在使用表之前定义。列名都以列族作为前缀。例如 courses:history,courses:math都属于courses 这个列族。
1.3.Cell
由{row key, columnFamily, version} 唯一确定的单元。cell中 的数据是没有类型的,全部是字节码形式存贮。
关键字:无类型、字节码
1.4.Time Stamp
HBASE 中通过rowkey和columns确定的为一个存贮单元称为cell。每个 cell都保存 着同一份数据的多个版本。版本通过时间戳来索引。时间戳的类型是 64位整型。时间戳可以由HBASE(在数据写入时自动 )赋值,此时时间戳是精确到毫秒 的当前系统时间。时间戳也可以由客户显式赋值。如果应用程序要避免数据版 本冲突,就必须自己生成具有唯一性的时间戳。每个 cell中,不同版本的数据按照时间倒序排序,即最新的数据排在最前面。
为了避免数据存在过多版本造成的的管理 (包括存贮和索引)负担,HBASE提供 了两种数据版本回收方式。一是保存数据的最后n个版本,二是保存最近一段 时间内的版本(比如最近七天)。用户可以针对每个列族进行设置。
2. hbase命令
1)进入shell终端
hbase shell
2)退出
quit Ctrl+C
3)创建表
create 't_user','base_info','family'
4)添加数据
put 't_user','rk001','base_info:name','zhangsan'
5)查看数据
get 't_user','rk001','base_info:name'
6)查看所有表
list
7)查看所有记录
scan 't_user'
8)查看表中的记录总数
count 't_user'
9)描述表
describe 't_user'
10)删除记录
delete 't_user'
11)清空表
truncate 't_user'
12)删除一张表
disable 't_user'----> drop 't_user'
3. hbase依赖zookeeper
1)保存Hmaster的地址和backup-master地址
hmaster:
a)管理HregionServer
b)做增删改查表的节点
c)管理HregionServer中的表分配
2)保存表-ROOT-的地址
hbase默认的根表,检索表。
3)HRegionServer列表
表的增删改查数据。
和hdfs交互,存取数据。
五、使用javaApi开发Hbase
/**
* hbase表的增删
* @author hasee
*
*/
public class TableAdmin {
/**
* 创建表
* @throws Exception
*/
@Test
public void testCreateTable() throws Exception{
//创建一个连接
//Hadoop里边的conf,会加载hdfs-site.xml core-site.xml。mapred-site.xml 等配置文件
//Configuration conf = new Configuration();
//加载hbash-site.xml和hadoop的文件
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
//从连接中获取表管理对象
Admin admin = connection.getAdmin();
//表描述 表名
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("t_people"));
//列簇
HColumnDescriptor descriptor2 = new HColumnDescriptor("base_info");
descriptor2.setMaxVersions(3);
HColumnDescriptor descriptor3 = new HColumnDescriptor("family");
descriptor.addFamily(descriptor2);
descriptor.addFamily(descriptor3);
//创建表
admin.createTable(descriptor);
//关闭资源
admin.close();
connection.close();
}
/**
* 删除表
* @throws Exception
*/
@Test
public void deleteTable() throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
//从连接中获取表管理对象
Admin admin = connection.getAdmin();
admin.disableTable(TableName.valueOf("t_people"));
admin.deleteTable(TableName.valueOf("t_people"));
System.out.println("删除成功");
admin.close();
connection.close();
}
}
public class TableDataCURD1 {
Table table;
Connection connection;
@Before
public void init() throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("t_people"));
}
/**
* 添加数据
* @throws Exception
*/
@Test
public void testPutDate() throws Exception{
Put put = new Put(Bytes.toBytes("rk002"));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
Put put2 = new Put(Bytes.toBytes("rk004"));
put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
List<Put> list = new ArrayList<>();
list.add(put);
list.add(put2);
table.put(list);
table.close();
connection.close();
}
/**
* 删除数据
* @throws Exception
*/
@Test
public void testdeleteDate() throws Exception{
Delete del= new Delete(Bytes.toBytes("rk002"));
table.delete(del);
}
@After
public void last() throws Exception{
table.close();
connection.close();
}
}
public class TableDataCURD2 {
private Table table;
@Before
public void init() throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.3:2181,192.168.1.4:2181,192.168.1.5:2181");
Connection connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("t_people"));
}
/**
* 单行查询
* @throws Exception
* @throws Exception
*/
@Test
public void testQuery() throws Exception{
Get get = new Get(Bytes.toBytes("rk001"));
get.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(Bytes.toString(value2));
}
/**
* 多行查询
* @throws Exception
*/
@Test
public void teseScanDate() throws Exception{
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
byte[] bs = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(bs));
byte[] bs2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(bs2));
System.out.println("-------------------------------------------------");
}
}
/**
* 列值过滤器
* @throws Exception
*/
@Test
public void scanData() throws Exception{
Scan scan = new Scan();
//列值过滤 参数:列簇名 列名 比较符 值
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("base_info"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("zhangsan"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
byte[] bs = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(bs));
byte[] bs2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(bs2));
}
}
/**
* 列名前置过滤器
* @throws Exception
* @throws Exception
*/
@Test
public void testScanByColumn() throws Exception{
Scan scan = new Scan();
//列值过滤 参数:列簇名 列名 比较符 值
ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("name"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
byte[] bs = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(bs));
//byte[] bs2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
//System.out.println(new String(bs2));
}
}
/**
* 多个列名前置过滤器
* @throws Exception
*/
@Test
public void MultipleColumnPrefixFilter() throws Exception{
Scan scan = new Scan();
//列值过滤 参数:列簇名 列名 比较符 值
byte[][] bytes = new byte[][]{Bytes.toBytes("name"),Bytes.toBytes("age")};
MultipleColumnPrefixFilter filter = new org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter(bytes);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
byte[] bs = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(bs));
byte[] bs2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(bs2));
}
}
/**
* rowkey过滤器
* @throws Exception
* @throws Exception
*/
@Test
public void testRowKey() throws Exception{
Scan scan = new Scan();
//列值过滤 参数:列簇名 列名 比较符 值
RowFilter filter = new RowFilter(CompareOp.NOT_EQUAL, new RegexStringComparator("^1234"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
byte[] bs = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(bs));
byte[] bs2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(bs2));
}
}
@After
public void last() throws Exception{
table.close();
}
}