HBase 優化

第 7 章HBase 優化

7.1 高可用

在 HBase 中 Hmaster 負責監控 RegionServer 的生命週期,均衡 RegionServer 的負載,如
果 Hmaster 掛掉了,那麼整個 HBase 集羣將陷入不健康的狀態,而且此時的工做狀態並不
會維持過久。因此 HBase 支持對 Hmaster 的高可用配置。
 

1.關閉 HBase 集羣(若是沒有開啓則跳過此步)

[lxl@hadoop102 hbase]$ bin/stop-hbase.sh

 

2.在 conf 目錄下建立 backup-masters 文件

[lxl@hadoop102 hbase]$ touch conf/backup-masters

 

3.在 backup-masters 文件中配置高可用 HMaster 節點

[lxl@hadoop102 hbase]$ echo hadoop103 > conf/backup-masters

 

4.將整個 conf 目錄 scp 到其餘節點

[atguigu@hadoop102 hbase]$ scp -r conf/ hadoop103:/opt/module/hbase/
[atguigu@hadoop102 hbase]$ scp -r conf/ hadoop104:/opt/module/hbase/

 

或者使用分發文件的腳本:
[lxl@hadoop102 hbase]$ xsync conf/backup-masters 

 

5.啓動hbase: 

[lxl@hadoop102 hbase]$ bin/start-hbase.sh

starting master, logging to /opt/module/hbase/logs/hbase-lxl-master-hadoop102.out
hadoop102: starting regionserver, logging to /opt/module/hbase/bin/../logs/hbase-lxl-regionserver-hadoop102.out
hadoop104: starting regionserver, logging to /opt/module/hbase/bin/../logs/hbase-lxl-regionserver-hadoop104.out
hadoop103: starting regionserver, logging to /opt/module/hbase/bin/../logs/hbase-lxl-regionserver-hadoop103.out
hadoop103: starting master, logging to /opt/module/hbase/bin/../logs/hbase-lxl-master-hadoop103.out
[lxl@hadoop102 hbase]$ util.sh
================ lxl@hadoop102 ================
13040 HMaster
2868 NodeManager
2949 JobHistoryServer
13190 HRegionServer
3115 QuorumPeerMain
2460 NameNode
13535 Jps
2575 DataNode
================ lxl@hadoop103 ================
8562 Jps
8307 HMaster
2868 QuorumPeerMain
8229 HRegionServer
2407 ResourceManager
2268 DataNode
2525 NodeManager
================ lxl@hadoop104 ================
2384 SecondaryNameNode
8864 Jps
2484 NodeManager
8676 HRegionServer
2646 QuorumPeerMain
2279 DataNodenode

 

 

 

6.打開頁面測試查看

http://hadooo102:16010

 

 

 

 

 

7.2 預分區

  每個 region 維護着 startRow 與 endRowKey,若是加入的數據符合某個 region 維護的
rowKey 範圍,則該數據交給這個 region 維護。那麼依照這個原則,咱們能夠將數據所要投
放的分區提早大體的規劃好,以提升 HBase 性能。

1.手動設定預分區

hbase> create 'staff1','info','partition1',SPLITS => ['1000','2000','3000','4000']

 

 

2.生成 16 進制序列預分區

  hbase(main):002:0> create 'staff2','info','partition2',{NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}

 

 

3.按照文件中設置的規則預分區

建立 splits.txt 文件內容以下:
aaaa
dddd
cccc
bbbb

 

 

而後執行:
hbase(main):003:0> create 'staff3','partition3',SPLITS_FILE => 'splits.txt'

 

 

4.使用 JavaAPI 建立預分區

//自定義算法,產生一系列 Hash 散列值存儲在二維數組中
byte[][] splitKeys = 某個散列值函數
//建立 HBaseAdmin 實例
HBaseAdmin hAdmin = new HBaseAdmin(HBaseConfiguration.create());
//建立 HTableDescriptor 實例
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
//經過 HTableDescriptor 實例和散列值二維數組建立帶有預分區的 HBase 表
hAdmin.createTable(tableDesc, splitKeys);

 

 

 

7.3 RowKey 設計

  一條數據的惟一標識就是 rowkey,那麼這條數據存儲於哪一個分區,取決於 rowkey 處於
