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.disableTable(tableName);
//删除表
admin.deleteTable(tableName);
}
//表的描述类,对应的表是tableName
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
//代表列族base的对象
HColumnDescriptor columnDesc = new HColumnDescriptor("base");
//将列族赋值给表的描述类
tableDesc.addFamily(columnDesc);
//创建表
admin.createTable(tableDesc);
}
@Test
public void testInster() throws Exception {
//获取Put对象
Put put = new Put("1111".getBytes());
put.add("base".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.add("base".getBytes(), "age".getBytes(), "20".getBytes());
put.add("base".getBytes(), "sex".getBytes(), "male".getBytes());
put.add("base".getBytes(), "addr".getBytes(), "China".getBytes());
table.put(put);
}
@Test
public void testInsters() throws Exception {
//获取List集合存放Put对象
List<Put> putList = new ArrayList<>();
Put put = null;
for (int i = 0; i < 1000; i++) {
put = new Put(Integer.toString(1111 + i).getBytes());
put.add("base".getBytes(), "name".getBytes(), ("zhangsan" + i).getBytes());
put.add("base".getBytes(), "age".getBytes(), ("20" + i).getBytes());
put.add("base".getBytes(), "sex".getBytes(), ("male" + i).getBytes());
put.add("base".getBytes(), "addr".getBytes(), ("China" + i).getBytes());
//将Put对象存放到List集合中
putList.add(put);
}
table.put(putList);
}
@Test
public void testInsters1000() throws Exception {
//获取Put对象
Put put = null;
for (int i = 1000; i < 2000; i++) {
put = new Put(Integer.toString(1111 + i).getBytes());
put.add("base".getBytes(), "name".getBytes(), ("zhangsan" + i).getBytes());
put.add("base".getBytes(), "age".getBytes(), ("20" + i).getBytes());
put.add("base".getBytes(), "sex".getBytes(), ("male" + i).getBytes());
put.add("base".getBytes(), "addr".getBytes(), ("China" + i).getBytes());
//直接提交到table中
table.put(put);
}
}
@Test
public void testGet() throws Exception {
//获取Get对象
Get get = new Get("2111".getBytes());
//获取结果
Result result = table.get(get);
Cell nameCell = result.getColumnLatestCell("base".getBytes(), "name".getBytes());
Cell ageCell = result.getColumnLatestCell("base".getBytes(), "age".getBytes());
Cell sexCell = result.getColumnLatestCell("base".getBytes(), "sex".getBytes());
Cell addrCell = result.getColumnLatestCell("base".getBytes(), "addr".getBytes());
byte[] nameBytes = CellUtil.cloneValue(nameCell);
byte[] ageBytes = CellUtil.cloneValue(ageCell);
byte[] sexBytes = CellUtil.cloneValue(sexCell);
byte[] addrBytes = CellUtil.cloneValue(addrCell);
String name = Bytes.toString(nameBytes);
String age = Bytes.toString(ageBytes);
String sex = Bytes.toString(sexBytes);
String addr = Bytes.toString(addrBytes);
System.out.println(name);
System.out.println(age);
System.out.println(sex);
System.out.println(addr);
}
@Test
public void testGetSome() throws IOException {
Get get = new Get("1111".getBytes());
//指定查询的列
get.addColumn("base".getBytes(), "name".getBytes());
get.addColumn("base".getBytes(), "sex".getBytes());
Result result = table.get(get);
Cell nameCell = result.getColumnLatestCell("base".getBytes(), "name".getBytes());
Cell sexCell = result.getColumnLatestCell("base".getBytes(), "sex".getBytes());
byte[] nameBytes = CellUtil.cloneValue(nameCell);
byte[] sexBytes = CellUtil.cloneValue(sexCell);
String name = Bytes.toString(nameBytes);
String sex = Bytes.toString(sexBytes);
System.out.println(name);
System.out.println(sex);
}
@Test
public void testScan() throws Exception {
Scan scan = new Scan();
//直接指定查询哪些列
scan.addColumn("base".getBytes(), "name".getBytes());
scan.addColumn("base".getBytes(), "age".getBytes());
//字典序从2000开始查询,包括2000
scan.setStartRow("2000".getBytes());
//字典序到2005结束查询,不包括2005
scan.setStopRow("2005".getBytes());
ResultScanner rss = table.getScanner(scan);
for (Result result : rss) {
Cell nameCell = result.getColumnLatestCell("base".getBytes(), "name".getBytes());
Cell ageCell = result.getColumnLatestCell("base".getBytes(), "age".getBytes());
System.out.println(Bytes.toString(CellUtil.cloneValue(nameCell)));
System.out.println(Bytes.toString(CellUtil.cloneValue(ageCell)));
}
}
}