Skip to content

Commit 6c3031b

Browse files
committed
feat: 更新 hbase 示例
1 parent 81a350a commit 6c3031b

File tree

17 files changed

+1153
-214
lines changed

17 files changed

+1153
-214
lines changed

codes/javadb/hbase/pom.xml

Lines changed: 53 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,60 @@
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">
5-
<modelVersion>4.0.0</modelVersion>
6-
<groupId>io.github.dunwu</groupId>
7-
<artifactId>javadb-hbase</artifactId>
8-
<version>1.0.0</version>
9-
<packaging>jar</packaging>
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">
5+
<modelVersion>4.0.0</modelVersion>
106

11-
<properties>
12-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<java.version>1.8</java.version>
14-
<maven.compiler.source>${java.version}</maven.compiler.source>
15-
<maven.compiler.target>${java.version}</maven.compiler.target>
7+
<parent>
8+
<groupId>io.github.dunwu</groupId>
9+
<artifactId>javadb</artifactId>
10+
<version>1.0.0</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
1613

17-
<hbase.version>2.4.15</hbase.version>
18-
<junit.version>4.13.1</junit.version>
19-
<dunwu.version>0.5.7</dunwu.version>
20-
</properties>
14+
<artifactId>javadb-hbase</artifactId>
15+
<packaging>jar</packaging>
2116

22-
<dependencies>
23-
<dependency>
24-
<groupId>org.apache.hbase</groupId>
25-
<artifactId>hbase-client</artifactId>
26-
<version>${hbase.version}</version>
27-
</dependency>
28-
<dependency>
29-
<groupId>org.apache.hadoop</groupId>
30-
<artifactId>hadoop-auth</artifactId>
31-
<version>2.10.2</version>
32-
</dependency>
33-
<dependency>
34-
<groupId>cn.hutool</groupId>
35-
<artifactId>hutool-all</artifactId>
36-
<version>5.8.18</version>
37-
</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>
48-
<dependency>
49-
<groupId>org.projectlombok</groupId>
50-
<artifactId>lombok</artifactId>
51-
<version>1.18.22</version>
52-
</dependency>
53-
<dependency>
54-
<groupId>org.springframework</groupId>
55-
<artifactId>spring-context-support</artifactId>
56-
<version>5.1.10.RELEASE</version>
57-
<exclusions>
58-
<exclusion>
59-
<groupId>commons-logging</groupId>
60-
<artifactId>commons-logging</artifactId>
61-
</exclusion>
62-
</exclusions>
63-
</dependency>
17+
<properties>
18+
<java.version>1.8</java.version>
19+
<maven.compiler.source>${java.version}</maven.compiler.source>
20+
<maven.compiler.target>${java.version}</maven.compiler.target>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
23+
</properties>
6424

65-
<dependency>
66-
<groupId>org.springframework.boot</groupId>
67-
<artifactId>spring-boot-starter-test</artifactId>
68-
<version>2.6.3</version>
69-
<scope>test</scope>
70-
</dependency>
71-
</dependencies>
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.apache.hbase</groupId>
28+
<artifactId>hbase-client</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.apache.hadoop</groupId>
32+
<artifactId>hadoop-auth</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>cn.hutool</groupId>
36+
<artifactId>hutool-all</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.fasterxml.jackson.core</groupId>
40+
<artifactId>jackson-databind</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.alibaba</groupId>
44+
<artifactId>fastjson</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.projectlombok</groupId>
48+
<artifactId>lombok</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-context-support</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-test</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
7260
</project>

