<< 2013年6月6日 | 首页 | 2013年6月8日 >>

Ken Wu's Blog » HBase性能调优

因官方Book Performance Tuning部分章节没有按配置项进行索引,不能达到快速查阅的效果。所以我以配置项驱动,重新整理了原文,并补充一些自己的理解,如有错误,欢迎指正。

配置优化

zookeeper.session.timeout
默认值:3分钟(180000ms)
说明:RegionServer与Zookeeper间的连接超时时间。当超时时间到后,ReigonServer会被Zookeeper从RS集群清单中移除,HMaster收到移除通知后,会对这台server负责的regions重新balance,让其他存活的RegionServer接管.
调优:
这个timeout决定了RegionServer是否能够及时的failover。设置成1分钟或更低,可以减少因等待超时而被延长的failover时间。
不过需要注意的是,对于一些Online应用,RegionServer从宕机到恢复时间本身就很短的(网络闪断,crash等故障,运维可快速介入),如果调低timeout时间,反而会得不偿失。因为当ReigonServer被正式从RS集群中移除时,HMaster就开始做balance了(让其他RS根据故障机器记录的WAL日志进行恢复)。当故障的RS在人工介入恢复后,这个balance动作是毫无意义的,反而会使负载不均匀,给RS带来更多负担。特别是那些固定分配regions的场景。

 

hbase.regionserver.handler.count
默认值:10
说明:RegionServer的请求处理IO线程数。
调优:
这个参数的调优与内存息息相关。
较少的IO线程,适用于处理单次请求内存消耗较高的Big PUT场景(大容量单次PUT或设置了较大cache的scan,均属于Big PUT)或ReigonServer的内存比较紧张的场景。
较多的IO线程,适用于单次请求内存消耗低,TPS要求非常高的场景。设置该值的时候,以监控内存为主要参考。
这里需要注意的是如果server的region数量很少,大量的请求都落在一个region上,因快速充满memstore触发flush导致的读写锁会影响全局TPS,不是IO线程数越高越好。
压测时,开启Enabling RPC-level logging,可以同时监控每次请求的内存消耗和GC的状况,最后通过多次压测结果来合理调节IO线程数。
这里是一个案例?Hadoop and HBase Optimization for Read Intensive Search Applications,作者在SSD的机器上设置IO线程数为100,仅供参考。

阅读全文……

标签 : , ,

hbase ( key 设计 ) 条件查询排序分页

Paging is a very common use-case for web sites and many other applications.  In relational databases, this is easily implemented with LIMIT and OFFSET, or by selecting the row number in the query and adding conditionals based on it’s value.  HBase 0.19.x, on the other hand, does not provide any queries or filters that support paging directly. After a quick example using SQL, I will show how to implement the same functionality in HBase.

Let’s assume that we have a large number of users.  Each user has performed a number of actions.  Each action has a unique identifier, a timestamp, and a name.

This is how you might get the third page of an individual users’ actions using SQL:

SELECT id, name, stamp FROM actions WHERE userid = 1
ORDER BY stamp DESC LIMIT 10 OFFSET 20;

This utilizes secondary indexes on both userid and stamp, meaning to accomplish this query you need at least three indexes on this table as id is the primary key.  Though a simple query to write, you will run into problems as the actions table grows to millions of rows and beyond.  Insertions would look like:

INSERT INTO actions (id, userid, name, stamp) VALUES (newid(), 1, 'Joe User', epoch());

HBase has no real indexes.  Rows are stored in sorted order, and columns in a family are sorted.  For more information, read the HBase Architecture page on the HBase Wiki.

Very conscious of the primary queries we will run on user-actions, we will design an HBase table to support paging queries on per-user, time-ordered lists of actions.

We will use the Java Client API for HBase, specifically the HTable class.  What we are looking for are two methods:

public static List<Action> getUserActions(int userid, int offset, int limit)
public static void putUserAction(Action action)

