|
| 1 | +package io.github.dunwu.javadb.hbase; |
| 2 | + |
| 3 | +import cn.hutool.core.io.IoUtil; |
| 4 | +import org.apache.hadoop.conf.Configuration; |
| 5 | +import org.apache.hadoop.hbase.NamespaceDescriptor; |
| 6 | +import org.apache.hadoop.hbase.TableName; |
| 7 | +import org.apache.hadoop.hbase.client.Admin; |
| 8 | +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; |
| 9 | +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; |
| 10 | +import org.apache.hadoop.hbase.client.Connection; |
| 11 | +import org.apache.hadoop.hbase.client.ConnectionFactory; |
| 12 | +import org.apache.hadoop.hbase.client.Table; |
| 13 | +import org.apache.hadoop.hbase.client.TableDescriptor; |
| 14 | +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; |
| 15 | + |
| 16 | +import java.io.Closeable; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +/** |
| 22 | + * HBase 管理工具类 |
| 23 | + * |
| 24 | + * @author <a href="mailto:forbreak@163.com">Zhang Peng</a> |
| 25 | + * @date 2023-03-27 |
| 26 | + */ |
| 27 | +public class HBaseAdminHelper implements Closeable { |
| 28 | + |
| 29 | + private final Connection connection; |
| 30 | + private final Configuration configuration; |
| 31 | + |
| 32 | + protected HBaseAdminHelper(Configuration configuration) throws IOException { |
| 33 | + this.configuration = configuration; |
| 34 | + this.connection = ConnectionFactory.createConnection(configuration); |
| 35 | + } |
| 36 | + |
| 37 | + protected HBaseAdminHelper(Connection connection) { |
| 38 | + this.configuration = connection.getConfiguration(); |
| 39 | + this.connection = connection; |
| 40 | + } |
| 41 | + |
| 42 | + public synchronized static HBaseAdminHelper newInstance(Configuration configuration) throws IOException { |
| 43 | + if (configuration == null) { |
| 44 | + throw new IllegalArgumentException("configuration can not be null!"); |
| 45 | + } |
| 46 | + return new HBaseAdminHelper(configuration); |
| 47 | + } |
| 48 | + |
| 49 | + public synchronized static HBaseAdminHelper newInstance(Connection connection) throws IOException { |
| 50 | + if (connection == null) { |
| 51 | + throw new IllegalArgumentException("connection can not be null!"); |
| 52 | + } |
| 53 | + return new HBaseAdminHelper(connection); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * 关闭内部持有的 HBase Connection 实例 |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public synchronized void close() { |
| 61 | + if (null == connection || connection.isClosed()) { |
| 62 | + return; |
| 63 | + } |
| 64 | + IoUtil.close(connection); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 获取 HBase 连接实例 |
| 69 | + * |
| 70 | + * @return / |
| 71 | + */ |
| 72 | + public Connection getConnection() { |
| 73 | + if (null == connection) { |
| 74 | + throw new RuntimeException("HBase connection init failed..."); |
| 75 | + } |
| 76 | + return connection; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 获取 HBase 配置 |
| 81 | + * |
| 82 | + * @return / |
| 83 | + */ |
| 84 | + public Configuration getConfiguration() { |
| 85 | + return configuration; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * 创建命名空间 |
| 90 | + * |
| 91 | + * @param namespace 命名空间 |
| 92 | + */ |
| 93 | + public void createNamespace(String namespace) throws IOException { |
| 94 | + NamespaceDescriptor nd = NamespaceDescriptor.create(namespace).build(); |
| 95 | + Admin admin = getAdmin(); |
| 96 | + admin.createNamespace(nd); |
| 97 | + admin.close(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * 删除命名空间 |
| 102 | + * |
| 103 | + * @param namespace 命名空间 |
| 104 | + */ |
| 105 | + public void dropNamespace(String namespace) throws IOException { |
| 106 | + dropNamespace(namespace, false); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * 删除命名空间 |
| 111 | + * |
| 112 | + * @param namespace 命名空间 |
| 113 | + * @param force 是否强制删除 |
| 114 | + */ |
| 115 | + public void dropNamespace(String namespace, boolean force) throws IOException { |
| 116 | + Admin admin = getAdmin(); |
| 117 | + if (force) { |
| 118 | + TableName[] tableNames = getAdmin().listTableNamesByNamespace(namespace); |
| 119 | + for (TableName name : tableNames) { |
| 120 | + admin.disableTable(name); |
| 121 | + admin.deleteTable(name); |
| 122 | + } |
| 123 | + } |
| 124 | + admin.deleteNamespace(namespace); |
| 125 | + admin.close(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * 指定表是否存在 |
| 130 | + * |
| 131 | + * @param tableName 表名 |
| 132 | + */ |
| 133 | + public boolean existsTable(TableName tableName) throws IOException { |
| 134 | + Admin admin = getAdmin(); |
| 135 | + boolean result = admin.tableExists(tableName); |
| 136 | + admin.close(); |
| 137 | + return result; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * 创建表 |
| 142 | + * |
| 143 | + * @param tableName 表名 |
| 144 | + * @param families 列族 |
| 145 | + */ |
| 146 | + public void createTable(TableName tableName, String... families) throws IOException { |
| 147 | + createTable(tableName, null, families); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * 创建表 |
| 152 | + * |
| 153 | + * @param tableName 表名 |
| 154 | + * @param splitKeys 表初始区域的拆分关键字 |
| 155 | + * @param families 列族 |
| 156 | + */ |
| 157 | + public void createTable(TableName tableName, byte[][] splitKeys, String... families) throws IOException { |
| 158 | + |
| 159 | + List<ColumnFamilyDescriptor> columnFamilyDescriptorList = new ArrayList<>(); |
| 160 | + TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); |
| 161 | + for (String cf : families) { |
| 162 | + ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.of(cf); |
| 163 | + columnFamilyDescriptorList.add(columnFamilyDescriptor); |
| 164 | + } |
| 165 | + builder.setColumnFamilies(columnFamilyDescriptorList); |
| 166 | + |
| 167 | + TableDescriptor td = builder.build(); |
| 168 | + Admin admin = getAdmin(); |
| 169 | + if (splitKeys != null) { |
| 170 | + admin.createTable(td, splitKeys); |
| 171 | + } else { |
| 172 | + admin.createTable(td); |
| 173 | + } |
| 174 | + admin.close(); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * 删除表 |
| 179 | + * |
| 180 | + * @param tableName 表名 |
| 181 | + */ |
| 182 | + public void dropTable(TableName tableName) throws IOException { |
| 183 | + if (existsTable(tableName)) { |
| 184 | + Admin admin = getAdmin(); |
| 185 | + if (admin.isTableEnabled(tableName)) { |
| 186 | + disableTable(tableName); |
| 187 | + } |
| 188 | + admin.deleteTable(tableName); |
| 189 | + admin.close(); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * 禁用表 |
| 195 | + * |
| 196 | + * @param tableName 表名 |
| 197 | + */ |
| 198 | + public void disableTable(TableName tableName) throws IOException { |
| 199 | + Admin admin = getAdmin(); |
| 200 | + admin.disableTable(tableName); |
| 201 | + admin.close(); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * 启用表 |
| 206 | + * |
| 207 | + * @param tableName 表名 |
| 208 | + */ |
| 209 | + public void enableTable(TableName tableName) throws IOException { |
| 210 | + Admin admin = getAdmin(); |
| 211 | + admin.enableTable(tableName); |
| 212 | + admin.close(); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * 获取 {@link Table} 实例 |
| 217 | + * |
| 218 | + * @param tableName 表名 |
| 219 | + * @return / |
| 220 | + */ |
| 221 | + public Table getTable(TableName tableName) throws IOException { |
| 222 | + return getConnection().getTable(tableName); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * 获取 {@link Admin} 实例 |
| 227 | + * |
| 228 | + * @return / |
| 229 | + */ |
| 230 | + public Admin getAdmin() throws IOException { |
| 231 | + return getConnection().getAdmin(); |
| 232 | + } |
| 233 | + |
| 234 | +} |
0 commit comments