Skip to content

Commit

Permalink
feat: 更新 hbase 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
dunwu committed Nov 29, 2023
1 parent 81a350a commit 6c3031b
Show file tree
Hide file tree
Showing 17 changed files with 1,153 additions and 214 deletions.
118 changes: 53 additions & 65 deletions codes/javadb/hbase/pom.xml
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>
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
public @interface RowKeyRule {

/**
* 主键
* 唯一索引的 get 方法
*/
String pk();
String uk();

/**
* 主键类型 {@link RowType}
Expand Down

This file was deleted.

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;
}

}
Loading

0 comments on commit 6c3031b

Please sign in to comment.