codes/javadb/hbase/src/main/java/io/github/dunwu/javadb/hbase/annotation/RowKeyRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
public @interface RowKeyRule {
2121

2222
/**
23-
* 主键
23+
* 唯一索引的 get 方法
2424
*/
25-
String pk();
25+
String uk();
2626

2727
/**
2828
* 主键类型 {@link RowType}

codes/javadb/hbase/src/main/java/io/github/dunwu/javadb/hbase/annotation/RowKeyRuleParser.java

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package io.github.dunwu.javadb.hbase.annotation;
2+
3+
import cn.hutool.core.util.HashUtil;
4+
import cn.hutool.core.util.IdUtil;
5+
import cn.hutool.core.util.ReflectUtil;
6+
import cn.hutool.core.util.StrUtil;
7+
import io.github.dunwu.javadb.hbase.constant.RowType;
8+
import io.github.dunwu.javadb.hbase.entity.BaseHbaseEntity;
9+
10+
import java.lang.reflect.Method;
11+
12+
/**
13+
* {@link RowKeyRule} 解析器
14+
*
15+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
16+
* @date 2023-11-20
17+
*/
18+
public class RowKeyUtil {
19+
20+
/**
21+
* 获取主键
22+
*/
23+
public static <T extends BaseHbaseEntity> String getRowKey(T entity) throws IllegalArgumentException {
24+
25+
String row = null;
26+
Class<? extends BaseHbaseEntity> clazz = entity.getClass();
27+
RowKeyRule rule = getRowKeyRule(entity.getClass());
28+
Method method = ReflectUtil.getMethodByName(clazz, rule.uk());
29+
if (method == null) {
30+
String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule 指定的 uk:{} 方法未找到!",
31+
clazz.getCanonicalName(), rule.uk());
32+
throw new IllegalArgumentException(msg);
33+
}
34+
switch (rule.type()) {
35+
case ORIGIN_ID:
36+
row = getRowKeyForOriginId(entity, method, rule.length());
37+
break;
38+
case TIMESTAMP:
39+
row = getRowKeyForTimestamp();
40+
break;
41+
case UUID:
42+
row = IdUtil.fastSimpleUUID();
43+
break;
44+
case BUCKET:
45+
row = getRowKeyForBucket(entity, method, rule.length(), rule.bucket());
46+
default:
47+
break;
48+
}
49+
50+
if (StrUtil.isBlank(row)) {
51+
throw new IllegalArgumentException(StrUtil.format("实体定义错误!未定义 @RowKeyRule", entity.getClass(),
52+
BaseHbaseEntity.class.getCanonicalName()));
53+
}
54+
return row;
55+
}
56+
57+
public static RowKeyRule getRowKeyRule(Class<? extends BaseHbaseEntity> clazz) {
58+
59+
RowKeyRule rule = clazz.getAnnotation(RowKeyRule.class);
60+
61+
if (rule == null) {
62+
String msg = StrUtil.format("{} 实体类定义错误!未定义 @RowKeyRule", clazz.getCanonicalName());
63+
throw new IllegalArgumentException(msg);
64+
}
65+
66+
if (rule.type() == RowType.ORIGIN_ID && rule.length() <= 0) {
67+
String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule type 为 ORIGIN_ID 时,length 必须大于 0!",
68+
clazz.getCanonicalName());
69+
throw new IllegalArgumentException(msg);
70+
}
71+
72+
if (rule.type() == RowType.BUCKET && (rule.length() <= 0 || rule.bucket() <= 0)) {
73+
String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule type 为 BUCKET 时,length 和 bucket 必须大于 0!",
74+
clazz.getCanonicalName());
75+
throw new IllegalArgumentException(msg);
76+
}
77+
return rule;
78+
}
79+
80+
public static <T extends BaseHbaseEntity> String getRowKeyForOriginId(T entity, Method method, int length)
81+
throws IllegalArgumentException {
82+
String originId;
83+
Object value = ReflectUtil.invoke(entity, method);
84+
if (value instanceof String) {
85+
originId = (String) value;
86+
} else {
87+
originId = String.valueOf(value);
88+
}
89+
if (length == 0) {
90+
throw new IllegalArgumentException("length 不能为 0");
91+
}
92+
return getRowKeyForOriginId(originId, length);
93+
}
94+
95+
public static String getRowKeyForOriginId(String bizId, int length) {
96+
return StrUtil.padPre(bizId, length, "0");
97+
}
98+
99+
public static String getRowKeyForTimestamp() {
100+
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
101+
return StrUtil.padPre(timestamp, 10, "0");
102+
}
103+
104+
public static <T extends BaseHbaseEntity> String getRowKeyForBucket(T entity, Method method, int length, int bucket)
105+
throws IllegalArgumentException {
106+
if (bucket == 0) {
107+
throw new IllegalArgumentException("bucket 不能为 0");
108+
}
109+
110+
String originId = getRowKeyForOriginId(entity, method, length);
111+
int bucketLength = getBucketIdLength(bucket);
112+
String bucketId = String.valueOf(HashUtil.fnvHash(originId) % bucket);
113+
return StrUtil.padPre(bucketId, bucketLength, "0") + originId;
114+
}
115+
116+
public static <T extends BaseHbaseEntity> String getRowKeyForBucket(String contentId, Class<T> clazz) {
117+
RowKeyRule rule = RowKeyUtil.getRowKeyRule(clazz);
118+
return RowKeyUtil.getRowKeyForBucket(contentId, rule.length(), rule.bucket());
119+
}
120+
121+
public static String getRowKeyForBucket(String bizId, int length, int bucket) throws IllegalArgumentException {
122+
String originId = getRowKeyForOriginId(bizId, length);
123+
int bucketLength = getBucketIdLength(bucket);
124+
String bucketId = String.valueOf(HashUtil.fnvHash(originId) % bucket);
125+
return StrUtil.padPre(bucketId, bucketLength, "0") + originId;
126+
}
127+
128+
private static int getBucketIdLength(int bucket) {
129+
bucket = bucket - 1;
130+
if (bucket <= 0) {
131+
return 1;
132+
}
133+
134+
int length = 0;
135+
while (bucket > 0) {
136+
length++;
137+
bucket = bucket / 10;
138+
}
139+
return length;
140+
}
141+
142+
}

0 commit comments

Comments
 (0)