Skip to content

Commit 14b594b

Browse files
committed
feat: 更新 hbase 示例
1 parent 7e344cd commit 14b594b

File tree

11 files changed

+278
-63
lines changed

11 files changed

+278
-63
lines changed

codes/javadb/hbase/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
<artifactId>hutool-all</artifactId>
3636
<version>5.8.18</version>
3737
</dependency>
38+
<dependency>
39+
<groupId>com.fasterxml.jackson.core</groupId>
40+
<artifactId>jackson-annotations</artifactId>
41+
<version>2.13.4</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.alibaba</groupId>
45+
<artifactId>fastjson</artifactId>
46+
<version>1.2.83</version>
47+
</dependency>
3848
<dependency>
3949
<groupId>org.projectlombok</groupId>
4050
<artifactId>lombok</artifactId>

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Connection getClient() {
3636

3737
@Override
3838
public String getNamespace() {
39-
return "default";
39+
return "test";
4040
}
4141

4242
@Override
@@ -57,25 +57,25 @@ public String getFamily() {
5757
}
5858

5959
@Override
60-
public T pojoById(String id) {
61-
if (StrUtil.isBlank(id)) {
60+
public T pojoByRowKey(String rowKey) {
61+
if (StrUtil.isBlank(rowKey)) {
6262
return null;
6363
}
6464
try {
65-
return hbaseTemplate.getEntity(getFullTableName(), id, getFamily(), getEntityClass());
65+
return hbaseTemplate.getEntity(getFullTableName(), rowKey, getFamily(), getEntityClass());
6666
} catch (IOException e) {
6767
log.error("【Hbase】pojoById 异常", e);
6868
return null;
6969
}
7070
}
7171

7272
@Override
73-
public List<T> pojoListByIds(Collection<String> ids) {
74-
if (CollectionUtil.isEmpty(ids)) {
73+
public List<T> pojoListByRowKeys(Collection<String> rowKeys) {
74+
if (CollectionUtil.isEmpty(rowKeys)) {
7575
return null;
7676
}
7777
try {
78-
return hbaseTemplate.getEntityList(getFullTableName(), ids.toArray(new String[0]),
78+
return hbaseTemplate.getEntityList(getFullTableName(), rowKeys.toArray(new String[0]),
7979
getFamily(), getEntityClass());
8080
} catch (IOException e) {
8181
log.error("【Hbase】getEntityList 异常", e);
@@ -84,10 +84,10 @@ public List<T> pojoListByIds(Collection<String> ids) {
8484
}
8585

8686
@Override
87-
public List<T> scroll(String scrollId, int size) {
87+
public List<T> scroll(String scrollRowKey, int size) {
8888
try {
8989
ScrollData<T> scrollData =
90-
hbaseTemplate.getEntityScroll(getFullTableName(), getFamily(), scrollId, size, getEntityClass());
90+
hbaseTemplate.getEntityScroll(getFullTableName(), getFamily(), scrollRowKey, size, getEntityClass());
9191
if (scrollData == null || CollectionUtil.isEmpty(scrollData.getContent())) {
9292
return new ArrayList<>();
9393
}
@@ -101,7 +101,8 @@ public List<T> scroll(String scrollId, int size) {
101101
@Override
102102
public T save(T entity) {
103103
try {
104-
hbaseTemplate.put(getFullTableName(), entity.getId(), getFamily(), entity);
104+
String rowKey = entity.getRowKey();
105+
hbaseTemplate.put(getFullTableName(), rowKey, getFamily(), entity);
105106
return entity;
106107
} catch (IOException e) {
107108
log.error("【Hbase】put 异常", e);
@@ -121,12 +122,12 @@ public boolean batchSave(Collection<T> list) {
121122
}
122123

123124
@Override
124-
public boolean deleteById(String id) {
125-
if (StrUtil.isBlank(id)) {
125+
public boolean delete(String rowKey) {
126+
if (StrUtil.isBlank(rowKey)) {
126127
return true;
127128
}
128129
try {
129-
hbaseTemplate.delete(getFullTableName(), id);
130+
hbaseTemplate.delete(getFullTableName(), rowKey);
130131
return true;
131132
} catch (IOException e) {
132133
log.error("【Hbase】delete 异常", e);
@@ -135,12 +136,12 @@ public boolean deleteById(String id) {
135136
}
136137

137138
@Override
138-
public boolean batchDeleteById(Collection<String> ids) {
139-
if (CollectionUtil.isEmpty(ids)) {
139+
public boolean batchDelete(Collection<String> rowKeys) {
140+
if (CollectionUtil.isEmpty(rowKeys)) {
140141
return true;
141142
}
142143
try {
143-
hbaseTemplate.batchDelete(getFullTableName(), ids.toArray(new String[0]));
144+
hbaseTemplate.batchDelete(getFullTableName(), rowKeys.toArray(new String[0]));
144145
return true;
145146
} catch (IOException | InterruptedException e) {
146147
log.error("【Hbase】batchDelete 异常", e);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.apache.hadoop.hbase.client.Table;
1313
import org.apache.hadoop.hbase.client.TableDescriptor;
1414
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
15+
import org.apache.hadoop.hbase.security.User;
16+
import org.apache.hadoop.security.UserGroupInformation;
1517

1618
import java.io.Closeable;
1719
import java.io.IOException;
@@ -32,10 +34,10 @@ public class HbaseAdmin implements Closeable {
3234
protected HbaseAdmin(Configuration configuration) throws IOException {
3335
this.configuration = configuration;
3436
// 无需鉴权连接
35-
this.connection = ConnectionFactory.createConnection(configuration);
37+
// this.connection = ConnectionFactory.createConnection(configuration);
3638
// 鉴权连接
37-
// this.connection = ConnectionFactory.createConnection(configuration, null,
38-
// new User.SecureHadoopUser(UserGroupInformation.createRemoteUser("test")));
39+
this.connection = ConnectionFactory.createConnection(configuration, null,
40+
new User.SecureHadoopUser(UserGroupInformation.createRemoteUser("test")));
3941
}
4042

4143
protected HbaseAdmin(Connection connection) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static HbaseAdmin newHbaseAdmin() throws IOException {
2323

2424
public static Configuration newHbaseConfiguration() {
2525
Configuration configuration = HBaseConfiguration.create();
26-
configuration.set("hbase.zookeeper.quorum", "10.101.129.74,10.101.129.76,10.101.129.77");
26+
configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
2727
configuration.set("hbase.zookeeper.property.clientPort", "2181");
2828
configuration.set("hbase.rootdir", "/hbase");
2929
configuration.set("hbase.meta.replicas.use", "true");

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ public interface HbaseMapper<T extends BaseHbaseEntity> {
5050
* @param id 即 Hbase rowkey
5151
* @return /
5252
*/
53-
T pojoById(String id);
53+
T pojoByRowKey(String id);
5454

5555
/**
5656
* 根据 ID 列表批量查数据
5757
*
5858
* @param ids 即 Hbase rowkey
5959
* @return /
6060
*/
61-
List<T> pojoListByIds(Collection<String> ids);
61+
List<T> pojoListByRowKeys(Collection<String> ids);
6262

6363
/**
6464
* 根据 ID 滚动分页查询
@@ -91,14 +91,14 @@ public interface HbaseMapper<T extends BaseHbaseEntity> {
9191
* @param id 即 Hbase rowkey
9292
* @return /
9393
*/
94-
boolean deleteById(String id);
94+
boolean delete(String id);
9595

9696
/**
9797
* 根据 ID 列表批量删除记录
9898
*
9999
* @param ids 即 Hbase rowkey
100100
* @return /
101101
*/
102-
boolean batchDeleteById(Collection<String> ids);
102+
boolean batchDelete(Collection<String> ids);
103103

104104
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import org.apache.hadoop.hbase.client.Row;
3434
import org.apache.hadoop.hbase.client.Scan;
3535
import org.apache.hadoop.hbase.client.Table;
36+
import org.apache.hadoop.hbase.security.User;
3637
import org.apache.hadoop.hbase.util.Bytes;
38+
import org.apache.hadoop.security.UserGroupInformation;
3739

3840
import java.io.Closeable;
3941
import java.io.IOException;
@@ -65,10 +67,10 @@ public class HbaseTemplate implements Closeable {
6567
protected HbaseTemplate(Configuration configuration) throws IOException {
6668
this.configuration = configuration;
6769
// 无需鉴权连接
68-
this.connection = ConnectionFactory.createConnection(configuration);
70+
// this.connection = ConnectionFactory.createConnection(configuration);
6971
// 鉴权连接
70-
// this.connection = ConnectionFactory.createConnection(configuration, null,
71-
// new User.SecureHadoopUser(UserGroupInformation.createRemoteUser("test")));
72+
this.connection = ConnectionFactory.createConnection(configuration, null,
73+
new User.SecureHadoopUser(UserGroupInformation.createRemoteUser("test")));
7274
}
7375

7476
protected HbaseTemplate(Connection connection) {
@@ -198,6 +200,10 @@ public void put(String tableName, String row, Long timestamp, Map<String, Map<St
198200
put(tableName, put);
199201
}
200202

203+
public <T extends BaseHbaseEntity> void put(String tableName, String family, T entity) throws IOException {
204+
put(tableName, entity.getRowKey(), family, entity);
205+
}
206+
201207
public void batchPut(String tableName, Collection<Put> list) throws IOException, InterruptedException {
202208
batch(tableName, list);
203209
}
@@ -273,11 +279,15 @@ public static Put newPut(String row, Long timestamp, Map<String, Map<String, Obj
273279
return put;
274280
}
275281

276-
private static <T extends BaseHbaseEntity> List<Put> newPutList(String family, Collection<T> list) {
282+
private static <T extends BaseHbaseEntity> List<Put> newPutList(String family, Collection<T> list)
283+
throws IOException {
277284
long timestamp = System.currentTimeMillis();
278-
return list.stream()
279-
.map(entity -> newPut(entity.getId(), timestamp, family, entity))
280-
.collect(Collectors.toList());
285+
List<Put> puts = new ArrayList<>();
286+
for (T entity : list) {
287+
Put put = newPut(entity.getRowKey(), timestamp, family, entity);
288+
puts.add(put);
289+
}
290+
return puts;
281291
}
282292

283293
// =====================================================================================
@@ -410,8 +420,10 @@ public <T> List<T> getEntityList(String tableName, String[] rows, String family,
410420
for (Result result : results) {
411421
Map<String, ColumnDo> columnMap =
412422
getColumnsFromResult(result, tableName, family, CollectionUtil.newArrayList(columns));
413-
T entity = toEntity(columnMap, clazz);
414-
list.add(entity);
423+
if (MapUtil.isNotEmpty(columnMap)) {
424+
T entity = toEntity(columnMap, clazz);
425+
list.add(entity);
426+
}
415427
}
416428
return list;
417429
}
@@ -911,7 +923,9 @@ private static Map<String, ColumnDo> getColumnsFromResult(Result result, String
911923
Map<String, ColumnDo> columnMap = new HashMap<>(columns.size());
912924
for (String column : columns) {
913925
ColumnDo columnDo = getColumnFromResult(result, tableName, family, column);
914-
columnMap.put(column, columnDo);
926+
if (columnDo != null) {
927+
columnMap.put(column, columnDo);
928+
}
915929
}
916930
return columnMap;
917931
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.github.dunwu.javadb.hbase.annotation;
2+
3+
import io.github.dunwu.javadb.hbase.constant.RowType;
4+
5+
import java.lang.annotation.Documented;
6+
import java.lang.annotation.ElementType;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* 表主键标识
13+
*
14+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
15+
* @date 2023-11-17
16+
*/
17+
@Documented
18+
@Retention(RetentionPolicy.RUNTIME)
19+
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
20+
public @interface RowKeyRule {
21+
22+
/**
23+
* 字段名(该值可无)
24+
*/
25+
String value() default "";
26+
27+
/**
28+
* 主键类型 {@link RowType}
29+
*/
30+
RowType type() default RowType.ORIGIN_ID;
31+
32+
/**
33+
* 原 ID 长度,type 为 {@link RowType#ORIGIN_ID} 或 {@link RowType#BUCKET} 时必填
34+
*/
35+
int length() default 0;
36+
37+
/**
38+
* 分桶数,type 为 {@link RowType#BUCKET} 时,才需要且必须指定
39+
*/
40+
int bucket() default 0;
41+
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.dunwu.javadb.hbase.constant;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* 生成ID类型枚举类
7+
*
8+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
9+
* @date 2023-11-17
10+
*/
11+
@Getter
12+
public enum RowType {
13+
14+
/**
15+
* 原 ID
16+
*/
17+
ORIGIN_ID(1),
18+
19+
/**
20+
* 以 10 位的时间戳(秒级)作为 ID
21+
* <p>
22+
* 特点:数据存储保证单调递增,适用于 scan 为主,且数据量不大(100w以内),读频率不高的业务场景。
23+
*/
24+
TIMESTAMP(2),
25+
26+
/**
27+
* UUID作为主键,适合数据量较大,且以 get 为主的场景(尽量保证数据存储离散)
28+
*/
29+
UUID(3),
30+
31+
/**
32+
* ID = bucket(2/3) + timestamp(10) + bizId,适合数据量较大,且需要大量 scan 的场景
33+
* <p>
34+
* 注:如果选择此 ID 类型,必须在 @TableId 中指定分桶数
35+
*/
36+
BUCKET(4);
37+
38+
private final int key;
39+
40+
RowType(int key) {
41+
this.key = key;
42+
}
43+
}

0 commit comments

Comments
 (0)