HBase筆記

 



什麼是 HBase?
HBase 是基於Google的 BigTable 設計的Column導向的資料庫管理系統。它最初由LinkedIn創建,但後來被Yahoo!Inc. 收購,然後出售給Apache軟體基金會,是Apache的Hadoop專案中的子專案。是運行於HDFS檔案系統之上的(無法單獨運行),為Hadoop提供類似BigTable的服務。
column-oriented的非關聯式資料庫,並具有水平擴展的能力。強調"讀寫當下的一致性",但非注重"最終的一致性"。

HBase 是一種基於 Hadoop 分佈式文件系統(HDFS)運行的欄位導向非關係型數據庫管理系統。 (column-oriented non-relational database management system)

HBase其實在最早就跟Zooker,Pig,Sqoop,Mahout機器學習等工具
於2009~2009同一時期問世
從Hadoop生態架構圖上可看到像是Pig,Mahout,Hive與MapReduce的模組都涉及到HBase。
此外,HDFS模組也是在其之下,表示上述這些模組都是based on  HDFS。


何謂column-oriented儲存導向?
白話而言就有點類似傳統資料庫row-oriented儲存導向轉置


row-oriented Database特點 
  • 數據是按照row逐筆存儲的,row存儲的寫入是一個原子動作,消耗的時間比column-oriented少,並且能夠保證數據的完整性,但也代表數據讀取過程中會產生冗余的數據。 
  • 沒有索引的查詢使用大量I/O,可通過索引加快查詢效率,但建立索引和物化視圖需要花費成本高,面對大量的查詢需求,Database必須被大量讀取才能滿足需求。

column-oriented Database特點
  • 數據是按照column逐欄存儲,即每一欄單獨存儲,數據即索引。但在寫入效率、保證數據完整性上都不如row-oriented存儲,它的優勢是在每次讀取只訪問查詢涉及到的column,不會產生冗余數據,可以大量降低系統I/O。
  • 每一column由一個thread來處理,即查詢的併發處裡性能提高,有多少CPU就能用多少CPU。 
  • 每一column數據類型一致,數據特徵相似,可以高效壓縮,提升查詢速度。


Apache HBase 是一個開放原始碼的 NoSQL 分散式大數據存放區。HBase 包含一組表格,這些表格以鍵值格式(Key-Value Pair)儲存資料。是一個基於 Hadoop 的開源排序映射(sorted map)資料庫。這表示資料儲存在個別欄,並由唯一的資料列鍵(RowKey)進行索引。
所以如果向 HBase 中同一張表插入相同 RowKey 的數據,則原先存在的數據會被新的數據給覆蓋掉(和 HashMap 效果相同)。此架構允許快速擷取個別列和欄,並對表格的個別欄進行高效率掃描。HBase可隨機、以嚴格一致性即時存取 PB 資料。HBase 對於處理大型稀疏的資料集非常有效

HBase 背後的基本思想是將數據組織到表中(稱為區域 region)和這些表中的欄位(Column)。每個區域都包含一組儲存格(cells),每個儲存格都包含一個值。單元格被組織成column,column包含值。

HBase 是在 Hadoop 上運行的 NoSQL 資料庫。它是 Hadoop 生態系統的一部分,能夠提供對 Hadoop 檔案系統中資料的隨機實時讀寫訪問。與傳統資料庫一樣,HBase 允許使用者查詢和更新數據行。但與傳統資料庫不同的是,HBase 不強制實施Row-level locking。
反而對於column level提供強一致性。

HBase 非常適合稀疏數據集,這些數據集在大數據使用案例中非常常見。HBase 提供 API,幾乎可以使用任何程式語言進行開發。



為什麼選擇 HBase?
  • 傳統的關係型資料庫(RDBMS)在資料量變大時,性能會指數級下降。
  • 傳統資料庫期望資料具有高度結構化,例如需要符合明確定義的架構。
  • 架構的任何變更可能會需要停機時間。
  • 對於稀疏數據集,維護大量的 NULL 值會造成很大的開銷。

