HBase series (two) - HBase system architecture and data structure

First, the basic concept

Hbase Table A typical table is as follows:

1.1 Row Key (OK key)

Row KeyIt is the primary key used to retrieve records. You want to access data in HBase Table, only the following three ways:

  • By specifying the Row Keyaccess;

  • Row Key access through the range, i.e. access line within a specified range;

  • A full table scan.

Row KeyIt can be any string, when storing data in Row Keythe lexicographic order. It should be noted that the following two points:

  • Because lexicographical sort of Int result 1,10,100,11,12,13,14,15,16,17,18,19,2,20,21, ..., 9,91,92,93,94,95 , 96,97,98,99. If you use integer string as row keys, then in order to maintain the natural sequence of integers, 0 row keys must be left to fill.

  • Atomic (regardless of how many write a column) when a row of read and write operations.

1.2 Column Family (column family)

Each column HBase table, belongs to a family row. Schema column family is part of the table, the column families need to be defined when creating the table. All columns column families are in a column family name as a prefix, for example courses:history, courses:mathbelong to coursesthis column family.

1.3 Column Qualifier (column qualifier)

Column qualifier, you can be understood as a specific column name, for example courses:history, courses:mathbelong to coursesthis column families, their columns are the qualifiers historyand math. Note that the table is not part of the column qualifier Schema, you can dynamically create columns in the process of inserting data.

1.4 Column (column)

HBase columns by the column and row group qualifiers, which are the :separated (colon), i.e., a full name should be expressed as a column 列族名 :列限定符.

1.5 Cell

CellRow, column and row combinations qualifier group, and contains a timestamp value. You can be understood as the equivalent of a cell is determined by the specified row and a specified column of a relational database, but is a different cell HBase is a plurality of versions of data, each of which version of the data time stamp make that distinction.

1.6 Timestamp (time stamp)

HBase by row keyand columndetermined is referred to as a memory cell Cell. Each Cellis saved with multiple versions of the same data. Indexed by timestamp version, type is 64-bit integer time stamp, the time stamp can be automatically assigned by the data write HBase, you may be explicitly specified by the customer. In each Cell, the different versions of data are arranged in reverse time stamp, i.e. the newest data at the top.

Second, the storage structure

2.1 Regions

All rows HBase Table in accordance with Row Keythe dictionary arrangement order. HBase Tables by range (row key range) row of keys is cut into a plurality of levels Region, one Regioncontaining all the lines between the start key and the end key.

Each table is only a start Region, as data continues to increase, Regionwill continue to increase, when the increase to a threshold of time, Regionit will divided into two new Region. When growing in Table row, there will be more and more Region.

RegionHBase is the distributed storage load balancing and minimum unit . This means that different Regioncan be distributed in a different Region Serveron. But one Regionis not split into multiple Server.

2.2 Region Server

Region ServerRunning on HDFS's DataNode. It has the following components:

  • WAL(Write Ahead Log,预写日志):用于存储尚未进持久化存储的数据记录,以便在发生故障时进行恢复。
  • BlockCache:读缓存。它将频繁读取的数据存储在内存中,如果存储不足,它将按照 最近最少使用原则 清除多余的数据。
  • MemStore:写缓存。它存储尚未写入磁盘的新数据,并会在数据写入磁盘之前对其进行排序。每个 Region 上的每个列族都有一个 MemStore。
  • HFile :将行数据按照 Key\Values 的形式存储在文件系统上。

Region Server 存取一个子表时,会创建一个 Region 对象,然后对表的每个列族创建一个 Store 实例,每个 Store 会有 0 个或多个 StoreFile 与之对应,每个 StoreFile 则对应一个 HFile,HFile 就是实际存储在 HDFS 上的文件。

三、Hbase系统架构

3.1 系统架构

HBase 系统遵循 Master/Salve 架构,由三种不同类型的组件组成:

Zookeeper

  1. 保证任何时候,集群中只有一个 Master;

  2. 存贮所有 Region 的寻址入口;

  3. 实时监控 Region Server 的状态,将 Region Server 的上线和下线信息实时通知给 Master;

  4. 存储 HBase 的 Schema,包括有哪些 Table,每个 Table 有哪些 Column Family 等信息。

Master

  1. 为 Region Server 分配 Region ;

  2. 负责 Region Server 的负载均衡 ;

  3. 发现失效的 Region Server 并重新分配其上的 Region;

  4. GFS 上的垃圾文件回收;

  5. 处理 Schema 的更新请求。

Region Server

  1. Region Server 负责维护 Master 分配给它的 Region ,并处理发送到 Region 上的 IO 请求;

  2. Region Server 负责切分在运行过程中变得过大的 Region。

3.2 组件间的协作

HBase 使用 ZooKeeper 作为分布式协调服务来维护集群中的服务器状态。 Zookeeper 负责维护可用服务列表,并提供服务故障通知等服务:

  • 每个 Region Server 都会在 ZooKeeper 上创建一个临时节点,Master 通过 Zookeeper 的 Watcher 机制对节点进行监控,从而可以发现新加入的 Region Server 或故障退出的 Region Server;

  • 所有 Masters 会竞争性地在 Zookeeper 上创建同一个临时节点,由于 Zookeeper 只能有一个同名节点,所以必然只有一个 Master 能够创建成功,此时该 Master 就是主 Master,主 Master 会定期向 Zookeeper 发送心跳。备用 Masters 则通过 Watcher 机制对主 HMaster 所在节点进行监听;

  • 如果主 Master 未能定时发送心跳,则其持有的 Zookeeper 会话会过期,相应的临时节点也会被删除,这会触发定义在该节点上的 Watcher 事件,使得备用的 Master Servers 得到通知。所有备用的 Master Servers 在接到通知后,会再次去竞争性地创建临时节点,完成主 Master 的选举。

四、数据的读写流程简述

4.1 写入数据的流程

  1. Client 向 Region Server 提交写请求;

  2. Region Server 找到目标 Region;

  3. Region 检查数据是否与 Schema 一致;

  4. 如果客户端没有指定版本,则获取当前系统时间作为数据版本;

  5. 将更新写入 WAL Log;

  6. 将更新写入 Memstore;

  7. 判断 Memstore 存储是否已满,如果存储已满则需要 flush 为 Store Hfile 文件。

更为详细写入流程可以参考:HBase - 数据写入流程解析

4.2 读取数据的流程

以下是客户端首次读写 HBase 上数据的流程:

  1. 客户端从 Zookeeper 获取 META 表所在的 Region Server;

  2. 客户端访问 META 表所在的 Region Server,从 META 表中查询到访问行键所在的 Region Server,之后客户端将缓存这些信息以及 META 表的位置;

  3. 客户端从行键所在的 Region Server 上获取数据。

如果再次读取,客户端将从缓存中获取行键所在的 Region Server。这样客户端就不需要再次查询 META 表,除非 Region 移动导致缓存失效,这样的话,则将会重新查询并更新缓存。

注:META 表是 HBase 中一张特殊的表,它保存了所有 Region 的位置信息,META 表自己的位置信息则存储在 ZooKeeper 上。

更为详细读取数据流程参考:

HBase 原理-数据读取流程解析

HBase 原理-迟到的‘数据读取流程部分细节

参考资料

本篇文章内容主要参考自官方文档和以下两篇博客,图片也主要引用自以下两篇博客:

官方文档:

更多大数据系列文章可以参见 GitHub 开源项目大数据入门指南

Guess you like

Origin www.cnblogs.com/heibaiying/p/11403502.html