工具类连接方式参考代码如下:
package Util;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class HbaseUtil {
private static Logger logger = Logger.getLogger(HbaseUtil.class);
private static Connection con;
private static Configuration conf;
private static ExecutorService pool = Executors.newFixedThreadPool(120);
static {
try {
conf = HBaseConfiguration.create();
String log4jPath = SysConfig.getValue("log4jPath").trim();
String coresitePath = SysConfig.getValue("coresitePath").trim();
String hdfssitePath = SysConfig.getValue("hdfssitePath").trim();
String mapredsitePath = SysConfig.getValue("mapredsitePath").trim();
String yarnsitePath = SysConfig.getValue("yarnsitePath").trim();
String sslclientPath = SysConfig.getValue("sslclientPath").trim();
String hbasesitePath = SysConfig.getValue("hbasesitePath").trim();
String hostsname = SysConfig.getValue("hostsname").trim();
String keporsPath = SysConfig.getValue("keporsPath").trim();
PropertyConfigurator.configure(log4jPath);
conf.addResource(new Path(coresitePath));
conf.addResource(new Path(hdfssitePath));
conf.addResource(new Path(mapredsitePath));
conf.addResource(new Path(yarnsitePath));
conf.addResource(new Path(sslclientPath));
conf.addResource(new Path(hbasesitePath));
conf.setInt("hbase.client.retries.number", 3);
conf.setInt("hbase.client.pause", 100);
// conf.setInt("hbase.rpc.timeout", 5000);
// conf.setInt("hbase.client.operation.timeout", 3000);
conf.setInt("zookeeper.session.timeout", 6000);
conf.setInt("zookeeper.recovery.retry", 2);
conf.setInt("zookeeper.recovery.retry.intervalmill", 200);
conf.setBoolean("hbase.ipc.client.tcpnodelay", true);
conf.setInt("hbase.ipc.client.connect.max.retries", 2);
conf.setInt("hbase.ipc.client.connection.maxidletime", 20000);
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(hostsname, keporsPath);
con = ConnectionFactory.createConnection(conf, pool);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
if (con == null || con.isClosed()) {
logger.error("HBase重新建立连接 ");
con = ConnectionFactory.createConnection(conf, pool);
}
} catch (IOException e) {
connectionClose();
logger.error("HBase 建立链接失败 ", e);
}
return con;
}
public static void connectionClose() {
try {
if (null != con) {
con.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}