Please note, I am using a custom object, Action, for simplicity.  It is a client-side holder for the four action fields (id, userid, name, stamp).

There are a number of ways to store your data in HBase that will allow the getUserActions query, but in this case we will go with a very tall table design (lots of rows with few columns in them) rather thanwide (lots of columns in each row).  Specifically, the difference here would be whether you have a row-per-action or a row-per-user.  We will do a row-per-action, but will be designing our row key (the primary key) to be a composite key to allow for grouping and sorting of actions, rather than just the action id.  This means we will not have random-access to an action by it’s id, so rather than defining this as the actions table (which might also exist if you needed actionid random access) we will define it as the useractions table, and we will only store a single column in a single family,content:name.

The row key that we will use in our HBase useractions table is:

<userid><reverse_order_stamp><actionid>

It’s very important that each of these fields is fixed-length and binary so that the lexicographical/ascending byte-ordering of HBase will properly sort our rows.

The userid field will be a 4 byte, big endian integer.  reverse_order_stamp is an 8 byte, big endian long with a value of (Long.MAX_VALUE - epoch).  This is so the most recent stamp is at the top rather than the bottom.  actionid is another 4 byte, big endian integer.  Thankfully, HBase provides helpful utilties in the org.apache.hadoop.hbase.util.Bytes class to deal with this (unfortunately it lacked some key features in 0.19, so the code below makes use of the Bytes class available in 0.20/TRUNK).  Before we get into HBase code, let’s define the helper methods makeActionRow and readActionRow to deal with the composite key:

public static byte [] makeActionRow(int userid, long stamp, int actionid)
throws Exception {
  byte [] useridBytes = Bytes.toBytes(userid);
  byte [] stampBytes = Bytes.toBytes(stamp);
  byte [] actionidBytes = Bytes.toBytes(actionid);
  return Bytes.add(useridBytes, stampBytes, actionidBytes);
}
 
public static Action readActionRow(byte [] row)
throws Exception {
  // Bytes.toInt(byte [] buf, int offset, int length)
  int userid = Bytes.toInt(row,0,4);
  long stamp = Long.MAX_VALUE - Bytes.toLong(row,4,8);
  int actionid = Bytes.toInt(row,12,4);
  return new Action(userid,stamp,actionid);
}

Now that we can deal with the composite keys, insertion is very straightforward:

public static void putUserAction(Action action) throws Exception {
  // Get the fields from the Action object
  int userid = action.getUserID();
  long stamp = Long.MAX_VALUE - action.getStamp();
  int actionid = action.getID();
  String name = action.getName();
 
  // Build the composite row, column, and value
  byte [] row = makeActionRow(userid,stamp,actionid);
  byte [] column = Bytes.toBytes("content:name");
  byte [] value = Bytes.toBytes(name);
 
  // Insert to HBase
  HTable ht = new HTable("useractions");
  BatchUpdate bu = new BatchUpdate(row);
  bu.put(column,value)
  ht.commit(bu);
}

We just serialize the fields into the composite row, and write the single column to HBase in a BatchUpdate.  Reading will deal with unserializing the fields and Scanners.  In addition to matching for the content:name column, we will also specify a startRow and stopRow so that the Scanner only returns results from the user we are looking at.  This way we do not have to worry about jumping to the next user in our code, the Scanner will just stop.

