refactor(EmpHBaseClient): 重构 HBase 客户端代码

- 将配置和连接相关代码提取到静态初始化方法中
- 添加静态 Admin 对象以提高性能
- 优化异常处理,使用 RuntimeException 抛出运行时异常
- 调整 HBase 客户端配置参数,提高连接可靠性
- 重构 close 方法,确保资源正确释放
- 新增 main 方法作为程序入口,演示功能使用
This commit is contained in:
fly6516 2025-03-26 10:04:55 +08:00
parent 5db4f7bca1
commit c3464a2571

View File

@ -32,30 +32,48 @@ import org.apache.hadoop.hbase.util.Bytes;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
public class EmpHBaseClient { public class EmpHBaseClient {
private Configuration conf; private static Configuration conf;
private Connection connection; private static Connection connection;
private static Admin admin;
private Table table; // 添加表对象成员变量 private Table table; // 添加表对象成员变量
public EmpHBaseClient() { // 新增静态初始化方法
public static void init() {
conf = HBaseConfiguration.create(); conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost"); conf.set("hbase.zookeeper.quorum", "localhost");
// 新增ZooKeeper端口配置默认2181但显式声明更可靠
conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.property.clientPort", "2181");
// 新增重试策略配置解决临时连接问题 conf.set("hbase.client.retries.number", "5");
conf.set("hbase.client.retries.number", "3"); conf.set("hbase.client.pause", "1000");
conf.set("hbase.rpc.timeout", "60000"); conf.set("hbase.rpc.timeout", "120000");
conf.set("hbase.zookeeper.connection.timeout", "30000");
try { try {
connection = ConnectionFactory.createConnection(conf); connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("HBase连接初始化失败", e);
}
}
// 修改构造函数依赖静态连接
public EmpHBaseClient() {
if (connection == null) {
init();
}
try {
table = connection.getTable(TableName.valueOf("emp1520")); table = connection.getTable(TableName.valueOf("emp1520"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("获取表失败", e);
} }
} }
// 创建职工表结构 // 创建职工表结构
public void createEmpTable() throws IOException { public void createEmpTable() throws IOException {
try (Admin admin = connection.getAdmin()) { if (admin == null) {
throw new IOException("Admin未初始化");
}
if (admin.tableExists(TableName.valueOf("emp1520"))) { if (admin.tableExists(TableName.valueOf("emp1520"))) {
System.out.println("Table already exists"); System.out.println("Table already exists");
return; return;
@ -68,7 +86,6 @@ public class EmpHBaseClient {
tableDesc.addFamily(new HColumnDescriptor("training")); // 存储培训课程 tableDesc.addFamily(new HColumnDescriptor("training")); // 存储培训课程
admin.createTable(tableDesc); admin.createTable(tableDesc);
} }
}
// 新增方法生成RowKey前缀MD5处理 // 新增方法生成RowKey前缀MD5处理
private String generateRowKeyPrefix(String empno) { private String generateRowKeyPrefix(String empno) {
@ -126,9 +143,13 @@ public class EmpHBaseClient {
} }
// 修改构造函数关闭资源时确保连接释放新增close方法 // 修改构造函数关闭资源时确保连接释放新增close方法
public void close() throws IOException { public static void close() {
if (table != null) table.close(); try {
if (admin != null) admin.close();
if (connection != null) connection.close(); if (connection != null) connection.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
// 新增查询ID 7500以上员工方法 // 新增查询ID 7500以上员工方法
@ -247,9 +268,9 @@ public class EmpHBaseClient {
// 新增main方法作为入口 // 新增main方法作为入口
public static void main(String[] args) { public static void main(String[] args) {
EmpHBaseClient client = null;
try { try {
client = new EmpHBaseClient(); EmpHBaseClient.init(); // 新增静态初始化调用
EmpHBaseClient client = new EmpHBaseClient();
System.out.println("1. 创建表结构..."); System.out.println("1. 创建表结构...");
client.createEmpTable(); client.createEmpTable();
@ -271,13 +292,7 @@ public class EmpHBaseClient {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (client != null) { EmpHBaseClient.close(); // 新增静态关闭调用
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
} }
} }