HBase 的特性
  • 水平擴展性:可以隨時新增任意數量的欄位。沒有固定欄位(has no fixed column schema)。屬於線性可擴展(linearly),因為它支持線性和模塊化(modularly)的擴展性(scalable)。
  • 適用於實時數據和對大數據進行隨機讀寫訪問。 HBase 適合用於寫入量大的應用,用於存儲大量數據集,並實時處理和提供分析。 
  •  HBase 可用於高速需求,因為它提供一致的讀寫(consistent reads and writes)
  • 自動故障轉移:自動故障轉移是一種資源,允許系統管理員在系統損壞時自動切換資料處理至備用系統。
  • HBase 是 MapReduce 任務的後備支持。(a backup support for MapReduce jobs)
  • 提供易於使用的 Java API 供客戶端訪問。與 Map/Reduce 框架的整合:所有命令和 Java 程式碼內部實現 Map/Reduce 操作,並構建於 Hadoop 分布式檔案系統之上。
  • 稀疏、分布式、持久性、多維度排序的映射:以行鍵(rowkey)、列鍵(column key)和時間戳(timestamp)為索引。
  • 關鍵值存儲或列族導向的資料庫:可以儲存版本化的映射結構。
  • 隨機訪問資料的平台:本質上是一個用於儲存和檢索資料的平臺。
  • 不關心數據類型:同一列的不同行可以儲存整數或字串等不同數據類型。
  • 不強求數據關係:HBase 不要求資料之間存在關係。
  • 設計為運行在叢集電腦上:利用廉價硬體即可運行,支持跨集群的數據複製(data replication across clusters)


HBase跟 HDFS差異比較

雖然 HBase 和 HDFS 都用於存儲大量數據,但主要差異有以下幾點:
1.HBase 以列(rows)和行(columns)的形式存儲數據,而 HDFS 則在網絡上不同節點分佈式存儲。
2.HBase 允許動態變更,而 HDFS 有一個固定的架構,不允許變更。 
3.HBase 適用於記錄層面上的讀、寫、更新和刪除類型的工作負載。而HDFS適合一次寫入和批量讀取的工作負載。(HDFS 僅是一個底層層面,具有文件系統的屬性)
4.HBase 允許存儲和處理大數據,而 HDFS 僅用於存儲。 


RowKey設計準則

RowKey 是一個二進位碼流,可以是任意字串,最大長度 64kb ,實際應用中一般為 10~100bytes,以 byte[] 形式保存,一般設計成固定長度。
建議越短越好,不要超過 16 個字節,原因如下:

在 HBase 的底層儲存 HFile 中,RowKey 是 KeyValue 結構中的一個域。假設 RowKey 長度 100B,那麼 1000 萬個資料中,光 RowKey 就佔用掉 100*1000w=10億個位元組 將近 1G 空間,這樣會極大影響 HFile 的儲存效率


HBase 中設計有 MemStore 和 BlockCache,分別對應column family級別的寫入緩存,和 RegionServer 級別的讀取緩存。 如果 RowKey 欄位過長,記憶體的有效利用率就會降低,系統不能緩存更多的數據,這樣會降低檢索效率。


MemStore:這是一種寫入緩存,專門用於暫存還沒有寫入到硬碟的數據。在 HBase 中,每個列族(column family)都有一個專門的 MemStore。當數據寫入到 HBase 時,它首先被寫入到 MemStore 中。當 MemStore 的數據達到一定量時,系統會將這些數據「沖刷」(flush)到硬碟上,存儲於一個稱為 HFile 的檔案中。

BlockCache:這是一種讀取緩存,位於 RegionServer 級別。當從 HFile 讀取數據時,數據塊(blocks)會被載入到 BlockCache 中。這樣,後續對這些數據的讀取可以直接從記憶體中完成,而不需要再次讀取硬碟,從而提高讀取效率。

總結:
RowKey 的長度:RowKey 是用於唯一標識每一行數據的鍵。如果 RowKey 過長,它會占用更多的記憶體空間。當 RowKey 過長時,相同的記憶體空間能夠存儲的 RowKey 數量會減少,這意味著 MemStore 和 BlockCache 能緩存的數據量會減少。結果是,當需要讀取數據時,更多的數據需要從硬碟上重新讀取,這會降低數據檢索的效率。


Hbase的缺點
  • 單一RowKey固有的限制決定了它不可能有效地支援多條件查詢
  • 不適合大範圍掃描查詢
  • 不直接支援SQL 的語句查詢

