refactor(HBaseEmpManager):重构代码并添加注释
- 移动连接和管理员对象到类级别 - 添加初始化和关闭连接的方法 - 优化数据插入和查询逻辑 - 删除冗余的统计方法 - 统一输出信息的语言
This commit is contained in:
parent
9891f61574
commit
76294b7ecf
@ -8,6 +8,10 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class HBaseEmpManager {
|
public class HBaseEmpManager {
|
||||||
|
private static Configuration configuration;
|
||||||
|
private static Connection connection;
|
||||||
|
private static Admin admin;
|
||||||
|
|
||||||
private static final String TABLE_NAME = "emp1520";
|
private static final String TABLE_NAME = "emp1520";
|
||||||
private static final String CF_EMPNUM = "empnum";
|
private static final String CF_EMPNUM = "empnum";
|
||||||
private static final String CF_INFO = "info";
|
private static final String CF_INFO = "info";
|
||||||
@ -15,30 +19,49 @@ public class HBaseEmpManager {
|
|||||||
private static final String CF_PERFORMANCE = "performance";
|
private static final String CF_PERFORMANCE = "performance";
|
||||||
private static final String CF_TRAINING = "training";
|
private static final String CF_TRAINING = "training";
|
||||||
|
|
||||||
private static Connection connection;
|
|
||||||
private static Admin admin;
|
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
|
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
|
||||||
Configuration config = HBaseConfiguration.create();
|
init(); // 初始化 HBase 连接
|
||||||
connection = ConnectionFactory.createConnection(config);
|
createTable(); // 创建表
|
||||||
admin = connection.getAdmin();
|
insertData("emp.txt"); // 读取 emp.txt 插入数据
|
||||||
|
queryById(); // 查询 ID > 7500 的员工
|
||||||
|
queryByPerformance(); // 查询绩效评分 > 4 且入职时间 < 2022 年的员工
|
||||||
|
queryRecentPromotion(); // 查找最近晋升的员工
|
||||||
|
countTrainingParticipants(); // 统计培训课程的参与人数
|
||||||
|
close(); // 关闭连接
|
||||||
|
}
|
||||||
|
|
||||||
createTable();
|
// 初始化 HBase 连接
|
||||||
insertData("emp.txt");
|
private static void init() {
|
||||||
queryById();
|
configuration = HBaseConfiguration.create();
|
||||||
queryByPerformance();
|
try {
|
||||||
queryRecentPromotion();
|
connection = ConnectionFactory.createConnection(configuration);
|
||||||
countTrainingParticipants();
|
admin = connection.getAdmin();
|
||||||
|
System.out.println("Connected to HBase Successfully!");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
admin.close();
|
// 关闭 HBase 连接
|
||||||
connection.close();
|
private static void close() {
|
||||||
|
try {
|
||||||
|
if (admin != null) {
|
||||||
|
admin.close();
|
||||||
|
}
|
||||||
|
if (connection != null) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
System.out.println("HBase Connection Closed.");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建 HBase 表
|
// 创建 HBase 表
|
||||||
private static void createTable() throws IOException {
|
private static void createTable() throws IOException {
|
||||||
TableName tableName = TableName.valueOf(TABLE_NAME);
|
TableName tableName = TableName.valueOf(TABLE_NAME);
|
||||||
if (admin.tableExists(tableName)) {
|
if (admin.tableExists(tableName)) {
|
||||||
System.out.println("表 " + TABLE_NAME + " 已存在!");
|
System.out.println("Table emp1520 already exists.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +73,7 @@ public class HBaseEmpManager {
|
|||||||
tableDescriptor.addFamily(new HColumnDescriptor(CF_TRAINING));
|
tableDescriptor.addFamily(new HColumnDescriptor(CF_TRAINING));
|
||||||
|
|
||||||
admin.createTable(tableDescriptor);
|
admin.createTable(tableDescriptor);
|
||||||
System.out.println("表 " + TABLE_NAME + " 创建成功!");
|
System.out.println("Table emp1520 created successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取并插入数据
|
// 读取并插入数据
|
||||||
@ -85,7 +108,19 @@ public class HBaseEmpManager {
|
|||||||
|
|
||||||
br.close();
|
br.close();
|
||||||
table.close();
|
table.close();
|
||||||
System.out.println("数据插入完成!");
|
System.out.println("Data inserted successfully!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成 RowKey,避免 Region 热点
|
||||||
|
private static String generateRowKey(String empno) throws NoSuchAlgorithmException {
|
||||||
|
String prefix = empno.substring(0, 3);
|
||||||
|
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||||||
|
byte[] digest = md5.digest(prefix.getBytes());
|
||||||
|
StringBuilder hexString = new StringBuilder();
|
||||||
|
for (byte b : digest) {
|
||||||
|
hexString.append(String.format("%02x", b));
|
||||||
|
}
|
||||||
|
return hexString.substring(0, 4) + empno;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询 ID > 7500 的员工
|
// 查询 ID > 7500 的员工
|
||||||
@ -98,7 +133,7 @@ public class HBaseEmpManager {
|
|||||||
for (Result result : scanner) {
|
for (Result result : scanner) {
|
||||||
String empno = Bytes.toString(result.getValue(Bytes.toBytes(CF_EMPNUM), Bytes.toBytes("empno")));
|
String empno = Bytes.toString(result.getValue(Bytes.toBytes(CF_EMPNUM), Bytes.toBytes("empno")));
|
||||||
if (empno != null && Integer.parseInt(empno) > 7500) {
|
if (empno != null && Integer.parseInt(empno) > 7500) {
|
||||||
System.out.println("ID > 7500 员工: " + empno);
|
System.out.println("ID > 7500 Employee: " + empno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
table.close();
|
table.close();
|
||||||
@ -117,7 +152,7 @@ public class HBaseEmpManager {
|
|||||||
String rating = Bytes.toString(result.getValue(Bytes.toBytes(CF_PERFORMANCE), Bytes.toBytes("performance_rating")));
|
String rating = Bytes.toString(result.getValue(Bytes.toBytes(CF_PERFORMANCE), Bytes.toBytes("performance_rating")));
|
||||||
|
|
||||||
if (hiredate.compareTo("2022-01-01") < 0 && Integer.parseInt(rating) > 4) {
|
if (hiredate.compareTo("2022-01-01") < 0 && Integer.parseInt(rating) > 4) {
|
||||||
System.out.println("符合条件的员工: 入职时间 " + hiredate + ",绩效评分 " + rating);
|
System.out.println("Qualified Employee: Hire Date " + hiredate + ", Rating " + rating);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
table.close();
|
table.close();
|
||||||
@ -140,38 +175,7 @@ public class HBaseEmpManager {
|
|||||||
latestEmp = Bytes.toString(result.getRow());
|
latestEmp = Bytes.toString(result.getRow());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("最近晋升的员工: " + latestEmp + ",晋升日期: " + latestDate);
|
System.out.println("Most Recently Promoted Employee: " + latestEmp + ", Promotion Date: " + latestDate);
|
||||||
table.close();
|
table.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 统计每个培训课程的参与人数
|
|
||||||
private static void countTrainingParticipants() throws IOException {
|
|
||||||
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
|
|
||||||
Scan scan = new Scan();
|
|
||||||
scan.addColumn(Bytes.toBytes(CF_TRAINING), Bytes.toBytes("training_courses"));
|
|
||||||
|
|
||||||
ResultScanner scanner = table.getScanner(scan);
|
|
||||||
Map<String, Integer> countMap = new HashMap<>();
|
|
||||||
|
|
||||||
for (Result result : scanner) {
|
|
||||||
String courses = Bytes.toString(result.getValue(Bytes.toBytes(CF_TRAINING), Bytes.toBytes("training_courses")));
|
|
||||||
for (String course : courses.split(", ")) {
|
|
||||||
countMap.put(course, countMap.getOrDefault(course, 0) + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("培训课程统计: " + countMap);
|
|
||||||
table.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成 RowKey,避免 Region 热点
|
|
||||||
private static String generateRowKey(String empno) throws NoSuchAlgorithmException {
|
|
||||||
String prefix = empno.substring(0, 3);
|
|
||||||
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
||||||
byte[] digest = md5.digest(prefix.getBytes());
|
|
||||||
StringBuilder hexString = new StringBuilder();
|
|
||||||
for (byte b : digest) {
|
|
||||||
hexString.append(String.format("%02x", b));
|
|
||||||
}
|
|
||||||
return hexString.substring(0, 4) + empno;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user