public static List<Action> getUserActions(int userid, int offset, int limit)
throws Exception {
  // Initialize counter and List to return
  int count = 0;
  List<Action> actions = new ArrayList<Action>(limit);
 
  // Initialize startRow, stopRow, and columns to match
  byte [] startRow = makeActionRow(userid,0,0);
  byte [] stopRow = makeActionRow(userid,Long.MAX_VALUE,Integer.MAX_VALUE);
  byte [][] columns = {Bytes.toBytes("content:name")};
 
  // Open Scanner
  HTable ht = new HTable("useractions");
  Scanner s = ht.getScanner(columns,startRow,stopRow);
  RowResult res = null;
 
  // Iterate over Scanner
  while((res = s.next()) != null) {
    // Check if past offset
    if(++count <= offset) continue;
 
    // Get data from RowResult
    byte [] row = res.getRow();
    byte [] value = res.get(columns[0]).getValue();
 
    // Build Action
    Action action = readActionRow(row);
    String name = Bytes.toString(value);
    action.setName(name);
    actions.add(action);
 
    // Check limit
    if(count == offset + limit) break;
  }
  // Cleanup and return
  s.close();
  return actions;
}

The storage of your data must be tied to how you need to query it.  Without a sophisticated query engine or indexing capabilities, you must design to take advantage of sorted rows and columns, potentially designing a table per query type.  Denormalization is okay!

阅读全文……

标签 : ,

Ken Wu's Blog » HBase二级索引与Join

1,按索引建表
每一个索引建立一个表,然后依靠表的row key来实现范围检索。row key在HBase中是以B+ tree结构化有序存储的,所以scan起来会比较效率。
单表以row key存储索引,column value存储id值或其他数据 ,这就是Hbase索引表的结构。

如何Join?
多索引(多表)的join场景中,主要有两种参考方案:

1,按索引的种类扫描各自独立的单索引表,最后将扫描结果merge。
这个方案的特点是简单,但是如果多个索引扫描结果数据量比较大的话,merge就会遇到瓶颈。

比如,现在有一张1亿的用户信息表,建有出生地和年龄两个索引,我想得到一个条件是在杭州出生,年龄为20岁的按用户id正序排列前10个的用户列表。
有一种方案是,系统先扫描出生地为杭州的索引,得到一个用户id结果集,这个集合的规模假设是10万。
然后扫描年龄,规模是5万,最后merge这些用户id,去重,排序得到结果。

这明显有问题,如何改良?
保证出生地和年龄的结果是排过序的,可以减少merge的数据量?但Hbase是按row key排序,value是不能排序的。
变通一下 – 将用户id冗余到row key里?OK,这是一种解决方案了,这个方案的图示如下:

merge时提取交集就是所需要的列表,顺序是靠索引增加了_id,以字典序保证的。

2, 按索引查询种类建立组合索引。
在方案1的场景中,想象一下,如果单索引数量多达10个会怎么样?10个索引,就要merge 10次,性能可想而知。


解决这个问题需要参考RDBMS的组合索引实现。
比如出生地和年龄需要同时查询,此时如果建立一个出生地和年龄的组合索引,查询时效率会高出merge很多。
当然,这个索引也需要冗余用户id,目的是让结果自然有序。结构图示如下:

这个方案的优点是查询速度非常快,根据查询条件,只需要到一张表中检索即可得到结果list。缺点是如果有多个索引,就要建立多个与查询条件一一对应的组合索引,存储压力会增大。

在制定Schema设计方案时,设计人员需要充分考虑场景的特点,结合方案一和二来使用。下面是一个简单的对比:

  单索引 组合索引
检索性能 优异 优异
存储 数据不冗余,节省存储。 数据冗余,存储比较浪费。
事务性 多个索引保证事务性比较困难。 多个索引保证事务性比较困难。
join 性能较差 性能优异
count,sum,avg,etc 符合条件的结果集全表扫描 符合条件的结果集全表扫描

从上表中可以得知,方案1,2都存在更新时事务性保证比较困难的问题。如果业务系统可以接受最终一致性的话,事务性会稍微好做一些。否则只能借助于复杂的分布式事务,比如JTA,Chubby等技术。
count, sum, avg, max, min等聚合功能,Hbase只能通过硬扫的方式,并且很悲剧,你可能需要做一些hack操作(比如加一个CF,value为null),否则你在扫描时可能需要往客户端传回所有数据。
当然你可以在这个场景上做一些优化,比如增加状态表等,但复杂性带来的风险会更高。
还有一种终极解决方案就是在业务上只提供上一页和下一页,这或许是最简单有效的方案了。

