本文主要是从HBase应用程序设计与开发的角度,总结几种经常使用的性能优化方法。有关HBase系统配置级别的优化,可参考:淘宝Ken Wu同窗的博客。html
下面是本文总结的第三部份内容:读表操做相关的优化方法。缓存
建立多个HTable客户端用于读操做,提升读数据的吞吐量,一个例子:性能优化
Configuration conf = HBaseConfiguration.create(); String table_log_name = “user_log”; rTableLog = HTable[tableN]; ( i = 0; i < tableN; i++) { rTableLog[i] = HTable(conf, table_log_name); rTableLog[i].setScannerCaching(50); }
hbase.client.scanner.caching配置项能够设置HBase scanner一次从服务端抓取的数据条数,默认状况下一次一条。经过将其设置成一个合理的值,能够减小scan过程当中next()的时间开销,代价是 scanner须要经过客户端的内存来维持这些被cache的行记录。网络
有三个地方能够进行配置:1)在HBase的conf配置文件中进行配置;2)经过调用HTable.setScannerCaching(int scannerCaching)进行配置;3)经过调用Scan.setCaching(int caching)进行配置。三者的优先级愈来愈高。多线程
scan时指定须要的Column Family,能够减小网络传输数据量,不然默认scan操做会返回整行全部Column Family的数据。
经过scan取完数据后,记得要关闭ResultScanner,不然RegionServer可能会出现问题(对应的Server资源没法释放)。
经过调用HTable.get(Get)方法能够根据一个指定的row key获取一行记录,一样HBase提供了另外一个方法:经过调用HTable.get(List<Get>)方法能够根据一个指定的row key列表,批量获取多行记录,这样作的好处是批量执行,只须要一次网络I/O开销,这对于对数据实时性要求高并且网络传输RTT高的情景下可能带来明显 的性能提高。并发
在客户端开启多个HTable读线程,每一个读线程负责经过HTable对象进行get操做。下面是一个多线程并发读取HBase,获取店铺一天内各分钟PV值的例子:性能
DataReaderServer { ConcurrentHashMap<String, String> getUnitMinutePV( uid, startStamp, endStamp){ min = startStamp; count = ()((endStamp - startStamp) / (60*1000)); List<String> lst = ArrayList<String>(); ( i = 0; i <= count; i++) { min = startStamp + i * 60 * 1000; lst.add(uid + "_" + min); } parallelBatchMinutePV(lst); } ConcurrentHashMap<String, String> parallelBatchMinutePV(List<String> lstKeys){ ConcurrentHashMap<String, String> hashRet = ConcurrentHashMap<String, String>(); parallel = 3; List<List<String>> lstBatchKeys = ; (lstKeys.size() < parallel ){ lstBatchKeys = ArrayList<List<String>>(1); lstBatchKeys.add(lstKeys); } { lstBatchKeys = ArrayList<List<String>>(parallel); ( i = 0; i < parallel; i++ ){ List<String> lst = ArrayList<String>(); lstBatchKeys.add(lst); } ( i = 0 ; i < lstKeys.size() ; i ++ ){ lstBatchKeys.get(i%parallel).add(lstKeys.get(i)); } } List<Future< ConcurrentHashMap<String, String> >> futures = ArrayList<Future< ConcurrentHashMap<String, String> >>(5); ThreadFactoryBuilder builder = ThreadFactoryBuilder(); builder.setNameFormat("ParallelBatchQuery"); ThreadFactory factory = builder.build(); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(lstBatchKeys.size(), factory); (List<String> keys : lstBatchKeys){ Callable< ConcurrentHashMap<String, String> > callable = BatchMinutePVCallable(keys); FutureTask< ConcurrentHashMap<String, String> > future = (FutureTask< ConcurrentHashMap<String, String> >) executor.submit(callable); futures.add(future); } executor.shutdown(); { stillRunning = !executor.awaitTermination( 5000000, TimeUnit.MILLISECONDS); (stillRunning) { { executor.shutdownNow(); } (Exception e) { e.printStackTrace(); } } } (InterruptedException e) { { Thread.currentThread().interrupt(); } (Exception e1) { e1.printStackTrace(); } } (Future f : futures) { { (f.get() != ) { hashRet.putAll((ConcurrentHashMap<String, String>)f.get()); } } (InterruptedException e) { { Thread.currentThread().interrupt(); } (Exception e1) { e1.printStackTrace(); } } (ExecutionException e) { e.printStackTrace(); } } hashRet; } ConcurrentHashMap<String, String> getBatchMinutePV(List<String> lstKeys){ ConcurrentHashMap<String, String> hashRet = ; List<Get> lstGet = ArrayList<Get>(); String[] splitValue = ; (String s : lstKeys) { splitValue = s.split("_"); uid = Long.parseLong(splitValue[0]); min = Long.parseLong(splitValue[1]); [] key = [16]; Bytes.putLong(key, 0, uid); Bytes.putLong(key, 8, min); Get g = Get(key); g.addFamily(fp); lstGet.add(g); } Result[] res = ; { res = tableMinutePV[rand.nextInt(tableN)].get(lstGet); } (IOException e1) { logger.error("tableMinutePV exception, e=" + e1.getStackTrace()); } (res != && res.length > 0) { hashRet = ConcurrentHashMap<String, String>(res.length); (Result re : res) { (re != && !re.isEmpty()) { { [] key = re.getRow(); [] value = re.getValue(fp, cp); (key != && value != ) { hashRet.put(String.valueOf(Bytes.toLong(key, Bytes.SIZEOF_LONG)), String.valueOf(Bytes .toLong(value))); } } (Exception e2) { logger.error(e2.getStackTrace()); } } } } hashRet; } } BatchMinutePVCallable Callable<ConcurrentHashMap<String, String>>{ List<String> keys; BatchMinutePVCallable(List<String> lstKeys ) { .keys = lstKeys; } ConcurrentHashMap<String, String> call() Exception { DataReadServer.getBatchMinutePV(keys); } }
对于频繁查询HBase的应用场景,能够考虑在应用程序中作缓存,当有新的查询请求时,首先在缓存中查找,若是存在则直接返回,再也不查询HBase;不然对HBase发起读请求查询,而后在应用程序中将查询结果缓存起来。至于缓存的替换策略,能够考虑LRU等经常使用的策略。优化
HBase上Regionserver的内存分为两个部分,一部分做为Memstore,主要用来写;另一部分做为BlockCache,主要用于读。ui
写请求会先写入Memstore,Regionserver会给每一个region提供一个Memstore,当Memstore满64MB之后,会 启动 flush刷新到磁盘。当Memstore的总大小超过限制时(heapsize * hbase.regionserver.global.memstore.upperLimit * 0.9),会强行启动flush进程,从最大的Memstore开始flush直到低于限制。spa
读请求先到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,以加大缓存的命中率。
有关BlockCache机制,请参考这里:HBase的Block cache,HBase的blockcache机制,hbase中的缓存的计算与使用。