Hbase架构
hbase介绍:HBase是一个高可靠性、高性能、面向列、可伸缩、实时读写的分布式数据库。主要用来存储非结构化和半结构化的松散数据。
a.目录表
hbase:meta作为HBase表存在,从HBase shell的list命令中过滤掉,实际上是一个表,保存系统中所有的region的列表。hbase:meta的位置信息存储在zookeeper中,其实所有查询的入口。
b.Client
包含访问HBase的接口并维护cache来加快对HBase的访问。HBase客户端查找关注的行范围所在的regionserver。它通过查询hbase:meta表来完成此操作。在找到所需的region之后,客户端与提供该region的RegionServer通信,而不是通过Master,并发出读取或写入请求。此信息缓存在客户端中,以便后续请求无需经过查找过程。如果Master的负载均衡器重新平衡或者由于regionserver宕机,都会重新指定该region的regionserver。客户端将重新查询目录表以确定用户region的新位置。
通过Admin进行管理功能的实现。
HBaseAdmin类进行管理。
c.zookkeeper
保证任何时候,集群中只有一个活跃master
存贮所有Region的寻址入口,hbase:meta
实时监控Region server的上线和下线信息。并实时通知Master
存储HBase的schema和table元数据
d.Master
hbase的主节点,管理region的分配,管理对表的操作
e.RegionServer
hbase的从节点
Region server维护region,处理对这些region的IO请求
Region server负责切分在运行过程中变得过大的region(裂变)
HBase自动把表水平划分成多个区域(region),每个region会保存一个表里面某段连续的数据(每条记录都有一个行键,按照行键字典序排列)
一个region由多个store组成,一个store对应一个CF(列族)
store包括位于内存中的memstore和位于磁盘的storefile。
写操作先写入memstore,当memstore中的数据达到某个阈值,hregionserver会启动flashcache进程写入storefile,每次写入形成单独的一个storefile。
当storefile文件的数量增长到一定阈值后,系统会进行合并(minor、major compaction),在合并过程中会进行版本合并和删除工作(majar),形成更大的storefile
当一个region所有storefile的大小和数量超过一定阈值后,会把当前的region分割为两个,并由hmaster分配到相应的regionserver服务器,实现负载均衡
客户端检索数据,先查找memstore,再blockcache(查询缓存),找不到再找storefile
StoreFile以HFile格式保存在HDFS上:
Hbase API操作
package 略
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MyTest {
//hbase的管理工具类
private HBaseAdmin admin;
//hbase表中数据的管理类
private HTable table;
//表名
private String tableName = "tb_user";
@Before
public void beforMethod() throws Exception {
//获取hbase的configuration
Configuration configuration = HBaseConfiguration.create();
//设置如何找到zookeeper
configuration.set("hbase.zookeeper.quorum", "node7,node8,node9");
//初始化admin
admin = new HBaseAdmin(configuration);
//初始化table
table = new HTable(configuration, tableName);
}
@After
public void afterMethod() throws Exception {
//关闭到hbase的连接
table.close();
admin.close();
}
@Test
public void testCreateTable() throws Exception {
//判断表是否已经存在,若已存在,删除
if (admin.tableExists(tableName)) {
//禁用表
admin.