阅读全文……

标签 : , ,

HBase存储时间相关多列数据的两种方案 - 大圆那些事 - 博客园

多行单列

表结构设计

Row Key:用户标识ID + (Long.MAX_VALUE - timestamp)

Column Family:’cf’

Column Qualifier:’’

Value:宝贝、URL等

其中,使用(Long.MAX_VALUE – timestamp)作为Row Key的后半部分是为了便于获取最近插入的数据,一个用户标识ID下的数据存储在多个Row Key下,每个Row Key下仅有一个Column Qualifier,表示该用户的一次时间相关的访问数据(访问宝贝、URL等)。

查询方式

1)查询某个特定timestamp下的记录,则使用用户标识ID + (Long.MAX_VALUE - timestamp)进行Get查找;

2)查询某个用户标识ID下所有的记录,则通过Scan.setStartRow(uid)和Scan.setStopRow(uid+1)进行Scan查找;

3)查询某个用户标识ID下最近时间内的N条记录,则通过Scan.setStartRow(uid)和Scan.setStopRow(uid+1)进行Scan查找,但是为了只获取N条记录,可以设置Scan.setCaching(N)优化查询,同时做一次ResultScanner.next(N)得到结果。

单行多列

表结构设计

Row Key:用户标识ID

Column Family:’cf’

Column Qualifier:(Long.MAX_VALUE - timestamp)

Value:宝贝、URL等

其中,使用(Long.MAX_VALUE – timestamp)作为Column Qualifier是为了便于获取最近插入的数据,一个用户标识ID下的数据存储在一个Row Key下,每个Row Key下会有多个Column Qualifier,表示该用户的所有时间相关的访问数据(访问宝贝、URL等)。

查询方式

1)查询某个特定timestamp下的记录,则使用用户标识ID进行Get查找,同时通过Get.addColumn(‘cf’, (Long.MAX_VALUE – timestamp))方法限定要查询的Column Qualifier;

2)查询某个用户标识ID下所有的记录,则直接使用用户标识ID进行Get查找,通过Get.addFamily(‘cf’)方法添加整个Column Family;

3)查询某个用户标识ID下最近时间内的N条记录,则直接使用用户标识ID进行Get查找,通过Get.addFamily(‘cf’)方法添加整个Column Family,通过ColumnCountGetFilter(int N)限制最多要查询返回N条记录。

阅读全文……

标签 : , ,

如何理解Hadoop-Hbase原理与应用小结 - leonarding技术博客 - ITPUB个人空间 - powered by X-Space

1 我们常说HBase是“数据即日志”的数据库,它是怎样修改和删除数据的?和Oracle这类传统的RDBMS有什么区别?
答:首先Hbase中的一个“元素”是由行键、列族名、限定符、时间戳唯一标识的并且行键作为数据行在表里的唯一标识,我们只有通过行键来访问列族别无他法。

修改数据:我们先找到要修改的行键把新的数据记录追加到对应的列族中并打上一个新时间戳代表最新版本。
删除数据:插入带有删除标记的行进入,相当于把整个行键所在的行删了。

小结:hbase中所有修改和删除都是用insert方式来完成的,这是由底层HDFS文件系统特性决定的,HDFS中的文件只能一次性写入不能修改可以删除在写回。因此hbase是天生面向时间查询的数据库。例如 查询最近一段时间一个人发布的博客、发布签名、发布照片so on。