Hbase 的使用案例是什麼?
1.FINRA (美國金融業監管局) 
是美國最大的獨立安全監管機構,負責監督和規範金融交易實務。FINRA 使用 Amazon EMR 在 Amazon S3 上執行 Apache HBase,用於隨機存取互動應用程式的 3 兆筆記錄 (每天增加數十億筆),以搜尋和顯示相關的市場事件。FINRA 透過去耦儲存和運算,可將一份資料副本存放在 Amazon S3 並隨運算容量需求調整叢集大小,而不用為了將資料存放在 3 倍複寫的 HDFS 而調整叢集大小。這樣每年可省下超過 60% 的費用,不但能夠輕鬆擴展運算,還能將全新 EC2 可用區域中叢集的恢復時間從幾天縮短到 30 分鐘以內。

2.Monster 是連結人才與工作機會的全球領導者
他們在 Amazon EMR 上使用 Apache HBase 存放點擊流和廣告活動資料,以便進行下游分析。這讓他們能夠以單一廣告曝光的精細度來監控不同客群在特定活動中的行為。Monster 的分析團隊可輕鬆瀏覽多個資料列來彙總每一使用者的瀏覽和點擊數,以識別行銷活動。另外,他們也利用 Apache HBase 與 Apache Hadoop 生態系統的緊密整合。Monster 在另一個 Amazon EMR 叢集上執行 Apache Hive,透過 SQL 來查詢 HBase 表格,這對於進行額外的分析並將資料從 Apache HBase 匯出到 Amazon Redshift 很有幫助。 



HBase架構圖

HBase 的架構主要由四個組件組成:
  1. HMaster
  2. HRegionserver
  3. HRegions
  4. ZooKeeper
從上述架構圖中可觀察到HBase 坐落在 HDFS 之上並在其上運行。
HDFS 提供了一個分佈式存儲環境,它是一種設計用於通用硬件運行的文件系統。它將每個文件存儲在多個塊中,並通過在 Hadoop 集群中複製塊來維持容錯
容錯:指的是系統或計算機在不利條件下(例如伺服器崩潰時)繼續運行的能力。

HMaster:
  • 是主服務器。它監控區域伺服器實例,將區域指派給區域伺服器,並分配服務到不同的區域伺服器,並管理對架構和元數據操作所做的任何變更。
  • HBase中可以啟動多個HMaster ,透過Zookeeper的Master Election機制,確保總共只有一個Master在運行。
  • 主要負責Table和Region的管理工作:
    1.管理用戶對Table的增刪改查操作,處理schema (metadata)更新請求。
    2.為Region server分配region,管理HRegionServer的負載平衡。
    3.Region Split後,負責新Region的分佈。
    4.在HRegionServer停機後,負責失效HRegionServer上Region遷移。(發現失效的region server並重新分配其上的region)
HRegionServer:
  • 區域伺服器接收來自客戶端的讀寫請求並將請求指派給列族所在的特定區域。它們負責服務和管理分佈式集群中存在的區域。 
  • 區域伺服器不必經常通過 HMaster,可以直接與客戶端通信以便利請求。 
  • 為HBase中最核心的模組,負責回應用戶I/O請求,向失效HDFS檔案系統中讀寫數據。
    1.Region server維護Master分配給它的region ,處理對這些region的IO請求
    2.Region server負責切分在運作過程中變得過大的region
HRegions:
  • 區域是 HBase 集群的基本構建元素和最小單位,包含column families。
  • 它包含多個存儲,每個column family一個, 並有兩部分組成 – HFile 和 Memstore。 

Zookeeper:
  • 是一個用於維護配置信息的中央服務,以維持節點之間的健康連接。
  • 它為分佈式應用提供同步,並用於通過觸發錯誤消息跟蹤伺服器失敗和網絡分割,然後開始修復失敗的節點。 
  • 職責在避免HMaster單點問題。HRegionServer把自己註冊到Zookeeper中,HMaster隨時感知各個HRegionServer的健康狀況。




1.zookeeper的選舉機制保證任何時候,集群中只有一個master。
2.即時監控Region Server的狀態,將Region server的上線和下線資訊即時通知Master
3.儲存Hbase的schema
4.儲存所有Region的尋址入口




Q1.What is an HBase region server?
 An HBase Server Component For Hosting HBase Table Regions
 An HBase Client Tool For Interacting With An HBase Table
 An Extension Point For Custom HBase Table Processing Logic
 None Of The Above


Q2.Which of the following is an HBase tool for compacting HBase table data?
 Apache Flume
 Apache HBase Compact
 Apache Pig
 Apache Hadoop DistCp

Q3.Which of the following is an HBase tool for bulk loading data into HBase?
 Apache HBase Bulk Load
 Apache Pig
 Apache Hadoop DistCp
 Apache Flume


