hbase1.2.1多线程并发查询问题

梦幻女侠 发布于 2016/12/09 17:39
阅读 747
收藏 1

大家好,请教个问题,我的hbase多线程并发查询数据出现问题,有的线程查询成功,有的失败

我的hbase使用的版本是1.2.1,使用的hbase工具类为

package com.jinhetech.xman.utils.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;

import com.jinhetech.xman.utils.log.ErrInLog;
import com.jinhetech.xman.utils.log.Logger;
/**
 * hbase客户端
 * @author xxmeng
 *
 */
public class HbaseClient {
	 private static final Logger LOGGER = Logger.getLogger(HbaseClient.class);
	    private Configuration conf = null;
	    private Connection conn = null;
	    private static HbaseClient instance = null;

	    private HbaseClient(){
	        init();
	    }
	    public void init(){
	        try{
	            /*conf.set("hbase.zookeeper.quorum", "127.0.0.1");  
	            conf.set("zookeeper.znode.parent", "/hbase");
	            conf.set("hbase.zookeeper.property.clientPort", "2181"); 
	            conf.set("hbase.client.pause", "50"); 
	            conf.set("hbase.client.retries.number", "3"); 
	            conf.set("hbase.rpc.timeout", "2000"); 
	            conf.set("hbase.client.operation.timeout", "3000"); 
	            conf.set("hbase.client.scanner.timeout.period", "10000"); */
	            conf = HBaseConfiguration.create();
				conf.addResource("config/hbase-site.xml");
	            conn = ConnectionFactory.createConnection(conf);
	        }catch(Exception e){
	            LOGGER.info("初始化hbase连接失败:"+ErrInLog.errInfo(e));
	        }
	    }

	    public static HbaseClient getInstance(){
	        if(instance == null){
	            synchronized (HbaseClient.class) {
	                if(instance == null){
	                    instance = new HbaseClient();
	                }
	            }
	        }
	        return instance;
	    }

	    /**
	     * 获取htable操作类
	     * @param tableName
	     * @return
	     * @throws IOException
	     */
	    public Table getHtable(String tableName) throws IOException{
	        return conn.getTable(TableName.valueOf(tableName));
	    }

	    /**
	     * 
	     * @param hTableInterface
	     */
	    public void relaseHtable(Table table){
	        if(table == null){
	            return;
	        }
	        try {
	            table.close();
	        } catch (IOException e) {
	            LOGGER.info("hbase中表关闭失败:"+ErrInLog.errInfo(e));
	        }
	    }

	    /**
	     * 关闭hbase连接
	     */
	    public void destory(){
	        try {
	            conn.close();
	            instance = null;
	        } catch (IOException e) {
	            LOGGER.info("hbase中连接关闭失败:"+ErrInLog.errInfo(e));
	        }
	    }

	}



在类中使用

HbaseClient client = HbaseClient.getInstance();
Table table = null;
rs = table.getScanner(sc);
// try {
if (rs != null) {
for (Result r : rs) {//这句开始报错
Map<String, Object> map = new HashMap<String, Object>();
for (KeyValue keyValue : r.raw()) {
map.put(new String(keyValue.getQualifier()),
new String(keyValue.getValue()));
}
protoList.add(map);
}
}
} catch (Exception e) {
logger.info("稻瘟病积分计算查询hbase异常");
logger.info("异常:" + ErrInLog.errInfo(e));
} finally {
client.relaseHtable(table);
}
这是怎么回事呢

加载中
0
蜗牛pass
你只关了table,   ResultScanner并没有关闭
梦幻女侠
梦幻女侠
恩,我先关闭下试试
蜗牛pass
回复 @梦幻女侠 : 不用,整个项目开启一个hbase连接就可以了, 它是线程安全的重量级连接
梦幻女侠
梦幻女侠
rs关闭就可以了么?hbase连接不用关闭吧
返回顶部
顶部