Skip to content

Commit df5785b

Browse files
committed
feat: hbase 代码示例
1 parent c82e9d7 commit df5785b

File tree

6 files changed

+304
-90
lines changed

6 files changed

+304
-90
lines changed

codes/javadb/hbase/pom.xml

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<dependency>
2424
<groupId>org.apache.hbase</groupId>
2525
<artifactId>hbase-client</artifactId>
26+
<version>${hbase.version}</version>
2627
</dependency>
2728
<dependency>
2829
<groupId>org.apache.hadoop</groupId>
@@ -40,36 +41,11 @@
4041
<version>1.18.22</version>
4142
</dependency>
4243

43-
<!-- test begin -->
4444
<dependency>
45-
<groupId>junit</groupId>
46-
<artifactId>junit</artifactId>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<version>2.6.3</version>
48+
<scope>test</scope>
4749
</dependency>
48-
<!-- test end -->
4950
</dependencies>
50-
51-
<dependencyManagement>
52-
<dependencies>
53-
<dependency>
54-
<groupId>org.apache.hbase</groupId>
55-
<artifactId>hbase-client</artifactId>
56-
<version>${hbase.version}</version>
57-
</dependency>
58-
<!-- <dependency>-->
59-
<!-- <groupId>io.github.dunwu</groupId>-->
60-
<!-- <artifactId>dunwu-tool-core</artifactId>-->
61-
<!-- <version>${dunwu.version}</version>-->
62-
<!-- </dependency>-->
63-
64-
65-
<!-- test begin -->
66-
<dependency>
67-
<groupId>junit</groupId>
68-
<artifactId>junit</artifactId>
69-
<version>${junit.version}</version>
70-
<scope>test</scope>
71-
</dependency>
72-
<!-- test end -->
73-
</dependencies>
74-
</dependencyManagement>
7551
</project>

codes/javadb/hbase/src/main/java/io/github/dunwu/javadb/hbase/HBaseAdminHelper.java

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,14 @@ public Configuration getConfiguration() {
9191
* @param namespace 命名空间
9292
*/
9393
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();
94+
Admin admin = null;
95+
try {
96+
admin = getAdmin();
97+
NamespaceDescriptor nd = NamespaceDescriptor.create(namespace).build();
98+
admin.createNamespace(nd);
99+
} finally {
100+
recycle(admin);
101+
}
98102
}
99103

100104
/**
@@ -113,16 +117,33 @@ public void dropNamespace(String namespace) throws IOException {
113117
* @param force 是否强制删除
114118
*/
115119
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);
120+
Admin admin = null;
121+
try {
122+
admin = getAdmin();
123+
if (force) {
124+
TableName[] tableNames = admin.listTableNamesByNamespace(namespace);
125+
for (TableName name : tableNames) {
126+
admin.disableTable(name);
127+
admin.deleteTable(name);
128+
}
122129
}
130+
admin.deleteNamespace(namespace);
131+
} finally {
132+
recycle(admin);
133+
}
134+
}
135+
136+
/**
137+
* 获取所有命名空间
138+
*/
139+
public String[] listNamespaces() throws IOException {
140+
Admin admin = null;
141+
try {
142+
admin = getAdmin();
143+
return admin.listNamespaces();
144+
} finally {
145+
recycle(admin);
123146
}
124-
admin.deleteNamespace(namespace);
125-
admin.close();
126147
}
127148

