-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,153 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,60 @@ | ||
<?xml version="1.0"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>io.github.dunwu</groupId> | ||
<artifactId>javadb-hbase</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>${java.version}</maven.compiler.source> | ||
<maven.compiler.target>${java.version}</maven.compiler.target> | ||
<parent> | ||
<groupId>io.github.dunwu</groupId> | ||
<artifactId>javadb</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<hbase.version>2.4.15</hbase.version> | ||
<junit.version>4.13.1</junit.version> | ||
<dunwu.version>0.5.7</dunwu.version> | ||
</properties> | ||
<artifactId>javadb-hbase</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.hbase</groupId> | ||
<artifactId>hbase-client</artifactId> | ||
<version>${hbase.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.hadoop</groupId> | ||
<artifactId>hadoop-auth</artifactId> | ||
<version>2.10.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.hutool</groupId> | ||
<artifactId>hutool-all</artifactId> | ||
<version>5.8.18</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
<version>2.13.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>1.2.83</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.22</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
<version>5.1.10.RELEASE</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>${java.version}</maven.compiler.source> | ||
<maven.compiler.target>${java.version}</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
</properties> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<version>2.6.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.hbase</groupId> | ||
<artifactId>hbase-client</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.hadoop</groupId> | ||
<artifactId>hadoop-auth</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.hutool</groupId> | ||
<artifactId>hutool-all</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 0 additions & 111 deletions
111
.../javadb/hbase/src/main/java/io/github/dunwu/javadb/hbase/annotation/RowKeyRuleParser.java
This file was deleted.
Oops, something went wrong.
142 changes: 142 additions & 0 deletions
142
codes/javadb/hbase/src/main/java/io/github/dunwu/javadb/hbase/annotation/RowKeyUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package io.github.dunwu.javadb.hbase.annotation; | ||
|
||
import cn.hutool.core.util.HashUtil; | ||
import cn.hutool.core.util.IdUtil; | ||
import cn.hutool.core.util.ReflectUtil; | ||
import cn.hutool.core.util.StrUtil; | ||
import io.github.dunwu.javadb.hbase.constant.RowType; | ||
import io.github.dunwu.javadb.hbase.entity.BaseHbaseEntity; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* {@link RowKeyRule} 解析器 | ||
* | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @date 2023-11-20 | ||
*/ | ||
public class RowKeyUtil { | ||
|
||
/** | ||
* 获取主键 | ||
*/ | ||
public static <T extends BaseHbaseEntity> String getRowKey(T entity) throws IllegalArgumentException { | ||
|
||
String row = null; | ||
Class<? extends BaseHbaseEntity> clazz = entity.getClass(); | ||
RowKeyRule rule = getRowKeyRule(entity.getClass()); | ||
Method method = ReflectUtil.getMethodByName(clazz, rule.uk()); | ||
if (method == null) { | ||
String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule 指定的 uk:{} 方法未找到!", | ||
clazz.getCanonicalName(), rule.uk()); | ||
throw new IllegalArgumentException(msg); | ||
} | ||
switch (rule.type()) { | ||
case ORIGIN_ID: | ||
row = getRowKeyForOriginId(entity, method, rule.length()); | ||
break; | ||
case TIMESTAMP: | ||
row = getRowKeyForTimestamp(); | ||
break; | ||
case UUID: | ||
row = IdUtil.fastSimpleUUID(); | ||
break; | ||
case BUCKET: | ||
row = getRowKeyForBucket(entity, method, rule.length(), rule.bucket()); | ||
default: | ||
break; | ||
} | ||
|
||
if (StrUtil.isBlank(row)) { | ||
throw new IllegalArgumentException(StrUtil.format("实体定义错误!未定义 @RowKeyRule", entity.getClass(), | ||
BaseHbaseEntity.class.getCanonicalName())); | ||
} | ||
return row; | ||
} | ||
|
||
public static RowKeyRule getRowKeyRule(Class<? extends BaseHbaseEntity> clazz) { | ||
|
||
RowKeyRule rule = clazz.getAnnotation(RowKeyRule.class); | ||
|
||
if (rule == null) { | ||
String msg = StrUtil.format("{} 实体类定义错误!未定义 @RowKeyRule", clazz.getCanonicalName()); | ||
throw new IllegalArgumentException(msg); | ||
} | ||
|
||
if (rule.type() == RowType.ORIGIN_ID && rule.length() <= 0) { | ||
String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule type 为 ORIGIN_ID 时,length 必须大于 0!", | ||
clazz.getCanonicalName()); | ||
throw new IllegalArgumentException(msg); | ||
} | ||
|
||
if (rule.type() == RowType.BUCKET && (rule.length() <= 0 || rule.bucket() <= 0)) { | ||
String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule type 为 BUCKET 时,length 和 bucket 必须大于 0!", | ||
clazz.getCanonicalName()); | ||
throw new IllegalArgumentException(msg); | ||
} | ||
return rule; | ||
} | ||
|
||
public static <T extends BaseHbaseEntity> String getRowKeyForOriginId(T entity, Method method, int length) | ||
throws IllegalArgumentException { | ||
String originId; | ||
Object value = ReflectUtil.invoke(entity, method); | ||
if (value instanceof String) { | ||
originId = (String) value; | ||
} else { | ||
originId = String.valueOf(value); | ||
} | ||
if (length == 0) { | ||
throw new IllegalArgumentException("length 不能为 0"); | ||
} | ||
return getRowKeyForOriginId(originId, length); | ||
} | ||
|
||
public static String getRowKeyForOriginId(String bizId, int length) { | ||
return StrUtil.padPre(bizId, length, "0"); | ||
} | ||
|
||
public static String getRowKeyForTimestamp() { | ||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000); | ||
return StrUtil.padPre(timestamp, 10, "0"); | ||
} | ||
|
||
public static <T extends BaseHbaseEntity> String getRowKeyForBucket(T entity, Method method, int length, int bucket) | ||
throws IllegalArgumentException { | ||
if (bucket == 0) { | ||
throw new IllegalArgumentException("bucket 不能为 0"); | ||
} | ||
|
||
String originId = getRowKeyForOriginId(entity, method, length); | ||
int bucketLength = getBucketIdLength(bucket); | ||
String bucketId = String.valueOf(HashUtil.fnvHash(originId) % bucket); | ||
return StrUtil.padPre(bucketId, bucketLength, "0") + originId; | ||
} | ||
|
||
public static <T extends BaseHbaseEntity> String getRowKeyForBucket(String contentId, Class<T> clazz) { | ||
RowKeyRule rule = RowKeyUtil.getRowKeyRule(clazz); | ||
return RowKeyUtil.getRowKeyForBucket(contentId, rule.length(), rule.bucket()); | ||
} | ||
|
||
public static String getRowKeyForBucket(String bizId, int length, int bucket) throws IllegalArgumentException { | ||
String originId = getRowKeyForOriginId(bizId, length); | ||
int bucketLength = getBucketIdLength(bucket); | ||
String bucketId = String.valueOf(HashUtil.fnvHash(originId) % bucket); | ||
return StrUtil.padPre(bucketId, bucketLength, "0") + originId; | ||
} | ||
|
||
private static int getBucketIdLength(int bucket) { | ||
bucket = bucket - 1; | ||
if (bucket <= 0) { | ||
return 1; | ||
} | ||
|
||
int length = 0; | ||
while (bucket > 0) { | ||
length++; | ||
bucket = bucket / 10; | ||
} | ||
return length; | ||
} | ||
|
||
} |
Oops, something went wrong.