public class HBaseClient {
public static final byte[] COLUMN_FAMILY = Bytes.toBytes("t");
private static Connection connection = null;
private static Logger logger = LoggerFactory.getLogger(HBaseClient.class);
static {
try {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "hnode2,hnode4,hnode5");
conf.setInt("hbase.rpc.timeout",1500);
conf.setInt("hbase.client.operation.timeout",2000);
conf.set("hbase.client.ipc.pool.type","RoundRobin");
conf.set("hbase.client.ipc.pool.size", "100");
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
private static Table getTable(String table) throws IOException {
return connection.getTable(TableName.valueOf(table));
}
/**
* 通过指定的table,key,field进行查询,暂时仅支持字符串类型
* @param table 表名
* @param key 查询rowkey
* @param field 字段
* @return 返回结果
*/
public static String query(String table,String key,String field){
String column = "";
try (Table tab = getTable(table)) {
Get get = new Get(Bytes.toBytes(key));
Cell cell = tab.get(get).getColumnLatestCell(COLUMN_FAMILY, Bytes.toBytes(field));
if(cell != null) {
column = Bytes.toString(CellUtil.cloneValue(cell));
}
} catch (Exception e) {
logger.error("查询请求出错:" + e.getMessage());
logger.error(e.toString());
}
return column;
}
。。。。
}
上面是我的客户端代码,总是不定期,一组超时。超时结束的时间特别巧,都是同一时间点:
2018-04-19 10:27:22,387 [http-nio-0.0.0.0-8401-exec-2] INFO com.x.data.xclient.service.RecommendService:54 - requestId:1001_1_4839597_null_1524104839135 耗时:3153
2018-04-19 10:27:22,409 [http-nio-0.0.0.0-8401-exec-7] INFO com.x.data.xclient.service.RecommendService:54 - requestId:1001_1_6560661_null_1524104833326 耗时:8997
2018-04-19 10:27:22,410 [http-nio-0.0.0.0-8401-exec-1] INFO com.x.data.xclient.service.RecommendService:54 - requestId:1001_1_4633235_null_1524104838673 耗时:3658
2018-04-19 10:27:22,442 [http-nio-0.0.0.0-8401-exec-5] INFO com.x.data.xclient.service.RecommendService:54 - requestId:1002_2_null_1922268_1524104831961 耗时:10401
2018-04-19 10:27:22,445 [http-nio-0.0.0.0-8401-exec-9] INFO com.x.data.xclient.service.RecommendService:54 - requestId:1001_1_2041887_null_1524104835395 耗时:6970
2018-04-19 10:27:22,445 [http-nio-0.0.0.0-8401-exec-10] INFO com.x.data.xclient.service.RecommendService:54 - requestId:1001_1_3392255_null_1524104833065 耗时:9327
2018-04-19 10:27:22,451 [http-nio-0.0.0.0-8401-exec-8] INFO com.x.data.xclient.service.RecommendService:54 - requestId:1001_1_4796956_null_1524104837966 耗时:4432
2018-04-19 10:27:22,468 [http-nio-0.0.0.0-8401-exec-11] INFO com.x.data.xclient.service.RecommendServi
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。