refactor(EmpHBaseClient): 重构 HBase 客户端代码
- 将配置和连接相关代码提取到静态初始化方法中 - 添加静态 Admin 对象以提高性能 - 优化异常处理,使用 RuntimeException 抛出运行时异常 - 调整 HBase 客户端配置参数,提高连接可靠性 - 重构 close 方法,确保资源正确释放 - 新增 main 方法作为程序入口,演示功能使用
This commit is contained in:
parent
5db4f7bca1
commit
c3464a2571
@ -32,42 +32,59 @@ import org.apache.hadoop.hbase.util.Bytes;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
public class EmpHBaseClient {
|
||||
private Configuration conf;
|
||||
private Connection connection;
|
||||
private static Configuration conf;
|
||||
private static Connection connection;
|
||||
private static Admin admin;
|
||||
private Table table; // 添加表对象成员变量
|
||||
|
||||
public EmpHBaseClient() {
|
||||
// 新增静态初始化方法
|
||||
public static void init() {
|
||||
conf = HBaseConfiguration.create();
|
||||
conf.set("hbase.zookeeper.quorum", "localhost");
|
||||
// 新增ZooKeeper端口配置(默认2181但显式声明更可靠)
|
||||
conf.set("hbase.zookeeper.property.clientPort", "2181");
|
||||
// 新增重试策略配置(解决临时连接问题)
|
||||
conf.set("hbase.client.retries.number", "3");
|
||||
conf.set("hbase.rpc.timeout", "60000");
|
||||
conf.set("hbase.client.retries.number", "5");
|
||||
conf.set("hbase.client.pause", "1000");
|
||||
conf.set("hbase.rpc.timeout", "120000");
|
||||
conf.set("hbase.zookeeper.connection.timeout", "30000");
|
||||
|
||||
try {
|
||||
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"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("获取表失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建职工表结构
|
||||
public void createEmpTable() throws IOException {
|
||||
try (Admin admin = connection.getAdmin()) {
|
||||
if (admin.tableExists(TableName.valueOf("emp1520"))) {
|
||||
System.out.println("Table already exists");
|
||||
return;
|
||||
}
|
||||
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("emp1520"));
|
||||
tableDesc.addFamily(new HColumnDescriptor("empnum")); // 存储员工ID
|
||||
tableDesc.addFamily(new HColumnDescriptor("info")); // 存储基本信息
|
||||
tableDesc.addFamily(new HColumnDescriptor("salary")); // 存储薪资
|
||||
tableDesc.addFamily(new HColumnDescriptor("performance")); // 存储绩效信息
|
||||
tableDesc.addFamily(new HColumnDescriptor("training")); // 存储培训课程
|
||||
admin.createTable(tableDesc);
|
||||
if (admin == null) {
|
||||
throw new IOException("Admin未初始化");
|
||||
}
|
||||
if (admin.tableExists(TableName.valueOf("emp1520"))) {
|
||||
System.out.println("Table already exists");
|
||||
return;
|
||||
}
|
||||
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("emp1520"));
|
||||
tableDesc.addFamily(new HColumnDescriptor("empnum")); // 存储员工ID
|
||||
tableDesc.addFamily(new HColumnDescriptor("info")); // 存储基本信息
|
||||
tableDesc.addFamily(new HColumnDescriptor("salary")); // 存储薪资
|
||||
tableDesc.addFamily(new HColumnDescriptor("performance")); // 存储绩效信息
|
||||
tableDesc.addFamily(new HColumnDescriptor("training")); // 存储培训课程
|
||||
admin.createTable(tableDesc);
|
||||
}
|
||||
|
||||
// 新增方法:生成RowKey前缀(MD5处理)
|
||||
@ -126,9 +143,13 @@ public class EmpHBaseClient {
|
||||
}
|
||||
|
||||
// 修改构造函数:关闭资源时确保连接释放(新增close方法)
|
||||
public void close() throws IOException {
|
||||
if (table != null) table.close();
|
||||
if (connection != null) connection.close();
|
||||
public static void close() {
|
||||
try {
|
||||
if (admin != null) admin.close();
|
||||
if (connection != null) connection.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 新增查询ID 7500以上员工方法
|
||||
@ -247,9 +268,9 @@ public class EmpHBaseClient {
|
||||
|
||||
// 新增main方法作为入口
|
||||
public static void main(String[] args) {
|
||||
EmpHBaseClient client = null;
|
||||
try {
|
||||
client = new EmpHBaseClient();
|
||||
EmpHBaseClient.init(); // 新增静态初始化调用
|
||||
EmpHBaseClient client = new EmpHBaseClient();
|
||||
System.out.println("1. 创建表结构...");
|
||||
client.createEmpTable();
|
||||
|
||||
@ -271,13 +292,7 @@ public class EmpHBaseClient {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (client != null) {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
EmpHBaseClient.close(); // 新增静态关闭调用
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user