Q2,Q3解釋:
Apache Flume: A tool for streaming data into Hadoop, not related to HBase compaction.
A tool for streaming log and event data into storage systems like HDFS or HBase, but it is not optimized for bulk loading large datasets.

Apache Pig: A high-level platform for processing large data sets, not specific to HBase.
While Pig can interact with HBase, it is primarily a data processing tool and does not handle bulk loading directly.

Apache Hadoop DistCp: A tool for distributed copying of data, unrelated to HBase table compaction.
A distributed copy tool for transferring large datasets between HDFS clusters, not specific to HBase data loading.

Q4.Which of the following is an HBase tool for exporting data from HBase?
 Apache Pig
 Apache Flume
 Apache Hadoop DistCp
 Apache HBase Export

Q4.解釋
Apache HBase Export is a built-in utility in HBase used to export data from HBase tables into HDFS or other storage systems. It allows users to extract table data for backup, migration, or analysis purposes. The export tool is highly efficient and specifically designed for HBase.

Q5.Which of the following is an HBase Java API class for managing HBase table administration tasks?
 HBaseAdmin
 HTableDescriptor
 HTable
 None Of The Above

Q5.解釋
The HBaseAdmin class in the HBase Java API is used for managing administrative tasks related to HBase tables, such as creating, deleting, enabling, disabling, and modifying tables. It provides methods that are essential for HBase table administration.

HTableDescriptor: This class describes the schema of an HBase table (e.g., column families, configurations), but it does not perform administrative tasks.

HTable: This class is used for performing read and write operations on HBase tables but is not for administrative purposes.


HBase與Hadoop HDFS觀念問題補充

Q1.______ released Hadoop to the Apache Software Foundation as an open-source project. (Fill in the blank)

Google
(O)Yahoo
IBM
Amazon

解析:
Yahoo released Hadoop as an open-source project in 2008. 


Q2.Which open-source product is designed to transfer bulk data between relational database systems and Hadoop?
Flume
(O)Sqoop
Pig
Hive

解析:
Sqoop is designed to transfer bulk data between relational database systems and Hadoop.

Q3.Which option represents the correct definition for Map in the MapReduce programming model?
Map aggregates and computes a set of result.
(O)Map processes data into key-value pairs.
Map creates a data dashboard.
Map produces a final output.

解析:
Map processes data into key-value pairs. 
Reduce aggregates and computes results, and produces a final output.


Q4.How does HDFS achieve fault tolerance?
By compressing data to reduce storage requirements
(O)By replicating data blocks across multiple DataNodes
By dividing data into smaller chunks
By using RAID (Redundant Array of Independent Disks) storage

解析:
HDFS achieves fault tolerance by creating multiple copies of data blocks 
on different DataNodes. If one DataNode fails, the data can still be accessed 
from the replicas on other nodes.


Q5.Which of the following file formats does Hive support? Select all that apply.(Multi-select)
Structured format
(O)Sequence file
(O)Record columnar files
(O)Flat files

解析:
Hive supports sequence files consisting of binary key-value pairs. 
Hive supports record columnar files that store table columns in a columnar database. 
Hive supports both flat and text files.


Q6.Which of the following statements is true about HBase? 

Stores data in a distributed manner across nodes on a network
(O)Runs on top of Hadoop Distributed File System (HDFS)
Consists of a fixed column schema
Writes once and is read many times

解析:
HBase runs on top of HDFS. 

Q7.Which component of HBase is a centralized service for maintaining configuration information to maintain healthy links between nodes?

HMaster
(O)ZooKeeper
Region
Region Servers

解析:
ZooKeeper is a centralized service for maintaining configuration information to maintain healthy links between nodes.

Q8.Which of the following statements is true with reference to Hive?

JDBC clients allow application based on ODBC to connect to Hive.
(O)The JDBC client allows Java-based applications to connect to Hive
ODBC clients allow Java applications to connect to Hive.
ODBC client allows applications based on JDBC clients to connect to Hive.

解析:
There are two Hive clients. JDBC clients allow Java-based applications to connect to Hive.










Ref:
HADOOP AND BIG DATA CHALLENGES
https://www.slideshare.net/slideshow/integration-of-hive-and-hbase-12805463/12805463
https://www.cnblogs.com/200911/p/4968968.html
HBase HFile 基本概念
Approaches to Backup and Disaster Recovery in HBase
大白話徹底搞懂HBase Rowkey 設計與實作方式

留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題