3.1 多htable併發讀
建立多個htable客戶端用於讀操作,提高讀資料的吞吐量,乙個例子:
3.2 htable引數設定static final configuration conf = hbaseconfiguration.create();
static final string table_log_name = 「user_log」;
rtablelog = new htable[tablen];
for (int i = 0; i < tablen; i++)
通過呼叫htable.setscannercaching(int scannercaching)可以設定hbase scanner一次從服務端抓取的資料條數,預設情況下一次一條。通過將此值設定成乙個合理的值,可以減少scan過程中next()的時間開銷,代價是scanner需要通過客戶端的記憶體來維持這些被cache的行記錄。
3.2.2 scan attribute selection
scan時指定需要的column family,可以減少網路傳輸資料量,否則預設scan操作會返回整行所有column family的資料。
3.2.3 close resultscanner
通過scan取完資料後,記得要關閉resultscanner,否則regionserver可能會出現問題(對應的server資源無法釋放)。
3.3 批量讀
通過呼叫htable.get(get)方法可以根據乙個指定的row key獲取一行記錄,同樣hbase提供了另乙個方法:通過呼叫htable.get(list)方法可以根據乙個指定的row key列表,批量獲取多行記錄,這樣做的好處是批量執行,只需要一次網路i/o開銷,這對於對資料實時性要求高而且網路傳輸rtt高的情景下可能帶來明顯的效能提公升。
3.4 多執行緒併發讀
在客戶端開啟多個htable讀執行緒,每個讀執行緒負責通過htable物件進行get操作。下面是乙個多執行緒併發讀取hbase,獲取店鋪一天內各分鐘pv值的例子:
3.5 快取查詢結果public class datareaderserver
return parallelbatchminutepv(lst);
} //多執行緒併發查詢,獲取分鐘pv值
private static concurrenthashmap parallelbatchminutepv(list lstkeys)
else
for(int i = 0 ; i < lstkeys.size() ; i ++ )
} list >> futures = new arraylist >>(5);
threadfactorybuilder builder = new threadfactorybuilder();
builder.setnameformat("parallelbatchquery");
threadfactory factory = builder.build();
threadpoolexecutor executor = (threadpoolexecutor) executors.newfixedthreadpool(lstbatchkeys.size(), factory);
for(list keys : lstbatchkeys)
executor.shutdown();
// wait for all the tasks to finish
try catch (exception e)
} } catch (interruptedexception e) catch (exception e1)
} // look for any exception
for (future f : futures)
} catch (interruptedexception e) catch (exception e1)
} catch (executionexception e)
} return hashret;
} //乙個執行緒批量查詢,獲取分鐘pv值
protected static concurrenthashmap getbatchminutepv(list lstkeys)
result res = null;
try catch (ioexception e1)
if (res != null && res.length > 0)
} catch (exception e2)
} }
} return hashret;
} } //呼叫介面類,實現callable介面
class batchminutepvcallable implements callable>
public concurrenthashmap call() throws exception
}
對於頻繁查詢hbase的應用場景,可以考慮在應用程式中做快取,當有新的查詢請求時,首先在快取中查詢,如果存在則直接返回,不再查詢hbase;否則對hbase發起讀請求查詢,然後在應用程式中將查詢結果快取起來。至於快取的替換策略,可以考慮lru等常用的策略。
3.6 blockcache
hbase上regionserver的記憶體分為兩個部分,一部分作為memstore,主要用來寫;另外一部分作為blockcache,主要用於讀。
寫請求會先寫入memstore,regionserver會給每個region提供乙個memstore,當memstore滿64mb以後,會啟動 flush重新整理到磁碟。當memstore的總大小超過限制時(heapsize * hbase.regionserver.global.memstore.upperlimit * 0.9),會強行啟動flush程序,從最大的memstore開始flush直到低於限制。
讀請求先到memstore中查資料,查不到就到blockcache中查,再查不到就會到磁碟上讀,並把讀的結果放入blockcache。由於blockcache採用的是lru策略,因此blockcache達到上限(heapsize * hfile.block.cache.size * 0.85)後,會啟動淘汰機制,淘汰掉最老的一批資料。
乙個regionserver上有乙個blockcache和n個memstore,它們的大小之和不能大於等於heapsize * 0.8,否則hbase不能啟動。預設blockcache為0.2,而memstore為0.4。對於注重讀響應時間的系統,可以將 blockcache設大些,比如設定blockcache=0.4,memstore=0.39,以加大快取的命中率。
HBASE部分 HBASE的架構
hbase的架構 包含訪問hbase的介面並維護cache來加快對hbase的訪問 zookeeper 保證任何時候,集群中只有乙個master 存貯所有region的定址入口。實時監控region server的上線和下線資訊。並實時通知master 儲存hbase的schema和table元資料...
hbase資料讀取優化 HBase效能優化 總結篇
1 hbase.hregion.max.filesize應該設定多少合適 預設值 256m 說明 maximum hstorefile size.if any one of a column families hstorefiles has?grown to exceed this value,th...
HBase查詢優化
1.概述 hbase是乙個實時的非關係型資料庫,用來儲存海量資料。但是,在實際使用場景中,在使用hbase api查詢hbase中的資料時,有時會發現資料查詢會很慢。本篇部落格將從客戶端優化和服務端優化兩個方面來介紹,如何提高查詢hbase的效率。2.內容 這裡,我們先給大家介紹如何從客戶端優化查詢...