From 2f36bb075f945ce82031148a25e49d422f63cab3 Mon Sep 17 00:00:00 2001 From: fly6516 Date: Wed, 26 Mar 2025 09:38:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor(hbase):=20=E9=87=8D=E6=9E=84=20HBase?= =?UTF-8?q?=20=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=BF=9E=E6=8E=A5=E6=96=B9?= =?UTF-8?q?=E5=BC=8F-=20=E6=9B=BF=E6=8D=A2=20HBaseAdmin=E4=B8=BA=20Admin?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=B9=B6=E4=BD=BF=E7=94=A8=20try?= =?UTF-8?q?-with-resources=20=E8=87=AA=E5=8A=A8=E7=AE=A1=E7=90=86=E8=B5=84?= =?UTF-8?q?=E6=BA=90=20-=20=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD=EF=BC=8C=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81=20-=20=E6=96=B0=E5=A2=9E=20MyConnec?= =?UTF-8?q?t=20=E7=B1=BB=EF=BC=8C=E5=AE=9E=E7=8E=B0=20HBase=20=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E5=92=8C=E5=85=B3?= =?UTF-8?q?=E9=97=AD=20-=20=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BD=BF=E7=94=A8=20Binary?= =?UTF-8?q?Comparator=20=E6=9B=BF=E4=BB=A3=20SubstringComparator=20-?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A9=BA=E5=80=BC=E6=A3=80=E6=9F=A5=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E9=AB=98=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=E7=9A=84?= =?UTF-8?q?=E5=81=A5=E5=A3=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/EmpHBaseClient.java | 30 ++++++++++++------------ src/main/java/MyConnect.java | 39 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/main/java/MyConnect.java diff --git a/src/main/java/EmpHBaseClient.java b/src/main/java/EmpHBaseClient.java index b2b0ffc..79d215c 100644 --- a/src/main/java/EmpHBaseClient.java +++ b/src/main/java/EmpHBaseClient.java @@ -16,19 +16,19 @@ public class EmpHBaseClient { // 创建职工表结构 public void createEmpTable() throws IOException { - HBaseAdmin admin = new HBaseAdmin(conf); - if (admin.tableExists("emp1520")) { - System.out.println("Table already exists"); - return; + 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); } - 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); - admin.close(); } // 新增方法:生成RowKey前缀(MD5处理) @@ -51,7 +51,7 @@ public class EmpHBaseClient { // info列族:基本信息存储 put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("ename"), Bytes.toBytes(ename)); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("job"), Bytes.toBytes(job)); - put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("mgr"), Bytes.toBytes(mgr == null ? "" : mgr)); + put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("mgr"), Bytes.toBytes(mgr)); // 删除原条件判断 put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("hiredate"), Bytes.toBytes(hiredate)); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("deptno"), Bytes.toBytes(deptno)); // 新增部门号字段存储 // salary列族:薪资相关字段存储 @@ -99,7 +99,7 @@ public class EmpHBaseClient { Bytes.toBytes("empnum"), Bytes.toBytes("empno"), CompareFilter.CompareOp.GREATER_OR_EQUAL, - new SubstringComparator("7500") + new BinaryComparator(Bytes.toBytes("7500")) ); filter.setFilterIfMissing(true); scan.setFilter(filter); @@ -152,7 +152,7 @@ public class EmpHBaseClient { String promotionDateStr = Bytes.toString( result.getValue(Bytes.toBytes("performance"), Bytes.toBytes("promotion_date")) ); - if (promotionDateStr != null) { + if (promotionDateStr != null && !promotionDateStr.isEmpty()) { // 新增空值检查 records.add(new PromotionRecord( Bytes.toString(result.getRow()), LocalDate.parse(promotionDateStr) diff --git a/src/main/java/MyConnect.java b/src/main/java/MyConnect.java new file mode 100644 index 0000000..9edaa07 --- /dev/null +++ b/src/main/java/MyConnect.java @@ -0,0 +1,39 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.client.*; +import java.io.IOException; +public class MyConnect{ + public static Configuration configuration; + public static Connection connection; + public static Admin admin; + public static void main(String[] args)throws IOException{ + init(); + close(); + } + //建立连接 + public static void init(){ + //根据 hbase-site.xml文件初始化Configuration 对象 + configuration = HBaseConfiguration.create(); + try{ + //根据 Configuration对象初始化Connection 对象 + connection = ConnectionFactory.createConnection(configuration); + //获取Admin 对象实例 + admin = connection.getAdmin(); + }catch (IOException e){ + e.printStackTrace(); + } + System.out.println("Connect to HBase Successfully!"); + } + //关闭连接 + public static void close(){ + try{ + if(admin != null){ + admin.close(); + } + if(null != connection){ + connection.close(); + } + }catch (IOException e){ + e.printStackTrace(); + } + } +} \ No newline at end of file