哪一個一個預分區的區間內,設計 rowkey的主要目的 ,就是讓數據均勻的分佈於全部的 region
中,在必定程度上防止數據傾斜。接下來咱們就談一談 rowkey 經常使用的設計方案。
 

1.生成隨機數、hash、散列值

好比:
原 本 rowKey 爲 1001 的 , SHA1 後變成:
dd01903921ea24941c26a48f2cec24e0bb0e8cc7
原 本 rowKey 爲 3001 的 , SHA1 後變成:
49042c54de64a1e9bf0b33e00245660ef92dc7bd
原 本 rowKey 爲 5001 的 , SHA1 後變成:
7b61dec07e02c188790670af43e717f0f46e8913
在作此操做以前,通常咱們會選擇從數據集中抽取樣本,來決定什麼樣的 rowKey 來 Hash
後做爲每一個分區的臨界值。

 

2.字符串反轉

20170524000001 轉成 10000042507102
20170524000002 轉成 20000042507102
這樣也能夠在必定程度上散列逐步 put 進來的數據。
 

3.字符串拼接

20170524000001_a12e
20170524000001_93i7

 

 

 

7.4 內存優化

  HBase 操做過程當中須要大量的內存開銷,畢竟 Table 是能夠緩存在內存中的,通常會分
配整個可用內存的 70%給 HBase 的 Java 堆。可是不建議分配很是大的堆內存,由於 GC 過
程持續過久會致使 RegionServer 處於長期不可用狀態, 通常 16~48G 內存就能夠了,若是因
爲框架佔用內存太高致使系統內存不足,框架同樣會被系統服務拖死。
 
 

7.5 基礎優化

1.容許在 HDFS 的文件中追加內容

hdfs-site.xml、hbase-site.xml
屬性:dfs.support.append
解釋:開啓 HDFS 追加同步,能夠優秀的配合 HBase 的數據同步和持久化。默認值爲 true

 

2.優化 DataNode 容許的最大文件打開數

hdfs-site.xml
屬性:dfs.datanode.max.transfer.threads
解釋:HBase 通常都會同一時間操做大量的文件,根據集羣的數量和規模以及數據動做,//合併文件或者刷寫的時候會操做大量的文件
設置爲 4096 或者更高。默認值:4096  //須要看集羣的大小而定

 

3.優化延遲高的數據操做的等待時間

hdfs-site.xml
屬性:dfs.image.transfer.timeout
解釋:若是對於某一次數據操做來說,延遲很是高,socket 須要等待更長的時間,建議把
該值設置爲更大的值(默認 60000 毫秒),以確保 socket 不會被 timeout 掉。

 

4.優化數據的寫入效率

mapred-site.xml
屬性:
mapreduce.map.output.compress
mapreduce.map.output.compress.codec
解釋:開啓這兩個數據能夠大大提升文件的寫入效率,減小寫入時間。第一個屬性值修改成
true,第二個屬性值修改成:org.apache.hadoop.io.compress.GzipCodec 或者
其餘壓縮方式。

 

5.設置 RPC 監聽數量

hbase-site.xml
屬性:hbase.regionserver.handler.count
解釋:默認值爲 30,用於指定 RPC 監聽的數量,能夠根據客戶端的請求數進行調整,讀寫
請求較多時,增長此值。

 

6.優化 HStore 文件大小

hbase-site.xml
屬性:hbase.hregion.max.filesize
解釋:默認值 10737418240(10GB),若是須要運行 HBase 的 MR 任務,能夠減少此值,
由於一個 region 對應一個 map 任務,若是單個 region 過大,會致使 map 任務執行時間
過長。該值的意思就是,若是 HFile 的大小達到這個數值,則這個 region 會被切分爲兩
個 Hfile。

 

7.優化 hbase 客戶端緩存

hbase-site.xml
屬性:hbase.client.write.buffer
解釋:用於指定 HBase 客戶端緩存,增大該值能夠減小 RPC 調用次數,可是會消耗更多內
存,反之則反之。通常咱們須要設定必定的緩存大小,以達到減小 RPC 次數的目的。

 

8.指定 scan.next 掃描 HBase 所獲取的行數

