Skip to content

Commit c82e9d7

Browse files
committed
feat: HBase 、ES 示例更新
1 parent aa66b1f commit c82e9d7

File tree

18 files changed

+1574
-749
lines changed

18 files changed

+1574
-749
lines changed

codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/util/ElasticsearchUtil.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.github.dunwu.javadb.elasticsearch.util;
22

3-
import cn.hutool.core.bean.BeanUtil;
4-
import cn.hutool.core.bean.copier.CopyOptions;
53
import cn.hutool.core.collection.CollectionUtil;
64
import cn.hutool.core.util.ArrayUtil;
75
import cn.hutool.core.util.StrUtil;
@@ -35,8 +33,6 @@
3533
import org.elasticsearch.common.unit.ByteSizeUnit;
3634
import org.elasticsearch.common.unit.ByteSizeValue;
3735
import org.elasticsearch.common.unit.TimeValue;
38-
import org.elasticsearch.common.xcontent.XContentBuilder;
39-
import org.elasticsearch.common.xcontent.XContentFactory;
4036
import org.elasticsearch.index.query.QueryBuilder;
4137
import org.elasticsearch.index.query.QueryBuilders;
4238
import org.elasticsearch.rest.RestStatus;
@@ -47,7 +43,6 @@
4743
import java.util.ArrayList;
4844
import java.util.Collection;
4945
import java.util.Collections;
50-
import java.util.HashMap;
5146
import java.util.List;
5247
import java.util.Map;
5348
import java.util.concurrent.TimeUnit;
@@ -170,18 +165,8 @@ public static RestClientBuilder builder(HttpHost[] httpHosts) {
170165

171166
public static <T extends EsEntity> String insert(RestHighLevelClient client, String index, String type, T entity)
172167
throws IOException {
173-
Map<String, Object> map = new HashMap<>();
174-
BeanUtil.beanToMap(entity, map, CopyOptions.create().ignoreError());
175-
XContentBuilder builder = XContentFactory.jsonBuilder();
176-
builder.startObject();
177-
for (Map.Entry<String, Object> entry : map.entrySet()) {
178-
String key = entry.getKey();
179-
Object value = entry.getValue();
180-
builder.field(key, value);
181-
}
182-
builder.endObject();
183-
184-
IndexRequest request = new IndexRequest(index, type).source(builder);
168+
Map<String, Object> map = toMap(entity);
169+
IndexRequest request = new IndexRequest(index, type).source(map);
185170
if (entity.getDocId() != null) {
186171
request.id(entity.getDocId());
187172
}

codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/util/JsonUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* JSON 工具类
2020
*
21-
* @author Zhang Peng
21+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
2222
* @date 2023-06-29
2323
*/
2424
@Slf4j

codes/javadb/hbase/pom.xml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4-
xmlns="http://maven.apache.org/POM/4.0.0">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0">
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>io.github.dunwu</groupId>
77
<artifactId>javadb-hbase</artifactId>
@@ -14,7 +14,7 @@
1414
<maven.compiler.source>${java.version}</maven.compiler.source>
1515
<maven.compiler.target>${java.version}</maven.compiler.target>
1616

17-
<hbase.version>1.3.1</hbase.version>
17+
<hbase.version>2.4.15</hbase.version>
1818
<junit.version>4.13.1</junit.version>
1919
<dunwu.version>0.5.7</dunwu.version>
2020
</properties>
@@ -25,8 +25,19 @@
2525
<artifactId>hbase-client</artifactId>
2626
</dependency>
2727
<dependency>
28-
<groupId>io.github.dunwu</groupId>
29-
<artifactId>dunwu-tool-core</artifactId>
28+
<groupId>org.apache.hadoop</groupId>
29+
<artifactId>hadoop-hdfs</artifactId>
30+
<version>2.10.2</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>cn.hutool</groupId>
34+
<artifactId>hutool-all</artifactId>
35+
<version>5.8.18</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<version>1.18.22</version>
3041
</dependency>
3142

3243
<!-- test begin -->
@@ -44,11 +55,12 @@
4455
<artifactId>hbase-client</artifactId>
4556
<version>${hbase.version}</version>
4657
</dependency>
47-
<dependency>
48-
<groupId>io.github.dunwu</groupId>
49-
<artifactId>dunwu-tool-core</artifactId>
50-
<version>${dunwu.version}</version>
51-
</dependency>
58+
<!-- <dependency>-->
59+
<!-- <groupId>io.github.dunwu</groupId>-->
60+
<!-- <artifactId>dunwu-tool-core</artifactId>-->
61+
<!-- <version>${dunwu.version}</version>-->
62+
<!-- </dependency>-->
63+
5264

5365
<!-- test begin -->
5466
<dependency>
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
}

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

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

0 commit comments

Comments
 (0)