hbase特点
(1)适合大量插入同时key-value查询,例如可以输入一个key查询一个value,还可以输入一组key查询一组value。
(2)瓶颈是硬盘的传输速度,因为有大量的插入操作和读出操作,使用SSD  SCSI  IDE不同的硬盘效率是不同的。
(3)适合数据分析。
(4)列式数据库会把相同列的数据都放在一块即列为单位存储。当我们查询某一列的时候只需要调出相应的块即可,这样还可以减少很多I/O。
(5)如果数据元素间的相似性很高的话可以进行大幅度的压缩,相似度越高压缩比越大,甚至可以压缩到原来十几分之一、上百分之一。即节约了空间又减少了I/O,从而提高性能。
(6)hbase只有主键索引,它使用的是LSM(Log Structure Merge)索引,因为hbase所有的修改都是使用追加方式完成的,从数据流上看按照顺序方式写入与日志写入的方式相同,我们又可以认为数据和日志一体化,这又节约了很多空间。

oracle特点
(1)适合小事务短时间片密集型OLTP系统,例如在线交易系统。
(2)瓶颈是硬盘的寻道时间(磁头移动时间),因为oracle随机写随机修改块,首先要找到块这个过程就是寻道时间,而寻道时间又由硬盘转速决定的,5400 7200 15000转/秒 不同的转速效率也是不同的。
(3)适合做SQL统计。
(4)行式数据库会按照数据行顺序集中存放即行为单位存储。当我们查询某一列的时候必须把表里所有的行读完才能抽取我们所要的行,这样很不划算,还要付出很大的I/O资源。
(5)那么从结构上讲oracle的压缩性能就要略逊一筹。
(6)oracle常用的是B+树索引,比较大小来查找记录,小的走左边大的走右边,如果列中的相似度较高的话性能较差。

2 HBase合并storefile的原因是什么?在合并的过程中会做什么操作?如果在合并过程中恰好有涉及到有关storefile的查询发生,会发生什么情况!

答:首先我们介绍一下Hbase数据存储的物理结构
一个物理节点只能跑一个HRegionserver
一个HRegionServer可以包括很多个Region实例,可以是不同表Region
一个Region包含一个hlog和多个store(一个store就是一个列族,因为同列族元素在物理上存放在同一个地方,不同列族在物理上是分离的)
一个store包含一个memstore和多个storefile
当我们在处理数据的时候,首先把数据加载到memstore,数据越来越多直到memstore占满,再写入硬盘storefile中,每次写入形成一个单独storefile,当storefile达到一定的数量后,就会开始把小storefile合并成大storefile,因为hadoop不擅长处理小文件,文件越大性能越好。
在合并的过程中会抛弃删除标识的行和版本过旧的行(hbase版本抛弃方式(1)我们可以预先定义版本的个数,超过这个值就抛弃(2)还可以预先定义版本的时间长短,超过这个时间就抛弃),合并完后形成更大的storefile,当达到数量再次合并,直到storefile容量超过一定阀值后会把当前的Region进行分裂为2个并由Hmaster(hbase数据库主控节点)分配到不同的HRegionServer服务器处理实现负载均衡。
如果在合并过程中恰好有涉及到有关storefile的查询发生的话,我们先是把小storefile加载到内存中进行合并此时如有用户访问可以在内存中检索相关数据返回给用户,我们可以想象在内存中做一个独立镜像备份专门提供被查询需求,另一个主体在另一块内存空间里进行合并,当合并完成后释放备份的内存空间,返回到原来的状态。
 
3 Hbase具有怎么样的一致性水平
答:hbase是最终一致性的系统,因为hbase是架构在hadoop之上的数据库,“错误是常态”是hadoop座右铭,在cap理论中hbase为了满足可用性和分区容错性牺牲了一部分的数据一致性。

举例:我们要进行电信的指标汇总,并且把汇总结果冗余三份分布在3个datanode中,我们可以设置阀值只要有2份结果保存了我们就可以继续做下面的操作,在一定时间范围内允许第3份结果稍后一致性同步,这就是最终一致性。所以说hbase是适用于AP理论的系统,最终一致性也满足分布式集群的特点。

阅读全文……

标签 : , ,