128149
/**
@@ -212,6 +233,32 @@ public void enableTable(TableName tableName) throws IOException {
212233
admin.close();
213234
}
214235

236+
/**
237+
* 获取所有表
238+
*/
239+
public TableName[] listTableNames() throws IOException {
240+
Admin admin = null;
241+
try {
242+
admin = getAdmin();
243+
return admin.listTableNames();
244+
} finally {
245+
recycle(admin);
246+
}
247+
}
248+
249+
/**
250+
* 获取指定命名空间下的所有表
251+
*/
252+
public TableName[] listTableNamesByNamespace(String namespace) throws IOException {
253+
Admin admin = null;
254+
try {
255+
admin = getAdmin();
256+
return admin.listTableNamesByNamespace(namespace);
257+
} finally {
258+
recycle(admin);
259+
}
260+
}
261+
215262
/**
216263
* 获取 {@link Table} 实例
217264
*
@@ -231,4 +278,11 @@ public Admin getAdmin() throws IOException {
231278
return getConnection().getAdmin();
232279
}
233280

281+
private void recycle(Admin admin) {
282+
if (null == admin) {
283+
return;
284+
}
285+
IoUtil.close(admin);
286+
}
287+
234288
}

codes/javadb/hbase/src/main/java/io/github/dunwu/javadb/hbase/HBaseDemo.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

codes/javadb/hbase/src/main/java/io/github/dunwu/javadb/hbase/HBaseHelper.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,25 @@ protected HBaseHelper(Configuration configuration) throws IOException {
6161
this.connection = ConnectionFactory.createConnection(configuration);
6262
}
6363

64+
protected HBaseHelper(Connection connection) {
65+
this.configuration = connection.getConfiguration();
66+
this.connection = connection;
67+
}
68+
6469
public static synchronized HBaseHelper newInstance(Configuration configuration) throws IOException {
6570
if (configuration == null) {
6671
throw new IllegalArgumentException("configuration can not be null!");
6772
}
6873
return new HBaseHelper(configuration);
6974
}
7075

76+
public synchronized static HBaseHelper newInstance(Connection connection) throws IOException {
77+
if (connection == null) {
78+
throw new IllegalArgumentException("connection can not be null!");
79+
}
80+
return new HBaseHelper(connection);
81+
}
82+
7183
/**
7284
* 关闭内部持有的 HBase Connection 实例
7385
*/
@@ -555,8 +567,7 @@ public Map<String, Map<String, String>> scanFamilyMap(TableName tableName, Strin
555567
* @return 一级 Map 的 key 是 Row Key;二级 Map 的 key 是列,value 是列值
556568
*/
557569
public Map<String, Map<String, String>> scanFamilyMap(TableName tableName, String family,
558-
Collection<String> columns)
559-
throws Exception {
570+
Collection<String> columns) throws Exception {
560571
HBaseFamilyRequest request = new HBaseFamilyRequest();
561572
request.setFamily(family)
562573
.setColumns(columns)
@@ -597,8 +608,7 @@ public Map<String, Map<String, String>> scanFamilyMap(TableName tableName, Strin
597608
* @return 一级 Map 的 key 是 Row Key;二级 Map 的 key 是列,value 是列值
598609
*/
599610
public Map<String, Map<String, String>> scanFamilyMap(TableName tableName, String family,
600-
Collection<String> columns,
601-
Filter filter) throws Exception {
611+
Collection<String> columns, Filter filter) throws Exception {
602612
HBaseFamilyRequest request = new HBaseFamilyRequest();
603613
request.setFamily(family)
604614
.setColumns(columns)
@@ -646,8 +656,7 @@ public Map<String, Map<String, String>> scanFamilyMap(TableName tableName, Strin
646656
* @return 一级 Map 的 key 是 Row Key;二级 Map 的 key 是列,value 是列值
647657
*/
648658
public Map<String, Map<String, String>> scanFamilyMap(TableName tableName, String family,
649-
Collection<String> columns,
650-
long minStamp, long maxStamp, Filter filter) throws Exception {
659+
Collection<String> columns, long minStamp, long maxStamp, Filter filter) throws Exception {
651660
HBaseFamilyRequest request = new HBaseFamilyRequest();
652661
request.setFamily(family)
653662
.setColumns(columns)
@@ -829,8 +838,8 @@ public PageData<HBaseRowData> pageRowData(HBaseMultiFamilyRequest request) throw
829838
request.getPageNo(), request.getPageSize(), request.toScan());
830839
}
831840

832-
public PageData<HBaseRowData> pageRowData(TableName tableName,
833-
Map<String, Collection<String>> familyColumns, Integer pageNo, Integer pageSize, Scan scan) throws Exception {
841+
public PageData<HBaseRowData> pageRowData(TableName tableName, Map<String, Collection<String>> familyColumns,
842+
Integer pageNo, Integer pageSize, Scan scan) throws Exception {
834843

835844
Table table = getTable(tableName);
836845
Map<String, Map<String, Map<String, String>>> rowMap = new HashMap<>();
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.github.dunwu.javadb.hbase;
2+
3+
import cn.hutool.core.io.IoUtil;
4+
import cn.hutool.core.util.ArrayUtil;
5+
import org.apache.hadoop.conf.Configuration;
6+
import org.apache.hadoop.hbase.HBaseConfiguration;
7+
import org.apache.hadoop.hbase.TableName;
8+
import org.apache.hadoop.hbase.client.Admin;
9+
import org.junit.jupiter.api.AfterAll;
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.io.IOException;
15+
import java.util.stream.Stream;
16+
17+
/**
18+
* HBase {@link Admin} API 测试例
19+
* <p>
20+
* HBaseAdminHelper 是针对 {@link Admin} 常用 API 的封装工具类
21+
*/
22+
public class HBaseAdminHelperTests {
23+
24+
private static HBaseAdminHelper hbaseAdminHelper = null;
25+
26+
@BeforeAll
27+
public static void init() throws IOException {
28+
Configuration conf = HBaseConfiguration.create();
29+
conf.set("hbase.zookeeper.quorum", "localhost");
30+
conf.set("hbase.zookeeper.port", "2181");
31+
hbaseAdminHelper = HBaseAdminHelper.newInstance(conf);
32+
}
33+
34+
@AfterAll
35+
public static void destroy() {
36+
IoUtil.close(hbaseAdminHelper);
37+
}
38+
39+
@Test
40+
@DisplayName("创建、删除、查看命名空间")
41+
public void testNamespace() throws IOException {
42+
// 创建命名空间
43+
hbaseAdminHelper.createNamespace("temp");
44+
dumpNamespaces();
45+
// 删除命名空间
46+
hbaseAdminHelper.dropNamespace("temp", true);
47+
dumpNamespaces();
48+
}
49+
50+
private void dumpNamespaces() throws IOException {
51+
String[] namespaces = hbaseAdminHelper.listNamespaces();
52+
System.out.println("命名空间:");
53+
if (ArrayUtil.isNotEmpty(namespaces)) {
54+
Stream.of(namespaces).forEach(System.out::println);
55+
}
56+
}
57+
58+
@Test
59+
@DisplayName("创建、删除、启用、禁用查看表")
60+
public void testTable() throws IOException {
61+
// 创建命名空间
62+
hbaseAdminHelper.createNamespace("temp");
63+
// 创建名为 test 的表,并含有两个列族 d 和 b
64+
hbaseAdminHelper.createTable(TableName.valueOf("temp:test"), "d", "b");
65+
// 查看表
66+
dumpTablesInNamespace("temp");
67+
// 禁用表
68+
hbaseAdminHelper.disableTable(TableName.valueOf("temp:test"));
69+
// 启用表
70+
hbaseAdminHelper.enableTable(TableName.valueOf("temp:test"));
71+
// 删除表
72+
hbaseAdminHelper.dropTable(TableName.valueOf("temp:test"));
73+
// 查看表
74+
dumpTablesInNamespace("temp");
75+
// 删除命名空间
76+
hbaseAdminHelper.dropNamespace("temp", true);
77+
}
78+
79+
private void dumpTablesInNamespace(String namespace) throws IOException {
80+
TableName[] tableNames = hbaseAdminHelper.listTableNamesByNamespace(namespace);
81+
System.out.println("表:");
82+
if (ArrayUtil.isNotEmpty(tableNames)) {
83+
Stream.of(tableNames).forEach(System.out::println);
84+
}
85+
}
86+
87+
}

0 commit comments

Comments
 (0)