In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "HBase performance optimization method sharing". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Design of the watch
1.1 Pre-Creating Regions
By default, an region partition is automatically created when the HBase table is created, and when the data is imported, all HBase clients write data to this region until the region is large enough. One way to speed up batch writes is to create some empty regions in advance, so that when the data is written to HBase, the data will be load balanced in the cluster according to the region partition.
For more information on pre-partitioning, see Table Creation: Pre-Creating Regions. Here is an example:
Public static boolean createTable (HBaseAdmin admin, HTableDescriptor table, byte [] [] splits) throws IOException {try {admin.createTable (table, splits); return true;} catch (TableExistsException e) {logger.info ("table" + table.getNameAsString () + "already exists"); / / the table already exists... Return false;} public static byte [] [] getHexSplits (String startKey, String endKey, int numRegions) {byte [] [] splits = new byte [numRegions-1] []; BigInteger lowestKey = new BigInteger (startKey, 16); BigInteger highestKey = new BigInteger (endKey, 16); BigInteger range = highestKey.subtract (lowestKey); BigInteger regionIncrement = range.divide (BigInteger.valueOf (numRegions); lowestKey = lowestKey.add (regionIncrement); for (int iRegions; I)
< numRegions-1;i++) { BigInteger key = lowestKey.add(regionIncrement.multiply(BigInteger.valueOf(i))); byte[] b = String.format("6x", key).getBytes(); splits[i] = b; } return splits;}1.2 Row Key HBase中row key用来检索表中的记录,支持以下三种方式: 通过单个row key访问:即按照某个row key键值进行get操作; 通过row key的range进行scan:即通过设置startRowKey和endRowKey,在这个范围内进行扫描; 全表扫描:即直接扫描整张表中所有行记录。 在HBase中,row key可以是任意字符串,最大长度64KB,实际应用中一般为10~100bytes,存为byte[]字节数组,一般设计成定长的。 row key是按照字典序存储,因此,设计row key时,要充分利用这个排序特点,将经常一起读取的数据存储到一块,将最近可能会被访问的数据放在一块。 举个例子:如果最近写入HBase表中的数据是最可能被访问的,可以考虑将时间戳作为row key的一部分,由于是字典序排序,所以可以使用Long.MAX_VALUE - timestamp作为row key,这样能保证新写入的数据在读取时可以被快速命中。 1.3 Column Family 不要在一张表里定义太多的column family。目前Hbase并 不能很好的处理超过2~3个column family的表。因为某个column family在flush的时候,它邻近的column family也会因关联效应被触发flush,最终导致系统产生更多的I/O。感兴趣的同学可以对自己的HBase集群进行实际测试,从得到的测试结果数 据验证一下。 1.4 In Memory 创建表的时候,可以通过HColumnDescriptor.setInMemory(true)将表放到RegionServer的缓存中,保证在读取的时候被cache命中。 1.5 Max Version 创建表的时候,可以通过HColumnDescriptor.setMaxVersions(int maxVersions)设置表中数据的最大版本,如果只需要保存最新版本的数据,那么可以设置setMaxVersions(1)。 1.6 Time To Live 创建表的时候,可以通过HColumnDescriptor.setTimeToLive(int timeToLive)设置表中数据的存储生命期,过期数据将自动被删除,例如如果只需要存储最近两天的数据,那么可以设置 setTimeToLive(2 * 24 * 60 * 60)。 1.7 Compact & Split 在HBase中,数据在更新时首先写入WAL 日志(HLog)和内存(MemStore)中,MemStore中的数据是排序的,当MemStore累计到一定阈值时,就会创建一个新的 MemStore,并且将老的MemStore添加到flush队列,由单独的线程flush到磁盘上,成为一个StoreFile。于此同时, 系统会在zookeeper中记录一个redo point,表示这个时刻之前的变更已经持久化了(minor compact)。 StoreFile是只读的,一旦创建后就不可以再修改。因此Hbase的更新其实是不断追加的操作。当一个Store中的StoreFile达到一定的阈值后,就会进行一次合并(major compact),将对同一个key的修改合并到一起,形成一个大的StoreFile,当StoreFile的大小达到一定阈值后,又会对 StoreFile进行分割(split),等分为两个StoreFile。 由于对表的更新是不断追加的,处理读请求时,需要访问Store中全部的StoreFile和MemStore,将它们按照row key进行合并,由于StoreFile和MemStore都是经过排序的,并且StoreFile带有内存中索引,通常合并过程还是比较快的。 实际应用中,可以考虑必要时手动进行major compact,将同一个row key的修改进行合并形成一个大的StoreFile。同时,可以将StoreFile设置大些,减少split的发生。 2. 写表操作 2.1 多HTable并发写 创建多个HTable客户端用于写操作,提高写数据的吞吐量,一个例子: static final Configuration conf = HBaseConfiguration.create();static final String table_log_name = "user_log";wTableLog = new HTable[tableN];for (int i = 0; i < tableN; i++) { wTableLog[i] = new HTable(conf, table_log_name); wTableLog[i].setWriteBufferSize(5 * 1024 * 1024); //5MB wTableLog[i].setAutoFlush(false);}2.2 HTable参数设置2.2.1 Auto Flush 通过调用HTable.setAutoFlush(false)方法可以将HTable写客户端的自动flush关闭,这样可以批量写入数据到 HBase,而不是有一条put就执行一次更新,只有当put填满客户端写缓存时,才实际向HBase服务端发起写请求。默认情况下auto flush是开启的。 2.2.2 Write Buffer 通过调用HTable.setWriteBufferSize(writeBufferSize)方法可以设置HTable客户端的写 buffer大小,如果新设置的buffer小于当前写buffer中的数据时,buffer将会被flush到服务端。其 中,writeBufferSize的单位是byte字节数,可以根据实际写入数据量的多少来设置该值。 2.2.3 WAL Flag 在HBae中,客户端向集群中的RegionServer提交数据时(Put/Delete操作),首先会先写WAL(Write Ahead Log)日志(即HLog,一个RegionServer上的所有Region共享一个HLog),只有当WAL日志写成功后,再接着写 MemStore,然后客户端被通知提交数据成功;如果写WAL日志失败,客户端则被通知提交失败。这样做的好处是可以做到RegionServer宕机 后的数据恢复。 因此,对于相对不太重要的数据,可以在Put/Delete操作时,通过调用Put.setWriteToWAL(false)或Delete.setWriteToWAL(false)函数,放弃写WAL日志,从而提高数据写入的性能。 值得注意的是:谨慎选择关闭WAL日志,因为这样的话,一旦RegionServer宕机,Put/Delete的数据将会无法根据WAL日志进行恢复。 2.3 批量写 通过调用HTable.put(Put)方法可以将一个指定的row key记录写入HBase,同样HBase提供了另一个方法:通过调用HTable.put(List)方法可以将指定的row key列表,批量写入多行记录,这样做的好处是批量执行,只需要一次网络I/O开销,这对于对数据实时性要求高,网络传输RTT高的情景下可能带来明显的 性能提升。 2.4 多线程并发写 在客户端开启多个HTable写线程,每个写线程负责一个HTable对象的flush操作,这样结合定时flush和写 buffer(writeBufferSize),可以既保证在数据量小的时候,数据可以在较短时间内被flush(如1秒内),同时又保证在数据量大的 时候,写buffer一满就及时进行flush。下面给个具体的例子: for (int i = 0; i < threadN; i++) { Thread th = new Thread() { public void run() { while (true) { try { sleep(1000); //1 second } catch (InterruptedException e) { e.printStackTrace(); } synchronized (wTableLog[i]) { try { wTableLog[i].flushCommits(); } catch (IOException e) { e.printStackTrace(); } } } } }; th.setDaemon(true); th.start();} 3. 读表操作 3.1 多HTable并发读 创建多个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++) { rTableLog[i] = new HTable(conf, table_log_name); rTableLog[i].setScannerCaching(50);}3.2 HTable参数设置3.2.1 Scanner Caching 通过调用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值的例子: public class DataReaderServer { //获取店铺一天内各分钟PV值的入口函数 public static ConcurrentHashMap getUnitMinutePV(long uid, long startStamp, long endStamp){ long min = startStamp; int count = (int)((endStamp - startStamp) / (60*1000)); List lst = new ArrayList(); for (int i = 0; i >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) {Callable
< ConcurrentHashMap >Callable = new BatchMinutePVCallable (keys); FutureTask
< ConcurrentHashMap >Future = (FutureTask
< ConcurrentHashMap >) executor.submit (callable); futures.add (future);} executor.shutdown (); / / Wait for all the tasks to finish try {boolean stillRunning =! executor.awaitTermination (5000000, TimeUnit.MILLISECONDS); if (stillRunning) {try {executor.shutdownNow () } catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (InterruptedException e) {try {Thread.currentThread (). Interrupt ();} catch (Exception E1) {/ / TODO Auto-generated catch block e1.printStackTrace () }} / / Look for any exception for (Future f: futures) {try {if (f.get ()! = null) {hashRet.putAll ((ConcurrentHashMap) f.get ()) }} catch (InterruptedException e) {try {Thread.currentThread () .interrupt ();} catch (Exception E1) {/ / TODO Auto-generated catch block e1.printStackTrace ();}} catch (ExecutionException e) {e.printStackTrace () }} return hashRet;} / / A thread batch query to get the minute PV value protected static ConcurrentHashMap getBatchMinutePV (List lstKeys) {ConcurrentHashMap hashRet = null; List lstGet = new ArrayList (); String [] splitValue = null; for (String s: lstKeys) {splitValue = s.split ("_") Long uid = Long.parseLong (splitValue [0]); long min = Long.parseLong (splitValue [1]); byte [] key = new byte [16]; Bytes.putLong (key, 0, uid); Bytes.putLong (key, 8, min); Get g = new Get (key); g.addFamily (fp); lstGet.add (g) } Result [] res = null; try {res = tableMinutePV [Rand.nextInt (tableN)] .get (lstGet);} catch (IOException E1) {logger.error ("tableMinutePV exception, e =" + e1.getStackTrace ());} if (res! = null & & res.length > 0) {hashRet = new ConcurrentHashMap (res.length) For (Result re: res) {if (re! = null & &! re.isEmpty ()) {try {byte [] key = re.getRow (); byte [] value = re.getValue (fp, cp) If (key! = null & & value! = null) {hashRet.put (String.valueOf (Bytes.toLong (key, Bytes.SIZEOF_LONG)), String.valueOf (Bytes.toLong (value) }} catch (Exception e2) {logger.error (e2.getStackTrace ());} return hashRet;}} / / call the Callable interface class BatchMinutePVCallable implements Callable > {private List keys Public BatchMinutePVCallable (List lstKeys) {this.keys = lstKeys;} public ConcurrentHashMap call () throws Exception {return DataReadServer.getBatchMinutePV (keys);}} 3.5 cache query results
For application scenarios where HBase is queried frequently, you can consider caching in the application. When there is a new query request, first look it up in the cache and return it directly if it exists. If you no longer query the HBase;, otherwise initiate a read request query on the HBase, and then cache the query results in the application. As for cache replacement strategies, you can consider common strategies such as LRU.
3.6 Blockcache
The memory of Regionserver on HBase is divided into two parts, one as Memstore, which is mainly used for writing, and the other as BlockCache, which is mainly used for reading.
The write request will be written to Memstore,Regionserver first and each region will be provided with a Memstore. When the Memstore is full of 64MB, it will start flush to flush to disk. When the total size of the Memstore exceeds the limit (heapsize * hbase.regionserver.global.memstore.upperLimit * 0.9), the flush process is forcibly started, starting from the largest Memstore until the flush is below the limit.
The read request will first check the data in Memstore, and if it cannot be found, it will be checked in BlockCache. If it cannot be found, it will be read on disk, and the read result will be put into BlockCache. Because BlockCache uses the LRU strategy, when the BlockCache reaches the upper limit (heapsize * hfile.block.cache.size * 0.85), the phase-out mechanism will be activated to phase out the oldest batch of data.
There is a BlockCache and N Memstore on a Regionserver, and the sum of their sizes cannot be greater than or equal to heapsize * 0.8, otherwise the HBase cannot be started. The default BlockCache is 0.2 and Memstore is 0.4. For systems that focus on read response time, you can set the BlockCache larger, such as setting BlockCache=0.4,Memstore=0.39, to increase the cache hit ratio.
For the BlockCache mechanism, please refer to here: the blockcache mechanism of HBase's Block cache,HBase, the calculation and use of caching in hbase.
4. Data calculation
4.1 Server-side computing
Coprocessor runs on the HBase RegionServer server, and each Regions maintains a reference to its associated coprocessor implementation class, which can be loaded through the local jar in classpath on RegionServer or the classloader of HDFS.
Currently, several coprocessor are available:
Coprocessor: provides hooks for region management, such as region's open/close/split/flush/compact, etc.
RegionObserver: provides hooks for monitoring table-related operations from the client, such as table get/put/scan/delete, etc.
Endpoint: provides command triggers that can execute arbitrary functions on the region. One use example is column aggregation on the RegionServer side, and here is a code example.
The above is just some basic introduction to coprocessor. I have no experience with its actual use, and I don't know its usability and performance data. Interested students can have a try, welcome to discuss.
4.2 write-end calculation 4.2.1 count
HBase itself can be regarded as a horizontally scalable Key-Value storage system, but its computing power is limited (Coprocessor can provide some server-side computing). Therefore, when using HBase, it is often necessary to calculate from the write side or the reader side, and then return the final calculation result to the caller. Here are two simple examples:
PV calculation: by accumulating the count in the memory of the HBase writer, maintaining the update of the PV value, and in order to achieve persistence, synchronize the PV calculation results to the HBase on a regular basis (for example, 1 second), so that the query side will have a delay of up to 1 second, and you can see the PV results with a second delay.
Minute PV calculation: combined with the PV calculation method mentioned above, the current cumulative PV value is written into HBase every minute according to rowkey + minute as the new rowkey, and then the cumulative PV value before each minute of the day is obtained through scan on the query side, and then the cumulative PV value before and after two minutes is subtracted in sequence to get the current PV value within one minute, and finally get the PV value within each minute of the day.
4.2.2 weight removal
For the calculation of UV, it is an example of de-recalculation. There are two situations:
If the memory can hold, then all existing UV identities can be maintained in the Hash table. Whenever a new identity is coming, quickly look up the Hash to determine whether it is a new UV. If so, the UV value is increased by 1, otherwise the UV value remains the same. In addition, in order to persist or provide to the query interface, the results of UV calculations can be synchronized to HBase on a regular basis (for example, 1 second).
If the memory can not hold, you can consider using Bloom Filter to achieve, so as to reduce the memory footprint as much as possible. In addition to the calculation of UV, judging the existence of URL is also a typical application scenario.
4.3 Reading end Computing
If the response time is demanding (for example, a single http request should be returned within milliseconds), I think it is not appropriate for the reader to do too much complex computing logic, and try to make the reader's function as simple as possible, that is, after reading the data from HBase RegionServer (scan or get), simply assemble the data according to the data format and return it directly to the frontend for use. Of course, if the response time requirements are general, or business characteristics require, you can also do some computing logic on the reader side.
5. Summary
As a Key-Value storage system, HBase is not omnipotent, it has its own unique features. Therefore, when we do the application based on it, we often need to optimize and improve from many aspects (table design, read table operation, write table operation, data calculation, etc.), sometimes even need to optimize the configuration of HBase from the system level, and even optimize HBase itself. This belongs to different levels of categories.
In short, in a nutshell, when optimizing the system, first locate the bottleneck that affects the performance of your program, and then optimize for the line. If the optimization meets your expectations, you can stop the optimization; otherwise, continue to look for new bottlenecks and start a new optimization until the performance requirements are met.
This is the end of the content of "HBase performance Optimization method sharing". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 212
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.