使用java查询HBase的优化

目前公司的HBase查询速度特别慢,但是从数据库查速度非常快,所以问题应该是出现在java代码的实现上

原来代码:

public Map<String,String> queryByRow(String tableName,String subRowkey){
        HTableInterface table = hBasePool.hTablePool.getTable(tableName); 
        Map<String, String> resultMap = null;
        try {  
        	Scan scan = new Scan();
        	Filter myFilter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator(subRowkey));
        	scan.setFilter(myFilter);
        	long startTime = System.currentTimeMillis();
            ResultScanner scanResult = table.getScanner(scan);
            resultMap = new HashMap<String, String>();  
            String row;
            for (Result res : scanResult) {
          	    row = new String(res.getRow()); 
                for (KeyValue keyValue : res.raw()) { 
                     resultMap.put(row, new String(keyValue.getValue()));    
                }
            }
            return resultMap;
        } catch (IOException e) {  
        	logger.error("hbase过滤查询失败!", e);  
        }
		return null;  
    }

根据准确的rowKey查询,走的却是模糊查询的方法,单个查询最快的永远是使用Get!

现在代码:

public Map<String,String> queryByRow(String tableName,String subRowkey){
    	Get get = new Get(Bytes.toBytes(subRowkey));
        Result result = null;
        Map<String, String> resultMap = new HashMap<String, String>();  
		try {
			result = new HTable(HBasePool.configuration, Bytes.toBytes(tableName)).get(get);
			for (KeyValue kv : result.list()) {
	        	 resultMap.put(subRowkey, new String(Bytes.toString(kv.getValue())));    
	        }
		} catch (Exception e) {
			logger.error("hbase查询失败!", e);  
		}
        return resultMap;
    } 

在main方法里调用:

long time = System.currentTimeMillis();
	    System.out.println(MyHBaseDAO.queryByRow("EmailBank", "1066606726@qq.comSPDB1426005427267").toString());
	    System.out.println(System.currentTimeMillis()-time);

使用原来的方法查询需要半分钟,现在查询只要0.5,0.6秒,提升了近500倍的速度!


java链接HBase的方法

HBasePool类:

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTablePool;
import org.springframework.stereotype.Service;

import com.lkb.util.InfoUtil;

/**
 * @author baitongyang
 * @date 2014-12-24
 * @Description 返回HtablePool实例的bean类。在此配置hbase属性。
 * @version V1.0
 */
public class HBasePool {
    public static Configuration configuration;
    public static HTablePool hTablePool;
    static {  
        configuration = HBaseConfiguration.create();  
//        configuration.set("hbase.zookeeper.property.clientPort", InfoUtil.getInstance().getInfo("hbase/hbase-setting", "hbase.zookeeper.property.clientPort"));  
        configuration.set("hbase.zookeeper.quorum", InfoUtil.getInstance().getInfo("hbase/hbase-setting", "hbase.zookeeper.quorum"));  
//        configuration.set("hbase.master", InfoUtil.getInstance().getInfo("hbase/hbase-setting", "hbase.master"));
//        configuration.set("hbase.rootdir", InfoUtil.getInstance().getInfo("hbase/hbase-setting", "hbase.rootdir"));
        String ms = InfoUtil.getInstance().getInfo("hbase/hbase-setting", "hbase.maxSize");
        int maxSize = NumberUtils.toInt(ms, 500);
        hTablePool = new HTablePool(configuration, maxSize);
    }
}

hbase-setting.properties:

#hbase zookeeper\u5BA2\u6237\u7AEF\u7AEF\u53E3\u53F7
hbase.zookeeper.property.clientPort=2181
#hbase\u8282\u70B9\u5730\u5740
hbase.zookeeper.quorum=HD1,HD3,HD5
#hbase\u4E3B\u8282\u70B9\u5730\u5740
hbase.master=HD1:60000

#hbase maxSize\u6700\u5927\u8FDE\u63A5\u6570
hbase.maxSize=1000

具体参考LKBHbase项目


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值