开发者社区> 问答> 正文

hbase1.2.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);
}
这是怎么回事呢

展开
收起
爱吃鱼的程序员 2020-06-08 20:11:47 1027 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你只关了table,  ResultScanner并没有关闭恩,我先关闭下试试回复<aclass="referer"target="_blank">@梦幻女侠:不用,整个项目开启一个hbase连接就可以了,它是线程安全的重量级连接rs关闭就可以了么?hbase连接不用关闭吧

    2020-06-08 20:12:05
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
大数据时代的存储 ——HBase的实践与探索 立即下载
Hbase在滴滴出行的应用场景和最佳实践 立即下载
阿里云HBase主备双活 立即下载