hbase-site.xml
屬性:hbase.client.scanner.caching
解釋:用於指定 scan.next 方法獲取的默認行數,值越大,消耗內存越大。

 

9.flush、compact、split 機制

當 MemStore 達到閾值,將 Memstore 中的數據 Flush 進 Storefile;compact 機制則是把 flush
出來的小文件合併成大的 Storefile 文件。split 則是當 Region 達到閾值,會把過大的 Region
一分爲二。
涉及屬性:
即:128M 就是 Memstore 的默認閾值
hbase.hregion.memstore.flush.size:134217728
即:這個參數的做用是當單個 HRegion 內全部的 Memstore 大小總和超過指定值時,flush 該
HRegion 的全部 memstore。RegionServer 的 flush 是經過將請求添加一個隊列,模擬生產消
費模型來異步處理的。那這裏就有一個問題,當隊列來不及消費,產生大量積壓請求時,可
能會致使內存陡增,最壞的狀況是觸發 OOM。
hbase.regionserver.global.memstore.upperLimit:0.4
hbase.regionserver.global.memstore.lowerLimit:0.38
即:當 MemStore 使用內存總量達到 hbase.regionserver.global.memstore.upperLimit 指定值時,
將會有多個 MemStores flush 到文件中,MemStore flush 順序是按照大小降序執行的,直到
刷新到 MemStore 使用內存略小於 lowerLimit
 
 
一些 flush、compact 的相關默認值參考:
 
    <!-- 一個store裏面容許存的hfile的個數,超過這個個數會被寫到新的一個hfile裏面 也便是每一個region的每一個列族對應的memstore在fulsh爲hfile的時候,默認狀況下當超過3個hfile的時候就會   
        對這些文件進行合併重寫爲一個新文件,設置個數越大能夠減小觸發合併的時間,可是每次合併的時間就會越長 -->  
    <property>  
        <name>hbase.hstore.compactionThreshold</name>  
        <value>3</value>  
        <description>  
            If more than this number of HStoreFiles in any one HStore  
            (one HStoreFile is written per flush of memstore) then a compaction  
            is run to rewrite all HStoreFiles files as one. Larger numbers  
            put off compaction but when it runs, it takes longer to complete.  
        </description>  
    </property>  
    <!-- 每一個minor compaction操做的 容許的最大hfile文件上限 -->  
    <property>  
        <name>hbase.hstore.compaction.max</name>  
        <value>10</value>  
        <description>Max number of HStoreFiles to compact per 'minor'  
            compaction.</description>  
    </property>  


    <!-- regionServer的全局memstore的大小,超過該大小會觸發flush到磁盤的操做,默認是堆大小的40%,並且regionserver級別的   
        flush會阻塞客戶端讀寫 -->  
    <property>  
        <name>hbase.regionserver.global.memstore.size</name>  
        <value></value>  
        <description>Maximum size of all memstores in a region server before  
            new  
            updates are blocked and flushes are forced. Defaults to 40% of heap (0.4).  
            Updates are blocked and flushes are forced until size of all  
            memstores  
            in a region server hits  
            hbase.regionserver.global.memstore.size.lower.limit.  
            The default value in this configuration has been intentionally left  
            emtpy in order to  
            honor the old hbase.regionserver.global.memstore.upperLimit property if  
            present.  
        </description>  
    </property>  
        <!-- 內存中的文件在自動刷新以前可以存活的最長時間,默認是1h -->  
    <property>  
        <name>hbase.regionserver.optionalcacheflushinterval</name>  
        <value>3600000</value>  
        <description>  
            Maximum amount of time an edit lives in memory before being automatically  
            flushed.  
            Default 1 hour. Set it to 0 to disable automatic flushing.  
        </description>  
    </property>  
     <!-- 單個region裏memstore的緩存大小,超過那麼整個HRegion就會flush,默認128M -->  
    <property>  
        <name>hbase.hregion.memstore.flush.size</name>  
        <value>134217728</value>  
        <description>  
            Memstore will be flushed to disk if size of the memstore  
            exceeds this number of bytes. Value is checked by a thread that runs  
            every hbase.server.thread.wakefrequency.  
        </description>  
    </property>  
相關文章
相關標籤/搜索