在使用Hbase的时候,通常涉及到前台分页,而分页又涉及到在前台显示符合条件的总数,在ORACLE中我们可以轻易的获得总条数,但在Hbase中就得做一些配置了,上代码:
1.服务器端配置
在$HBASE_HOME/conf/hbase-site.xml添加一个配置项。具体如下
<property>
<name>hbase.coprocessor.region.classes</name>
<value>org.apache.hadoop.hbase.coprocessor.AggregateImplementation</value>
</property>
若之前未配置此项,则配置完后,需要重启hbase方能生效,客户端的hbase-site.xml最好也加入此配置,以防出现奇怪的问题。
2.客户端使用,直接上代码。
scan直接用查询结果所用的scan即可。
-
privatelonggetTotalNumber(Scanscan){
- AggregationClientaggregationClient=newAggregationClient(conf);
-
longrowCount=0;
-
try{
- <scan.addColumn(columnFamily,etimeQualifier);
- rowCount=aggregationClient.rowCount(tableName,null,scan);
- }catch(Throwablee){
- LOG.fatal("getTotalNumberwrong.");
- e.printStackTrace();
- }
-
returnrowCount;
- }
- 更多hbase性能请参见:http://www.icartype.com